1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lincable\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
6
|
|
|
<<<<<<< HEAD |
|
|
|
|
7
|
|
|
======= |
8
|
|
|
use Lincable\UrlGenerator; |
9
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
10
|
|
|
use Lincable\MediaManager; |
11
|
|
|
use Lincable\UrlGenerator; |
12
|
|
|
use Illuminate\Container\Container; |
13
|
|
|
use Lincable\Http\File\FileResolver; |
14
|
|
|
<<<<<<< HEAD |
15
|
|
|
use Lincable\Eloquent\Events\UploadFailure; |
16
|
|
|
use Lincable\Eloquent\Events\UploadSuccess; |
17
|
|
|
======= |
18
|
|
|
use Lincable\Eloquent\Events\UploadSuccess; |
19
|
|
|
use Lincable\Eloquent\Events\UploadFailure; |
20
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
21
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
22
|
|
|
use Lincable\Exceptions\ConflictFileUploadHttpException; |
23
|
|
|
|
24
|
|
|
trait Lincable |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Add the lincable fields to model fillables. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function addLincableFields() |
32
|
|
|
{ |
33
|
|
|
// Get the model fillable fields. |
34
|
|
|
$fillables = $this->getFillable(); |
35
|
|
|
|
36
|
|
|
$this->fillable(array_merge($fillables, (array) $this->getUrlField())); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Link the model to a file. |
41
|
|
|
* |
42
|
|
|
* @param mixed $file |
43
|
|
|
* @return this |
44
|
|
|
*/ |
45
|
|
|
public function link($file) |
46
|
|
|
{ |
47
|
|
|
// Resolve the file object to link the model. It can |
48
|
|
|
// be a symfony uploaded file or a file request, which |
49
|
|
|
// is preferable for linking. |
50
|
|
|
$file = FileResolver::resolve($file); |
51
|
|
|
|
52
|
|
|
// Get the current media manager of application. |
53
|
|
|
$mediaManager = Container::getInstance()->make(MediaManager::class); |
54
|
|
|
<<<<<<< HEAD |
55
|
|
|
|
56
|
|
|
======= |
57
|
|
|
|
58
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
59
|
|
|
// Handle the file upload to the disk storage. All errors on upload are covered |
60
|
|
|
// for better handling upload events. One the upload has been executed with |
61
|
|
|
// success, the model is auto updated, setting the url_field model configuration |
62
|
|
|
// with the path to the new file. On error, an event of failure is dispatched and |
63
|
|
|
// a HTTPException is also reported, if not covered will return a 409 HTTP status. |
64
|
|
|
$this->handleUpload($mediaManager->getDisk(), $mediaManager->buildUrlGenerator(), $file); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the url field to link the model. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getUrlField() |
75
|
|
|
{ |
76
|
|
|
return config('lincable.models.url_field'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Throw a HTTP exception indicating that file could not be uploaded. |
81
|
|
|
* |
82
|
|
|
* @throws \Lincable\Exceptions\ConflictFileUploadHttpException |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function throwUploadFailureException() |
86
|
|
|
{ |
87
|
|
|
<<<<<<< HEAD |
88
|
|
|
throw new ConflictFileUploadHttpException('Could not store the file on disk.'); |
89
|
|
|
======= |
90
|
|
|
throw new ConflictFileUploadHttpException("Could not store the file on disk."); |
91
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Handle the file upload for the model. |
96
|
|
|
* |
97
|
|
|
* @param \Illuminate\Filesystem\FilesystemAdapter $storage |
98
|
|
|
* @param \Lincable\UrlGenerator $generator |
99
|
|
|
* @param \Illuminate\Http\File $file |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function handleUpload(FilesystemAdapter $storage, UrlGenerator $generator, File $media) |
103
|
|
|
{ |
104
|
|
|
// Get the original fillable array from model. |
105
|
|
|
$originalFillables = $this->getFillable(); |
106
|
|
|
<<<<<<< HEAD |
107
|
|
|
|
108
|
|
|
======= |
109
|
|
|
|
110
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
111
|
|
|
// Set the model instance to seed the generator and generate |
112
|
|
|
// the url injecting the model attributes. |
113
|
|
|
$url = $generator->forModel($this)->generate(); |
114
|
|
|
|
115
|
|
|
$this->addLincableFields(); |
116
|
|
|
|
117
|
|
|
rescue(function () use ($storage, $url, $media) { |
118
|
|
|
// Put the file on storage and get the full url to location. |
119
|
|
|
$fileUrl = $storage->putFile($url, $media); |
120
|
|
|
<<<<<<< HEAD |
121
|
|
|
|
122
|
|
|
======= |
123
|
|
|
|
124
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
125
|
|
|
// Update the model with the url of the uploaded file. |
126
|
|
|
$this->fill([$this->getUrlField() => $storage->url($fileUrl)]); |
127
|
|
|
|
128
|
|
|
// Send the event that the upload has been executed with success. |
129
|
|
|
event(new UploadSuccess($this, $media)); |
130
|
|
|
<<<<<<< HEAD |
131
|
|
|
|
132
|
|
|
======= |
133
|
|
|
|
134
|
|
|
>>>>>>> 4c8a26f5a973bebf5b69c343119e2161a489f17b |
135
|
|
|
$this->save(); |
136
|
|
|
}, function () use ($media) { |
137
|
|
|
|
138
|
|
|
// Send the event that the upload has failed. |
139
|
|
|
event(new UploadFailure($this, $media)); |
140
|
|
|
|
141
|
|
|
$this->throwUploadFailureException(); |
142
|
|
|
}); |
143
|
|
|
|
144
|
|
|
// Re-set the original fillables. |
145
|
|
|
$this->fillable($originalFillables); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|