UploadController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. if ($this->config['maintenance']) {
  40. $this->json['message'] = 'Endpoint under maintenance.';
  41. return json($response, $this->json, 503);
  42. }
  43. try {
  44. $file = $this->validateFile($request, $response);
  45. $user = make(UserQuery::class)->get($request, $this->session->get('user_id'));
  46. $this->validateUser($request, $response, $file, $user);
  47. } catch (ValidationException $e) {
  48. return $e->response();
  49. }
  50. if (!$this->updateUserQuota($request, $user->id, $file->getSize())) {
  51. $this->json['message'] = 'User disk quota exceeded.';
  52. return json($response, $this->json, 507);
  53. }
  54. try {
  55. $response = $this->saveMedia($response, $file, $user);
  56. $this->setSessionQuotaInfo($user->current_disk_quota + $file->getSize(), $user->max_disk_quota);
  57. } catch (Exception $e) {
  58. $this->updateUserQuota($request, $user->id, $file->getSize(), true);
  59. throw $e;
  60. }
  61. return $response;
  62. }
  63. /**
  64. * @param Request $request
  65. * @param Response $response
  66. *
  67. * @return Response
  68. * @throws Exception
  69. */
  70. public function uploadEndpoint(Request $request, Response $response): Response
  71. {
  72. if ($this->config['maintenance']) {
  73. $this->json['message'] = 'Endpoint under maintenance.';
  74. return json($response, $this->json, 503);
  75. }
  76. try {
  77. $file = $this->validateFile($request, $response);
  78. } catch (ValidationException $e) {
  79. return $e->response();
  80. }
  81. if (param($request, 'token') === null) {
  82. $this->json['message'] = 'Token not specified.';
  83. return json($response, $this->json, 400);
  84. }
  85. $user = $this->database->query('SELECT * FROM `users` WHERE `token` = ? LIMIT 1', param($request, 'token'))->fetch();
  86. if (!$user) {
  87. $this->json['message'] = 'Token specified not found.';
  88. return json($response, $this->json, 404);
  89. }
  90. try {
  91. $this->validateUser($request, $response, $file, $user);
  92. } catch (ValidationException $e) {
  93. return $e->response();
  94. }
  95. if (!$this->updateUserQuota($request, $user->id, $file->getSize())) {
  96. $this->json['message'] = 'User disk quota exceeded.';
  97. return json($response, $this->json, 507);
  98. }
  99. try {
  100. $response = $this->saveMedia($response, $file, $user);
  101. } catch (Exception $e) {
  102. $this->updateUserQuota($request, $user->id, $file->getSize(), true);
  103. throw $e;
  104. }
  105. return $response;
  106. }
  107. /**
  108. * @param Request $request
  109. * @param Response $response
  110. * @return UploadedFileInterface
  111. * @throws ValidationException
  112. */
  113. protected function validateFile(Request $request, Response $response)
  114. {
  115. if ($request->getServerParams()['CONTENT_LENGTH'] > stringToBytes(ini_get('post_max_size'))) {
  116. $this->json['message'] = 'File too large (post_max_size too low?).';
  117. throw new ValidationException(json($response, $this->json, 400));
  118. }
  119. $file = array_values($request->getUploadedFiles());
  120. /** @var UploadedFileInterface|null $file */
  121. $file = $file[0] ?? null;
  122. if ($file === null) {
  123. $this->json['message'] = 'Request without file attached.';
  124. throw new ValidationException(json($response, $this->json, 400));
  125. }
  126. if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
  127. $this->json['message'] = 'File too large (upload_max_filesize too low?).';
  128. throw new ValidationException(json($response, $this->json, 400));
  129. }
  130. return $file;
  131. }
  132. /**
  133. * @param Request $request
  134. * @param Response $response
  135. * @param UploadedFileInterface $file
  136. * @param $user
  137. * @return void
  138. * @throws ValidationException
  139. */
  140. protected function validateUser(Request $request, Response $response, UploadedFileInterface $file, $user)
  141. {
  142. if (!$user->active) {
  143. $this->json['message'] = 'Account disabled.';
  144. throw new ValidationException(json($response, $this->json, 401));
  145. }
  146. }
  147. /**
  148. * @param Response $response
  149. * @param UploadedFileInterface $file
  150. * @param $user
  151. * @return Response
  152. * @throws \League\Flysystem\FileExistsException
  153. * @throws \League\Flysystem\FileNotFoundException
  154. */
  155. protected function saveMedia(Response $response, UploadedFileInterface $file, $user)
  156. {
  157. do {
  158. $code = humanRandomString();
  159. } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count > 0);
  160. $published = 1;
  161. if ($this->getSetting('hide_by_default') === 'on') {
  162. $published = 0;
  163. }
  164. $fileInfo = pathinfo($file->getClientFilename());
  165. $storagePath = "$user->user_code/$code.$fileInfo[extension]";
  166. $this->storage->writeStream($storagePath, $file->getStream()->detach());
  167. $this->database->query('INSERT INTO `uploads`(`user_id`, `code`, `filename`, `storage_path`, `published`) VALUES (?, ?, ?, ?, ?)', [
  168. $user->id,
  169. $code,
  170. $file->getClientFilename(),
  171. $storagePath,
  172. $published,
  173. ]);
  174. $mediaId = $this->database->getPdo()->lastInsertId();
  175. $this->autoTag($mediaId, $storagePath);
  176. $this->json['message'] = 'OK';
  177. $this->json['url'] = urlFor("/{$user->user_code}/{$code}.{$fileInfo['extension']}");
  178. $this->logger->info("User $user->username uploaded new media.", [$mediaId]);
  179. return json($response, $this->json, 201);
  180. }
  181. /**
  182. * @param $mediaId
  183. * @param $storagePath
  184. * @throws \League\Flysystem\FileNotFoundException
  185. */
  186. protected function autoTag($mediaId, $storagePath)
  187. {
  188. $mime = $this->storage->getMimetype($storagePath);
  189. [$type, $subtype] = explode('/', $mime);
  190. /** @var TagQuery $query */
  191. $query = make(TagQuery::class);
  192. $query->addTag($type, $mediaId);
  193. if ($type === 'application') {
  194. $query->addTag($subtype, $mediaId);
  195. }
  196. }
  197. }