From 4d8a42cadbb96654c8069a682a9ff38dc6c45a75 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 9 Apr 2025 13:21:24 +0200 Subject: [PATCH 1/2] better naming of shared output files, support more mime types Signed-off-by: Julien Veyssier --- lib/Service/AssistantService.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Service/AssistantService.php b/lib/Service/AssistantService.php index 7de7d9fb..bc8b55cc 100644 --- a/lib/Service/AssistantService.php +++ b/lib/Service/AssistantService.php @@ -441,12 +441,20 @@ public function shareOutputFile(string $userId, int $ocpTaskId, int $fileId): st private function getTargetFileName(File $file): string { $mime = mime_content_type($file->fopen('rb')); $name = $file->getName(); - $ext = ''; - if ($mime === 'image/png') { - $ext = '.png'; - } elseif ($mime === 'image/jpeg') { - $ext = '.jpg'; - } + + $mime2ext = [ + 'image/png' => '.png', + 'image/jpeg' => '.jpg', + 'image/webp' => '.webp', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx', + 'application/vnd.oasis.opendocument.spreadsheet' => '.ods', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx', + 'application/vnd.oasis.opendocument.text-web' => '.odt', + 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx', + 'application/vnd.oasis.opendocument.presentation' => '.odp', + 'application/vnd.oasis.opendocument.graphics' => '.odg', + ]; + $ext = $mime2ext[$mime] ?? ''; if (str_ends_with($name, $ext)) { return $name; From d0f8ca234ef618603303c594fd53c3229874b3d8 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Thu, 10 Apr 2025 10:56:52 +0200 Subject: [PATCH 2/2] use ralouphie/mimey to get file extensions from mimetypes Signed-off-by: Julien Veyssier --- composer.json | 1 + composer.lock | 46 +++++++++++++++++++++++++++++++- lib/Service/AssistantService.php | 27 ++++++------------- 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index d9bcc803..7f54d4dd 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "henck/rtf-to-html": "^1.2", "html2text/html2text": "^4.3", "phpoffice/phpword": "^1.2", + "ralouphie/mimey": "^1.0", "smalot/pdfparser": "^2.11" }, "scripts": { diff --git a/composer.lock b/composer.lock index 0ef0edf5..e9bc2df6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b2ea5122778117d13a792a020c986787", + "content-hash": "82ecee0e36d1b1a3fba5a93982fd6d62", "packages": [ { "name": "erusev/parsedown", @@ -302,6 +302,50 @@ }, "time": "2024-08-30T18:03:42+00:00" }, + { + "name": "ralouphie/mimey", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/mimey.git", + "reference": "2a0e997c733b7c2f9f8b61cafb006fd5fb9fa15a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/mimey/zipball/2a0e997c733b7c2f9f8b61cafb006fd5fb9fa15a", + "reference": "2a0e997c733b7c2f9f8b61cafb006fd5fb9fa15a", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "require-dev": { + "phpunit/phpunit": "~3.7.0", + "satooshi/php-coveralls": ">=1.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Mimey\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "PHP package for converting file extensions to MIME types and vice versa.", + "support": { + "issues": "https://github.com/ralouphie/mimey/issues", + "source": "https://github.com/ralouphie/mimey/tree/master" + }, + "time": "2016-09-28T03:36:23+00:00" + }, { "name": "smalot/pdfparser", "version": "v2.12.0", diff --git a/lib/Service/AssistantService.php b/lib/Service/AssistantService.php index bc8b55cc..d23854a1 100644 --- a/lib/Service/AssistantService.php +++ b/lib/Service/AssistantService.php @@ -439,27 +439,16 @@ public function shareOutputFile(string $userId, int $ocpTaskId, int $fileId): st * @throws NotPermittedException */ private function getTargetFileName(File $file): string { - $mime = mime_content_type($file->fopen('rb')); - $name = $file->getName(); - - $mime2ext = [ - 'image/png' => '.png', - 'image/jpeg' => '.jpg', - 'image/webp' => '.webp', - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx', - 'application/vnd.oasis.opendocument.spreadsheet' => '.ods', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx', - 'application/vnd.oasis.opendocument.text-web' => '.odt', - 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx', - 'application/vnd.oasis.opendocument.presentation' => '.odp', - 'application/vnd.oasis.opendocument.graphics' => '.odg', - ]; - $ext = $mime2ext[$mime] ?? ''; + $mimeType = mime_content_type($file->fopen('rb')); + $fileName = $file->getName(); + + $mimes = new \Mimey\MimeTypes; - if (str_ends_with($name, $ext)) { - return $name; + $extension = $mimes->getExtension($mimeType); + if (is_string($extension) && $extension !== '' && !str_ends_with($fileName, $extension)) { + return $fileName . '.' . $extension; } - return $name . $ext; + return $fileName; } /**