123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- namespace App\Controllers;
- use App\Exceptions\UnauthorizedException;
- use App\Web\Session;
- use League\Flysystem\FileExistsException;
- use League\Flysystem\FileNotFoundException;
- use League\Flysystem\Filesystem;
- use Slim\Exception\NotFoundException;
- use Slim\Http\Request;
- use Slim\Http\Response;
- use Slim\Http\Stream;
- class UploadController extends Controller
- {
- /**
- * @param Request $request
- * @param Response $response
- * @return Response
- * @throws FileExistsException
- */
- public function upload(Request $request, Response $response): Response
- {
- $json = ['message' => null];
- if ($request->getParam('token') === null) {
- $json['message'] = 'Token not specified.';
- return $response->withJson($json, 400);
- }
- $user = $this->database->query('SELECT * FROM `users` WHERE `token` = ? LIMIT 1', $request->getParam('token'))->fetch();
- if (!$user) {
- $json['message'] = 'Token specified not found.';
- return $response->withJson($json, 404);
- }
- if (!$user->active) {
- $json['message'] = 'Account disabled.';
- return $response->withJson($json, 401);
- }
- do {
- $code = uniqid();
- } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count > 0);
- /** @var \Psr\Http\Message\UploadedFileInterface $file */
- $file = $request->getUploadedFiles()['upload'];
- $fileInfo = pathinfo($file->getClientFilename());
- $storagePath = "$user->user_code/$code.$fileInfo[extension]";
- $this->getStorage()->writeStream($storagePath, $file->getStream()->detach());
- $this->database->query('INSERT INTO `uploads`(`user_id`, `code`, `filename`, `storage_path`) VALUES (?, ?, ?, ?)', [
- $user->id,
- $code,
- $file->getClientFilename(),
- $storagePath,
- ]);
- $base_url = $this->settings['base_url'];
- $json['message'] = 'OK.';
- $json['url'] = "$base_url/$user->user_code/$code.$fileInfo[extension]";
- $this->logger->info("User $user->username uploaded new media.", [$this->database->raw()->lastInsertId()]);
- return $response->withJson($json, 201);
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param $args
- * @return Response
- * @throws FileNotFoundException
- * @throws NotFoundException
- */
- public function show(Request $request, Response $response, $args): Response
- {
- $media = $this->getMedia($args['userCode'], $args['mediaCode']);
- if (!$media || !$media->published && Session::get('user_id') !== $media->user_id && !Session::get('admin', false)) {
- throw new NotFoundException($request, $response);
- }
- $filesystem = $this->getStorage();
- if (stristr($request->getHeaderLine('User-Agent'), 'TelegramBot') ||
- stristr($request->getHeaderLine('User-Agent'), 'facebookexternalhit/') ||
- stristr($request->getHeaderLine('User-Agent'), 'Discordbot/') ||
- stristr($request->getHeaderLine('User-Agent'), 'Facebot')) {
- return $this->streamMedia($request, $response, $filesystem, $media);
- } else {
- try {
- $mime = $filesystem->getMimetype($media->storage_path);
- $type = explode('/', $mime)[0];
- if ($type === 'text') {
- $media->text = $filesystem->read($media->storage_path);
- } else if (in_array($type, ['image', 'video']) && $request->getHeaderLine('Scheme') === 'HTTP/2.0') {
- $response = $response->withHeader('Link', "<{$this->settings['base_url']}/$args[userCode]/$args[mediaCode]/raw>; rel=preload; as={$type}");
- }
- } catch (FileNotFoundException $e) {
- throw $e;
- }
- return $this->view->render($response, 'upload/public.twig', [
- 'media' => $media,
- 'type' => $mime,
- 'extension' => pathinfo($media->filename, PATHINFO_EXTENSION),
- ]);
- }
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param $args
- * @return Response
- * @throws NotFoundException
- * @throws FileNotFoundException
- */
- public function getRawById(Request $request, Response $response, $args): Response
- {
- $media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', $args['id'])->fetch();
- if (!$media) {
- throw new NotFoundException($request, $response);
- }
- return $this->streamMedia($request, $response, $this->getStorage(), $media);
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param $args
- * @return Response
- * @throws NotFoundException
- * @throws FileNotFoundException
- */
- public function showRaw(Request $request, Response $response, $args): Response
- {
- $media = $this->getMedia($args['userCode'], $args['mediaCode']);
- if (!$media || !$media->published && Session::get('user_id') !== $media->user_id && !Session::get('admin', false)) {
- throw new NotFoundException($request, $response);
- }
- return $this->streamMedia($request, $response, $this->getStorage(), $media);
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param $args
- * @return Response
- * @throws NotFoundException
- * @throws FileNotFoundException
- */
- public function download(Request $request, Response $response, $args): Response
- {
- $media = $this->getMedia($args['userCode'], $args['mediaCode']);
- if (!$media || !$media->published && Session::get('user_id') !== $media->user_id && !Session::get('admin', false)) {
- throw new NotFoundException($request, $response);
- }
- return $this->streamMedia($request, $response, $this->getStorage(), $media, 'attachment');
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param $args
- * @return Response
- * @throws NotFoundException
- */
- public function togglePublish(Request $request, Response $response, $args): Response
- {
- if (Session::get('admin')) {
- $media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', $args['id'])->fetch();
- } else {
- $media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? AND `user_id` = ? LIMIT 1', [$args['id'], Session::get('user_id')])->fetch();
- }
- if (!$media) {
- throw new NotFoundException($request, $response);
- }
- $this->database->query('UPDATE `uploads` SET `published`=? WHERE `id`=?', [$media->published ? 0 : 1, $media->id]);
- return $response->withStatus(200);
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param $args
- * @return Response
- * @throws NotFoundException
- * @throws UnauthorizedException
- */
- public function delete(Request $request, Response $response, $args): Response
- {
- $media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', $args['id'])->fetch();
- if (Session::get('admin', false) || $media->user_id === Session::get('user_id')) {
- $filesystem = $this->getStorage();
- try {
- $filesystem->delete($media->storage_path);
- } catch (FileNotFoundException $e) {
- throw new NotFoundException($request, $response);
- } finally {
- $this->database->query('DELETE FROM `uploads` WHERE `id` = ?', $args['id']);
- $this->logger->info('User ' . Session::get('username') . ' deleted a media.', [$args['id']]);
- Session::set('used_space', $this->humanFilesize($this->getUsedSpaceByUser(Session::get('user_id'))));
- }
- } else {
- throw new UnauthorizedException();
- }
- return $response->withStatus(200);
- }
- /**
- * @param $userCode
- * @param $mediaCode
- * @return mixed
- */
- protected function getMedia($userCode, $mediaCode)
- {
- $mediaCode = pathinfo($mediaCode)['filename'];
- $media = $this->database->query('SELECT * FROM `uploads` INNER JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `user_code` = ? AND `uploads`.`code` = ? LIMIT 1', [
- $userCode,
- $mediaCode,
- ])->fetch();
- return $media;
- }
- /**
- * @param Request $request
- * @param Response $response
- * @param Filesystem $storage
- * @param $media
- * @param string $disposition
- * @return Response
- * @throws FileNotFoundException
- */
- protected function streamMedia(Request $request, Response $response, Filesystem $storage, $media, string $disposition = 'inline'): Response
- {
- $mime = $storage->getMimetype($media->storage_path);
- if ($request->getParam('width') !== null && explode('/', $mime)[0] === 'image') {
- $image = imagecreatefromstring($storage->read($media->storage_path));
- $scaled = imagescale($image, $request->getParam('width'), $request->getParam('height') !== null ? $request->getParam('height') : -1);
- imagedestroy($image);
- ob_start();
- imagepng($scaled, null, 9);
- $imagedata = ob_get_contents();
- ob_end_clean();
- imagedestroy($scaled);
- return $response
- ->withHeader('Content-Type', 'image/png')
- ->withHeader('Content-Disposition', $disposition . ';filename="scaled-' . pathinfo($media->filename)['filename'] . '.png"')
- ->write($imagedata);
- } else {
- ob_end_clean();
- return $response
- ->withHeader('Content-Type', $mime)
- ->withHeader('Content-Disposition', $disposition . ';filename="' . $media->filename . '"')
- ->withHeader('Content-Length', $storage->getSize($media->storage_path))
- ->withBody(new Stream($storage->readStream($media->storage_path)));
- }
- }
- }
|