-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUploadPresentation.php
116 lines (103 loc) · 3.47 KB
/
UploadPresentation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace App\Livewire\Presentation;
use App\Traits\FileValidation;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;
use Livewire\Component;
use Livewire\WithFileUploads;
class UploadPresentation extends Component
{
use WithFileUploads;
use FileValidation;
public $file;
public $presentation;
public $filename;
/**
* Triggered when initializing the component
* @param $presentation
* @return void
*/
public function mount($presentation)
{
$this->presentation = $presentation;
}
/**
* If a file already is uploaded as presentation, it updates it
* @return void
*/
public function updatedFile()
{
$this->validateFileNameLength($this->file, 'file');
$this->validate([
'file' => ['required',
'file',
'max:10240',
'mimetypes:application/pdf,application/vnd.ms-powerpoint,application
/vnd.openxmlformats-officedocument.presentationml.presentation',
]
], [
'file.required' => 'The file is required',
'file.max' => 'The file must not be larger than 10MB',
'file.mimetypes' => 'The file must be a pdf, powerpoint, or presentation document',
]);
$this->filename = $this->file->getClientOriginalName();
}
/**
* Downloads the available file
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function downloadFile()
{
return Storage::download($this->presentation->file_path, $this->presentation->file_original_name);
}
/**
* The presentation is saved
* @return void
*/
public function save()
{
$this->validate([
'file' => ['required',
'file',
'max:10240',
'mimetypes:application/pdf,application/vnd.ms-powerpoint,application
/vnd.openxmlformats-officedocument.presentationml.presentation',
],
], [
'file.required' => 'The file is required',
'file.max' => 'The file must not be larger than 10MB',
'file.mimetypes' => 'The file must be a pdf, powerpoint, or presentation document',
]);
Storage::delete('presentations' . explode('@', Auth::user()->email)[0] . '-presentation');
$path = $this->file->storeAs('presentations', explode('@', Auth::user()->email)[0] . '-presentation');
$this->presentation->file_path = $path;
$this->presentation->file_original_name = $this->file->getClientOriginalName();
$this->presentation->save();
session()->flash('message', 'Presentation is successfully updated.');
}
/**
* Removes the uploaded ifle
* @return void
*/
public function delete()
{
$this->filename = null;
$this->file = null;
Storage::delete('presentations' . explode('@', Auth::user()->email)[0] . '-presentation');
$this->presentation->file_path = '';
$this->presentation->file_original_name = '';
$this->presentation->save();
session()->flash('message', 'Presentation is successfully removed.');
}
/**
* Renders the component
* @return View
*/
public function render(): View
{
return view('livewire.presentation.upload-presentation');
}
}