UploadController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Controllers;
  3. use App\Database\Queries\UserQuery;
  4. use App\Exceptions\ValidationException;
  5. use Exception;
  6. use Psr\Http\Message\ResponseInterface as Response;
  7. use Psr\Http\Message\ServerRequestInterface as Request;
  8. use Psr\Http\Message\UploadedFileInterface;
  9. class UploadController extends Controller
  10. {
  11. private $json = [
  12. 'message' => null,
  13. 'version' => PLATFORM_VERSION,
  14. ];
  15. /**
  16. * @param Response $response
  17. *
  18. * @return Response
  19. * @throws \Twig\Error\LoaderError
  20. * @throws \Twig\Error\RuntimeError
  21. * @throws \Twig\Error\SyntaxError
  22. */
  23. public function uploadWebPage(Response $response): Response
  24. {
  25. return view()->render($response, 'upload/web.twig');
  26. }
  27. /**
  28. * @param Request $request
  29. * @param Response $response
  30. * @return Response
  31. * @throws Exception
  32. */
  33. public function uploadWeb(Request $request, Response $response): Response
  34. {
  35. if ($this->config['maintenance']) {
  36. $this->json['message'] = 'Endpoint under maintenance.';
  37. return json($response, $this->json, 503);
  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. } catch (ValidationException $e) {
  75. return $e->response();
  76. }
  77. if (param($request, 'token') === null) {
  78. $this->json['message'] = 'Token not specified.';
  79. return json($response, $this->json, 400);
  80. }
  81. $user = $this->database->query('SELECT * FROM `users` WHERE `token` = ? LIMIT 1', param($request, 'token'))->fetch();
  82. if (!$user) {
  83. $this->json['message'] = 'Token specified not found.';
  84. return json($response, $this->json, 404);
  85. }
  86. try {
  87. $this->validateUser($request, $response, $file, $user);
  88. } catch (ValidationException $e) {
  89. return $e->response();
  90. }
  91. if (!$this->updateUserQuota($request, $user->id, $file->getSize())) {
  92. $this->json['message'] = 'User disk quota exceeded.';
  93. return json($response, $this->json, 507);
  94. }
  95. try {
  96. $response = $this->saveMedia($response, $file, $user);
  97. } catch (Exception $e) {
  98. $this->updateUserQuota($request, $user->id, $file->getSize(), true);
  99. throw $e;
  100. }
  101. return $response;
  102. }
  103. /**
  104. * @param Request $request
  105. * @param Response $response
  106. * @return UploadedFileInterface
  107. * @throws ValidationException
  108. */
  109. protected function validateFile(Request $request, Response $response)
  110. {
  111. if ($request->getServerParams()['CONTENT_LENGTH'] > stringToBytes(ini_get('post_max_size'))) {
  112. $this->json['message'] = 'File too large (post_max_size too low?).';
  113. throw new ValidationException(json($response, $this->json, 400));
  114. }
  115. $file = array_values($request->getUploadedFiles());
  116. /** @var UploadedFileInterface|null $file */
  117. $file = $file[0] ?? null;
  118. if ($file === null) {
  119. $this->json['message'] = 'Request without file attached.';
  120. throw new ValidationException(json($response, $this->json, 400));
  121. }
  122. if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
  123. $this->json['message'] = 'File too large (upload_max_filesize too low?).';
  124. throw new ValidationException(json($response, $this->json, 400));
  125. }
  126. return $file;
  127. }
  128. /**
  129. * @param Request $request
  130. * @param Response $response
  131. * @param UploadedFileInterface $file
  132. * @param $user
  133. * @return void
  134. * @throws ValidationException
  135. */
  136. protected function validateUser(Request $request, Response $response, UploadedFileInterface $file, $user)
  137. {
  138. if (!$user->active) {
  139. $this->json['message'] = 'Account disabled.';
  140. throw new ValidationException(json($response, $this->json, 401));
  141. }
  142. }
  143. /**
  144. * @param Response $response
  145. * @param UploadedFileInterface $file
  146. * @param $user
  147. * @return Response
  148. * @throws \League\Flysystem\FileExistsException
  149. */
  150. protected function saveMedia(Response $response, UploadedFileInterface $file, $user)
  151. {
  152. do {
  153. $code = humanRandomString();
  154. } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count > 0);
  155. $published = 1;
  156. if ($this->getSetting('hide_by_default') === 'on') {
  157. $published = 0;
  158. }
  159. $fileInfo = pathinfo($file->getClientFilename());
  160. $storagePath = "$user->user_code/$code.$fileInfo[extension]";
  161. $this->storage->writeStream($storagePath, $file->getStream()->detach());
  162. $this->database->query('INSERT INTO `uploads`(`user_id`, `code`, `filename`, `storage_path`, `published`) VALUES (?, ?, ?, ?, ?)', [
  163. $user->id,
  164. $code,
  165. $file->getClientFilename(),
  166. $storagePath,
  167. $published,
  168. ]);
  169. $this->json['message'] = 'OK';
  170. $this->json['url'] = urlFor("/{$user->user_code}/{$code}.{$fileInfo['extension']}");
  171. $this->logger->info("User $user->username uploaded new media.", [$this->database->getPdo()->lastInsertId()]);
  172. return json($response, $this->json, 201);
  173. }
  174. }