diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index 86fb84a..417e916 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -76,7 +76,7 @@ public function download(Document $document): StreamedResponse { $this->authorize('view', $document); - return Storage::download($document->path); + return Storage::download($document->path, $document->file_name_from_title); } public function stream(Document $document) diff --git a/app/Models/Document.php b/app/Models/Document.php index f5a5ff7..2d0a0b8 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -11,6 +11,7 @@ use App\Options\FilterValue; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; @@ -28,6 +29,8 @@ * @property FileType $file_type * @property ApprovalStatus $approval_status * + * @property-read string $file_name_from_title {@see self::fileNameFromTitle()} + * * @property-read HasDocuments|Event $reference {@see self::reference()} * @property-read User $uploadedByUser {@see self::uploadedByUser()} */ @@ -48,6 +51,14 @@ class Document extends Model 'approval_status', ]; + public function fileNameFromTitle(): Attribute + { + return Attribute::get( + fn () => preg_replace('/[^A-Za-z0-9äöüÄÖÜß_\-]/u', '', str_replace(' ', '-', $this->title)) + . '.' . pathinfo($this->path, PATHINFO_EXTENSION) + ); + } + public function reference(): MorphTo { return $this->morphTo('reference'); diff --git a/resources/views/documents/shared/document_embed.blade.php b/resources/views/documents/shared/document_embed.blade.php index 5ecf92e..e9e9671 100644 --- a/resources/views/documents/shared/document_embed.blade.php +++ b/resources/views/documents/shared/document_embed.blade.php @@ -5,20 +5,26 @@ @case(\App\Options\FileType::Audio) @break @case(\App\Options\FileType::Image) {{ $document->title }} @break + @case(\App\Options\FileType::PDF) + + @break @case(\App\Options\FileType::Video) @break @default {{ __('Unfortunately, it is not possible to display this document in the browser. However, you can download the file.') }} - @include('documents.shared.document_download_link') @break @endswitch + +
+ + {{ __('Download file') }} + +
diff --git a/resources/views/documents/shared/document_form_fields.blade.php b/resources/views/documents/shared/document_form_fields.blade.php index 62166d2..86c549b 100644 --- a/resources/views/documents/shared/document_form_fields.blade.php +++ b/resources/views/documents/shared/document_form_fields.blade.php @@ -26,7 +26,7 @@ if ($refs.title.value === '') { let fileName = $refs.file.files[0].name; fileName = fileName.substring(0, fileName.lastIndexOf('.')); - $refs.title.value = fileName.replace(/_/g, ' '); + $refs.title.value = fileName.replace(/_/g, ' ').replace(/-/g, ' '); } }"> {{ __('File') }}