From 041bb9fd55894bd0d65fcac746f31b35fac6d06f Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Tue, 7 Apr 2020 18:43:34 +0200 Subject: [PATCH] added tags on the preview page added a way to disable autotagging Fixed issue with email content type the uploads page previews are now with a link Disabled logging of 404 errors --- app/Controllers/AdminController.php | 16 +++---- app/Controllers/MediaController.php | 24 ++++++++--- app/Controllers/SettingController.php | 1 + app/Controllers/UploadController.php | 6 ++- app/Controllers/UserController.php | 4 +- app/Exceptions/Handlers/AppErrorHandler.php | 14 ++++++ app/Web/Mail.php | 2 +- bootstrap/app.php | 2 +- resources/lang/en.lang.php | 1 + resources/templates/dashboard/grid.twig | 2 +- resources/templates/dashboard/system.twig | 20 ++++++--- resources/templates/upload/public.twig | 47 +++++++++++++++------ src/js/app.js | 9 ++++ 13 files changed, 107 insertions(+), 41 deletions(-) diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index 6eca5d0..f82b906 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -21,6 +21,13 @@ class AdminController extends Controller */ public function system(Request $request, Response $response): Response { + $settings = []; + foreach ($this->database->query('SELECT `key`, `value` FROM `settings`') as $setting) { + $settings[$setting->key] = $setting->value; + } + + $settings['default_user_quota'] = humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, true); + return view()->render($response, 'dashboard/system.twig', [ 'usersCount' => $usersCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `users`')->fetch()->count, 'mediasCount' => $mediasCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `uploads`')->fetch()->count, @@ -32,14 +39,7 @@ class AdminController extends Controller 'forced_lang' => $request->getAttribute('forced_lang'), 'php_version' => phpversion(), 'max_memory' => ini_get('memory_limit'), - 'register_enabled' => $this->getSetting('register_enabled', 'off'), - 'hide_by_default' => $this->getSetting('hide_by_default', 'off'), - 'copy_url_behavior' => $this->getSetting('copy_url_behavior', 'off'), - 'quota_enabled' => $this->getSetting('quota_enabled', 'off'), - 'default_user_quota' => humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, true), - 'recaptcha_enabled' => $this->getSetting('recaptcha_enabled', 'off'), - 'recaptcha_site_key' => $this->getSetting('recaptcha_site_key'), - 'recaptcha_secret_key' => $this->getSetting('recaptcha_secret_key'), + 'settings' => $settings, ]); } diff --git a/app/Controllers/MediaController.php b/app/Controllers/MediaController.php index 84f94e6..fc9bdfa 100644 --- a/app/Controllers/MediaController.php +++ b/app/Controllers/MediaController.php @@ -33,7 +33,7 @@ class MediaController extends Controller */ public function show(Request $request, Response $response, string $userCode, string $mediaCode, string $token = null): Response { - $media = $this->getMedia($userCode, $mediaCode); + $media = $this->getMedia($userCode, $mediaCode, true); if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false))) { throw new HttpNotFoundException($request); @@ -112,7 +112,7 @@ class MediaController extends Controller */ public function getRaw(Request $request, Response $response, string $userCode, string $mediaCode, ?string $ext = null): Response { - $media = $this->getMedia($userCode, $mediaCode); + $media = $this->getMedia($userCode, $mediaCode, false); if (!$media || !$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false)) { throw new HttpNotFoundException($request); @@ -144,7 +144,7 @@ class MediaController extends Controller */ public function download(Request $request, Response $response, string $userCode, string $mediaCode): Response { - $media = $this->getMedia($userCode, $mediaCode); + $media = $this->getMedia($userCode, $mediaCode, false); if (!$media || !$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false)) { throw new HttpNotFoundException($request); @@ -230,7 +230,7 @@ class MediaController extends Controller */ public function deleteByToken(Request $request, Response $response, string $userCode, string $mediaCode, string $token): Response { - $media = $this->getMedia($userCode, $mediaCode); + $media = $this->getMedia($userCode, $mediaCode, false); if (!$media) { throw new HttpNotFoundException($request); @@ -286,16 +286,28 @@ class MediaController extends Controller * @param $userCode * @param $mediaCode * + * @param bool $withTags * @return mixed */ - protected function getMedia($userCode, $mediaCode) + protected function getMedia($userCode, $mediaCode, $withTags = false) { $mediaCode = pathinfo($mediaCode)['filename']; - return $this->database->query('SELECT `uploads`.*, `users`.*, `users`.`id` AS `userId`, `uploads`.`id` AS `mediaId` FROM `uploads` INNER JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `user_code` = ? AND `uploads`.`code` = ? LIMIT 1', [ + $media = $this->database->query('SELECT `uploads`.*, `users`.*, `users`.`id` AS `userId`, `uploads`.`id` AS `mediaId` FROM `uploads` INNER JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `user_code` = ? AND `uploads`.`code` = ? LIMIT 1', [ $userCode, $mediaCode, ])->fetch(); + + if (!$withTags || !$media) { + return $media; + } + + $media->tags = []; + foreach ($this->database->query('SELECT `tags`.`id`, `tags`.`name` FROM `uploads_tags` INNER JOIN `tags` ON `uploads_tags`.`tag_id` = `tags`.`id` WHERE `uploads_tags`.`upload_id` = ?', $media->mediaId) as $tag) { + $media->tags[$tag->id] = $tag->name; + } + + return $media; } /** diff --git a/app/Controllers/SettingController.php b/app/Controllers/SettingController.php index 16fded8..3e432b2 100644 --- a/app/Controllers/SettingController.php +++ b/app/Controllers/SettingController.php @@ -29,6 +29,7 @@ class SettingController extends Controller // registrations $this->updateSetting('register_enabled', param($request, 'register_enabled', 'off')); + $this->updateSetting('auto_tagging', param($request, 'auto_tagging', 'off')); // quota $this->updateSetting('quota_enabled', param($request, 'quota_enabled', 'off')); diff --git a/app/Controllers/UploadController.php b/app/Controllers/UploadController.php index 1c75fef..b0cf325 100644 --- a/app/Controllers/UploadController.php +++ b/app/Controllers/UploadController.php @@ -200,7 +200,9 @@ class UploadController extends Controller ]); $mediaId = $this->database->getPdo()->lastInsertId(); - $this->autoTag($mediaId, $storagePath); + if ($this->getSetting('auto_tagging') === 'on') { + $this->autoTag($mediaId, $storagePath); + } $this->json['message'] = 'OK'; $this->json['url'] = urlFor("/{$user->user_code}/{$code}.{$fileInfo['extension']}"); @@ -225,7 +227,7 @@ class UploadController extends Controller $query = make(TagQuery::class); $query->addTag($type, $mediaId); - if ($type === 'application') { + if ($type === 'application' || $subtype === 'gif') { $query->addTag($subtype, $mediaId); } } diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 5d7d5f6..661925e 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -103,7 +103,7 @@ class UserController extends Controller if (param($request, 'send_notification') !== null) { $resetToken = null; - if (!empty(param($request, 'password'))) { + if (empty(param($request, 'password'))) { $resetToken = bin2hex(random_bytes(16)); $this->database->query('UPDATE `users` SET `reset_token`=? WHERE `id` = ?', [ @@ -284,7 +284,7 @@ class UserController extends Controller */ private function sendCreateNotification($request, $resetToken = null) { - if (empty(param($request, 'password'))) { + if ($resetToken === null && !empty(param($request, 'password'))) { $message = lang('mail.new_account_text_with_pw', [ param($request, 'username'), $this->config['app_name'], diff --git a/app/Exceptions/Handlers/AppErrorHandler.php b/app/Exceptions/Handlers/AppErrorHandler.php index 0da16e0..53b179f 100644 --- a/app/Exceptions/Handlers/AppErrorHandler.php +++ b/app/Exceptions/Handlers/AppErrorHandler.php @@ -2,7 +2,10 @@ namespace App\Exception\Handlers; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; use Slim\Handlers\ErrorHandler; +use Throwable; class AppErrorHandler extends ErrorHandler { @@ -10,4 +13,15 @@ class AppErrorHandler extends ErrorHandler { resolve('logger')->error($error); } + + public function __invoke(ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails): ResponseInterface + { + $response = parent::__invoke($request, $exception, $displayErrorDetails, $logErrors, $logErrorDetails); + + if ($response->getStatusCode() !== 404) { + $this->writeToErrorLog(); + } + + return $response; + } } diff --git a/app/Web/Mail.php b/app/Web/Mail.php index a0c4d00..93c6cbd 100644 --- a/app/Web/Mail.php +++ b/app/Web/Mail.php @@ -117,7 +117,7 @@ class Mail $this->addRequiredHeader('X-Mailer: PHP/'.phpversion()); $this->addRequiredHeader('MIME-Version: 1.0'); - $this->addRequiredHeader('Content-Type: text/html; charset=iso-8859-1'); + $this->addRequiredHeader('Content-Type: text/plain; charset=iso-8859-1'); $this->headers .= $this->additionalHeaders; diff --git a/bootstrap/app.php b/bootstrap/app.php index 29c2175..c99e69b 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -112,7 +112,7 @@ $errorHandler = new AppErrorHandler($app->getCallableResolver(), $app->getRespon $errorHandler->registerErrorRenderer('text/html', HtmlErrorRenderer::class); // Add Error Middleware -$errorMiddleware = $app->addErrorMiddleware($config['debug'], true, true); +$errorMiddleware = $app->addErrorMiddleware($config['debug'], false, true); $errorMiddleware->setDefaultErrorHandler($errorHandler); // Load the application routes diff --git a/resources/lang/en.lang.php b/resources/lang/en.lang.php index 0a1d388..80ebf13 100644 --- a/resources/lang/en.lang.php +++ b/resources/lang/en.lang.php @@ -172,4 +172,5 @@ Click on the following link to go to the login page: 'ldap_cant_connect' => 'Can\'t connect to the LDAP auth server.', 'upload_max_file_size' => 'The max file size is currently %s.', 'no_tags' => 'No tags added', + 'auto_tagging' => 'Auto upload tagging', ]; diff --git a/resources/templates/dashboard/grid.twig b/resources/templates/dashboard/grid.twig index 2ac058a..dbc40d7 100644 --- a/resources/templates/dashboard/grid.twig +++ b/resources/templates/dashboard/grid.twig @@ -48,7 +48,7 @@ {% if isDisplayableImage(media.mimetype) %} -
+
{% else %}
{% endif %} diff --git a/resources/templates/dashboard/system.twig b/resources/templates/dashboard/system.twig index 94512c3..efab9c4 100644 --- a/resources/templates/dashboard/system.twig +++ b/resources/templates/dashboard/system.twig @@ -62,7 +62,13 @@
- + +
+
+
+ +
+
@@ -88,7 +94,7 @@
- + {{ lang('custom_head_html_hint') }}
@@ -96,13 +102,13 @@
- +
- + 512M, 2G, 1T, ...
@@ -110,20 +116,20 @@
- +
{{ lang('only_recaptcha_v3') }}
- +
- +
{% endif %} + {% set typeMatched = false %} {% if type is same as ('image') %} + {% set typeMatched = true %}
{{ media.filename }}
-
-
- {{ media.filename }} -
-
{% elseif type is same as ('text') %} + {% set typeMatched = true %}
{{ media.text }}
-
-
- {{ media.filename }} -
-
{% elseif type is same as ('audio') %} + {% set typeMatched = true %}
{% elseif type is same as ('video') %} + {% set typeMatched = true %}
{% elseif media.mimetype is same as ('application/pdf') %} + {% set typeMatched = true %} Your browser does not support PDF previews. Download - {% else %} + {% endif %} + {% if not typeMatched %}
@@ -98,17 +95,41 @@ {{ media.filename }}
-
+
{{ media.size }}
-
+ {% if media.tags is not empty %} +
+
+ {% for tag_id, tag_name in media.tags %} + {{ tag_name }} + {% endfor %} +
+
+ {% endif %} +
+ {% else %} +
+
+ {{ media.filename }} +
+
+ {% if media.tags is not empty %} +
+
+ {% for tag_id, tag_name in media.tags %} + {{ tag_name }} + {% endfor %} +
+
+ {% endif %} {% endif %}
diff --git a/src/js/app.js b/src/js/app.js index 9814aa0..ba4524e 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -11,6 +11,15 @@ var app = { var text = Math.round(uploadProgress) + '%'; $('#uploadProgess').css({'width': text}).text(text); }, + queuecomplete: function () { + $('#uploadProgess').css({'width': '0%'}).text(''); + }, + success: function (file, response) { + $(file.previewElement) + .find('.dz-filename') + .children() + .html('' + file.name + ''); + }, timeout: 0 }; },