]> BookStack Code Mirror - bookstack/blobdiff - app/Uploads/ImageService.php
Allow uploads of files containing dots in filename. Closes BookStackApp/BookStack...
[bookstack] / app / Uploads / ImageService.php
index 92c3994a71e0386a792951dac75b6ad5542b9ec7..ee42e6e3345066ca26d9eac4449cc8ade2691610 100644 (file)
@@ -60,7 +60,7 @@ class ImageService
         int $resizeHeight = null,
         bool $keepRatio = true
     ) {
-        $imageName = $uploadedFile->getClientOriginalName();
+        $imageName = $this->sanitizeFileName($uploadedFile->getClientOriginalName());
         $imageData = file_get_contents($uploadedFile->getRealPath());
 
         if ($resizeWidth !== null || $resizeHeight !== null) {
@@ -426,4 +426,15 @@ class ImageService
         $basePath = ($this->storageUrl == false) ? url('/') : $this->storageUrl;
         return rtrim($basePath, '/') . $filePath;
     }
+
+    /**
+     * Returns a sanitized filename with only one file extension
+     */
+    private function sanitizeFileName(string $fileName): string
+    {
+        $parts = explode('.', $fileName);
+        $extension = array_pop($parts);
+
+        return sprintf('%s.%s', implode('-', $parts), $extension);
+    }
 }