UploadController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Controllers;
  3. use App\Database\Queries\TagQuery;
  4. use App\Database\Queries\UserQuery;
  5. use App\Exceptions\ValidationException;
  6. use Exception;
  7. use Psr\Http\Message\ResponseInterface as Response;
  8. use Psr\Http\Message\ServerRequestInterface as Request;
  9. use Psr\Http\Message\UploadedFileInterface;
  10. class UploadController extends Controller
  11. {
  12. private $json = [
  13. 'message' => null,
  14. 'version' => PLATFORM_VERSION,
  15. ];
  16. /**
  17. * @param Response $response
  18. *
  19. * @return Response
  20. * @throws \Twig\Error\LoaderError
  21. * @throws \Twig\Error\RuntimeError
  22. * @throws \Twig\Error\SyntaxError
  23. */
  24. public function uploadWebPage(Response $response): Response
  25. {
  26. $maxFileSize = min(stringToBytes(ini_get('post_max_size')), stringToBytes(ini_get('upload_max_filesize')));
  27. return view()->render($response, 'upload/web.twig', [
  28. 'max_file_size' => humanFileSize($maxFileSize),
  29. ]);
  30. }
  31. /**
  32. * @param Request $request
  33. * @param Response $response
  34. * @return Response
  35. * @throws Exception
  36. */
  37. public function uploadWeb(Request $request, Response $response): Response
  38. {
  39. try {
  40. $file = $this->validateFile($request, $response);
  41. $user = make(UserQuery::class)->get($request, $this->session->get('user_id'));
  42. $this->validateUser($request, $response, $file, $user);
  43. } catch (ValidationException $e) {
  44. return $e->response();
  45. }
  46. if (!$this->updateUserQuota($request, $user->id, $file->getSize())) {
  47. $this->json['message'] = 'User disk quota exceeded.';
  48. return json($response, $this->json, 507);
  49. }
  50. try {
  51. $response = $this->saveMedia($response, $file, $user);
  52. $this->setSessionQuotaInfo($user->current_disk_quota + $file->getSize(), $user->max_disk_quota);
  53. } catch (Exception $e) {
  54. $this->updateUserQuota($request, $user->id, $file->getSize(), true);
  55. throw $e;
  56. }
  57. return $response;
  58. }
  59. /**
  60. * @param Request $request
  61. * @param Response $response
  62. *
  63. * @return Response
  64. * @throws Exception
  65. */
  66. public function uploadEndpoint(Request $request, Response $response): Response
  67. {
  68. if ($this->config['maintenance']) {
  69. $this->json['message'] = 'Endpoint under maintenance.';
  70. return json($response, $this->json, 503);
  71. }
  72. try {
  73. $file = $this->validateFile($request, $response);
  74. if (param($request, 'token') === null) {
  75. $this->json['message'] = 'Token not specified.';
  76. return json($response, $this->json, 400);
  77. }
  78. $user = $this->database->query('SELECT * FROM `users` WHERE `token` = ? LIMIT 1', param($request, 'token'))->fetch();
  79. $this->validateUser($request, $response, $file, $user);
  80. } catch (ValidationException $e) {
  81. return $e->response();
  82. }
  83. if (!$this->updateUserQuota($request, $user->id, $file->getSize())) {
  84. $this->json['message'] = 'User disk quota exceeded.';
  85. return json($response, $this->json, 507);
  86. }
  87. try {
  88. $response = $this->saveMedia($response, $file, $user);
  89. } catch (Exception $e) {
  90. $this->updateUserQuota($request, $user->id, $file->getSize(), true);
  91. throw $e;
  92. }
  93. return $response;
  94. }
  95. /**
  96. * @param Request $request
  97. * @param Response $response
  98. * @return UploadedFileInterface
  99. * @throws ValidationException
  100. */
  101. protected function validateFile(Request $request, Response $response)
  102. {
  103. if ($request->getServerParams()['CONTENT_LENGTH'] > stringToBytes(ini_get('post_max_size'))) {
  104. $this->json['message'] = 'File too large (post_max_size too low?).';
  105. throw new ValidationException(json($response, $this->json, 400));
  106. }
  107. $file = array_values($request->getUploadedFiles());
  108. /** @var UploadedFileInterface|null $file */
  109. $file = $file[0] ?? null;
  110. if ($file === null) {
  111. $this->json['message'] = 'Request without file attached.';
  112. throw new ValidationException(json($response, $this->json, 400));
  113. }
  114. if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
  115. $this->json['message'] = 'File too large (upload_max_filesize too low?).';
  116. throw new ValidationException(json($response, $this->json, 400));
  117. }
  118. return $file;
  119. }
  120. /**
  121. * @param Request $request
  122. * @param Response $response
  123. * @param UploadedFileInterface $file
  124. * @param $user
  125. * @return void
  126. * @throws ValidationException
  127. */
  128. protected function validateUser(Request $request, Response $response, UploadedFileInterface $file, $user)
  129. {
  130. if (!$user) {
  131. $this->json['message'] = 'Token specified not found.';
  132. throw new ValidationException(json($response, $this->json, 404));
  133. }
  134. if (!$user->active) {
  135. $this->json['message'] = 'Account disabled.';
  136. throw new ValidationException(json($response, $this->json, 401));
  137. }
  138. }
  139. /**
  140. * @param Response $response
  141. * @param UploadedFileInterface $file
  142. * @param $user
  143. * @return Response
  144. * @throws \League\Flysystem\FileExistsException
  145. * @throws \League\Flysystem\FileNotFoundException
  146. */
  147. protected function saveMedia(Response $response, UploadedFileInterface $file, $user)
  148. {
  149. do {
  150. $code = humanRandomString();
  151. } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count > 0);
  152. $fileInfo = pathinfo($file->getClientFilename());
  153. $storagePath = "$user->user_code/$code.$fileInfo[extension]";
  154. $this->storage->writeStream($storagePath, $file->getStream()->detach());
  155. $this->database->query('INSERT INTO `uploads`(`user_id`, `code`, `filename`, `storage_path`, `published`) VALUES (?, ?, ?, ?, ?)', [
  156. $user->id,
  157. $code,
  158. $file->getClientFilename(),
  159. $storagePath,
  160. $user->hide_uploads == '1' ? 0 : 1,
  161. ]);
  162. $mediaId = $this->database->getPdo()->lastInsertId();
  163. if ($this->getSetting('auto_tagging') === 'on') {
  164. $this->autoTag($mediaId, $storagePath);
  165. }
  166. $this->json['message'] = 'OK';
  167. $this->json['url'] = urlFor("/{$user->user_code}/{$code}.{$fileInfo['extension']}");
  168. $this->logger->info("User $user->username uploaded new media.", [$mediaId]);
  169. return json($response, $this->json, 201);
  170. }
  171. /**
  172. * @param $mediaId
  173. * @param $storagePath
  174. * @throws \League\Flysystem\FileNotFoundException
  175. */
  176. protected function autoTag($mediaId, $storagePath)
  177. {
  178. $mime = $this->storage->getMimetype($storagePath);
  179. [$type, $subtype] = explode('/', $mime);
  180. /** @var TagQuery $query */
  181. $query = make(TagQuery::class);
  182. $query->addTag($type, $mediaId);
  183. if ($type === 'application' || $subtype === 'gif') {
  184. $query->addTag($subtype, $mediaId);
  185. }
  186. }
  187. }