Skip to content

Preview PDFs, replace "-" when generating the title from the file name, generate download file name from title #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()}
*/
Expand All @@ -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');
Expand Down
12 changes: 9 additions & 3 deletions resources/views/documents/shared/document_embed.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@
@case(\App\Options\FileType::Audio)
<audio controls>
<source src="{{ route('documents.stream', $document) }}" alt="{{ $document->title }}"/>
@include('documents.shared.document_download_link')
</audio>
@break
@case(\App\Options\FileType::Image)
<img src="{{ route('documents.stream', $document) }}" alt="{{ $document->title }}"/>
@break
@case(\App\Options\FileType::PDF)
<object data="{{ route('documents.stream', $document) }}" type="application/pdf" class="w-100 vh-100"></object>
@break
@case(\App\Options\FileType::Video)
<video width="1000" controls>
<source src="{{ route('documents.stream', $document) }}" alt="{{ $document->title }}"/>
@include('documents.shared.document_download_link')
</video>
@break
@default
<x-bs::alert variant="danger">{{ __('Unfortunately, it is not possible to display this document in the browser. However, you can download the file.') }}</x-bs::alert>
@include('documents.shared.document_download_link')
@break
@endswitch

<div class="mt-3">
<x-bs::button.link variant="secondary" href="{{ route('documents.download', $document) }}" class="text-nowrap">
<i class="fa fa-fw fa-download"></i> {{ __('Download file') }}
</x-bs::button.link>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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') }}
Expand Down