@@ -28,28 +28,22 @@ public function attach($file, $attachmentable)
28
28
return false ;
29
29
}
30
30
31
+ // $this->disk()->put();
32
+
31
33
$ attachmentable ->attachments ()->create ([
32
34
'attachmentorable_id ' => auth ()->user ()->id ,
33
35
'attachmentorable_type ' => get_class (auth ()->user ()),
34
36
]);
35
37
36
-
37
- }
38
38
39
- public function name ($ name )
40
- {
41
- $ this ->filename = $ name ;
42
- return $ this ;
43
39
}
44
40
45
-
46
-
47
41
public function detach ($ file )
48
42
{
49
- //
43
+ $ this -> disk ()-> delete ( $ file );
50
44
}
51
45
52
- public function uploadOneFile (UploadedFile $ uploadedFile , $ path = null )
46
+ public function uploadFile (UploadedFile $ uploadedFile , $ path = null )
53
47
{
54
48
55
49
$ path = $ this ->path ($ path );
@@ -89,7 +83,7 @@ public function uploadOneFile(UploadedFile $uploadedFile, $path = null)
89
83
return false ;
90
84
}
91
85
92
- public function uploadOneImage (UploadedFile $ uploadedFile , $ path = null )
86
+ public function uploadImage (UploadedFile $ uploadedFile , $ path = null )
93
87
{
94
88
95
89
$ path = $ this ->path ($ path );
@@ -130,22 +124,13 @@ public function uploadOneImage(UploadedFile $uploadedFile, $path = null)
130
124
return false ;
131
125
}
132
126
133
- public function path ($ path )
134
- {
135
- return $ this ->defaultUploadFolderName . $ this ->ds . $ path ;
136
- }
137
-
138
127
function mkdir_if_not_exists ($ dirPath )
139
128
{
140
129
if (!file_exists ($ dirPath )) {
141
130
mkdir ($ dirPath , 0777 , true );
142
131
}
143
132
}
144
133
145
- public function deleteOne ($ folder = null , $ filename = null , $ disk = null )
146
- {
147
- $ this ->disk ()->delete ($ folder . $ filename );
148
- }
149
134
150
135
151
136
/**
@@ -162,73 +147,22 @@ public function fromPost($uploadedFile, $disk = null)
162
147
return null ;
163
148
}
164
149
165
- $ this ->disk = $ this ->disk ?: ($ disk ?: Storage::getDefaultDriver ());
166
150
$ this ->filename = $ uploadedFile ->getClientOriginalName ();
167
151
$ this ->filesize = method_exists ($ uploadedFile , 'getSize ' ) ? $ uploadedFile ->getSize () : $ uploadedFile ->getClientSize ();
168
152
$ this ->filetype = $ uploadedFile ->getMimeType ();
169
153
$ this ->filepath = $ this ->filepath ?: ($ this ->getStorageDirectory () . $ this ->getPartitionDirectory () . $ this ->getDiskName ());
170
- $ this ->putFile ($ uploadedFile ->getRealPath (), $ this ->filepath );
171
-
172
- return $ this ;
173
- }
174
-
175
-
176
- /**
177
- * Creates a file object from a file on the disk.
178
- *
179
- * @param string $filePath source file
180
- * @param string $disk target storage disk
181
- *
182
- * @return $this|null
183
- */
184
- public function fromFile ($ filePath , $ disk = null )
185
- {
186
- if ($ filePath === null ) {
187
- return null ;
188
- }
189
-
190
- $ file = new FileObj ($ filePath );
191
-
192
- $ this ->disk = $ this ->disk ?: ($ disk ?: Storage::getDefaultDriver ());
193
154
$ this ->filename = $ file ->getFilename ();
194
155
$ this ->filesize = $ file ->getSize ();
195
156
$ this ->filetype = $ file ->getMimeType ();
196
157
$ this ->filepath = $ this ->filepath ?: ($ this ->getStorageDirectory () . $ this ->getPartitionDirectory () . $ this ->getDiskName ());
197
158
$ this ->putFile ($ file ->getRealPath (), $ this ->filepath );
198
159
160
+ $ this ->putFile ($ uploadedFile ->getRealPath (), $ this ->filepath );
161
+
199
162
return $ this ;
200
163
}
201
164
202
165
203
- /**
204
- * Creates a file object from a stream
205
- *
206
- * @param resource $stream source stream
207
- * @param string $filename the resource filename
208
- * @param string $disk target storage disk
209
- *
210
- * @return $this|null
211
- */
212
- public function fromStream ($ stream , $ filename , $ disk = null )
213
- {
214
- if ($ stream === null ) {
215
- return null ;
216
- }
217
-
218
- $ this ->disk = $ this ->disk ?: ($ disk ?: Storage::getDefaultDriver ());
219
-
220
- $ driver = Storage::disk ($ this ->disk );
221
-
222
- $ this ->filename = $ filename ;
223
- $ this ->filepath = $ this ->filepath ?: ($ this ->getStorageDirectory () . $ this ->getPartitionDirectory () . $ this ->getDiskName ());
224
-
225
- $ driver ->putStream ($ this ->filepath , $ stream );
226
-
227
- $ this ->filesize = $ driver ->size ($ this ->filepath );
228
- $ this ->filetype = $ driver ->mimeType ($ this ->filepath );
229
-
230
- return $ this ;
231
- }
232
166
233
167
/**
234
168
* Register an outputting model event with the dispatcher.
@@ -242,100 +176,6 @@ public static function outputting($callback)
242
176
static ::registerModelEvent ('outputting ' , $ callback );
243
177
}
244
178
245
- public function getUuidAttribute ()
246
- {
247
- if ( ! empty ($ this ->attributes ['uuid ' ])) {
248
- return $ this ->attributes ['uuid ' ];
249
- }
250
-
251
- $ generator = config ('attachments.uuid_provider ' );
252
-
253
- if (strpos ($ generator , '@ ' ) !== false ) {
254
- $ generator = explode ('@ ' , $ generator , 2 );
255
- }
256
-
257
- if ( ! is_array ($ generator ) && function_exists ($ generator )) {
258
- return $ this ->uuid = call_user_func ($ generator );
259
- }
260
-
261
- if (is_callable ($ generator )) {
262
- return $ this ->uuid = forward_static_call ($ generator );
263
- }
264
-
265
- throw new \Exception ('Missing UUID provider configuration for attachments ' );
266
- }
267
-
268
-
269
- public function getExtensionAttribute ()
270
- {
271
- return pathinfo ($ this ->original_name , PATHINFO_EXTENSION );
272
- }
273
-
274
-
275
- public function getPathAttribute ()
276
- {
277
- return pathinfo ($ this ->filepath , PATHINFO_DIRNAME );
278
- }
279
-
280
-
281
- public function getUrlAttribute ()
282
- {
283
- if ($ this ->isLocalStorage ()) {
284
- return $ this ->proxy_url ;
285
- } else {
286
- return Storage::disk ($ this ->disk )->url ($ this ->filepath );
287
- }
288
- }
289
-
290
-
291
- public function getUrlInlineAttribute ()
292
- {
293
- if ($ this ->isLocalStorage ()) {
294
- return $ this ->proxy_url_inline ;
295
- } else {
296
- return Storage::disk ($ this ->disk )->url ($ this ->filepath );
297
- }
298
- }
299
-
300
-
301
- public function getProxyUrlAttribute ()
302
- {
303
- return route ('attachments.download ' , [
304
- 'id ' => $ this ->uuid ,
305
- 'name ' => $ this ->extension ?
306
- Str::slug (substr ($ this ->filename , 0 , -1 * strlen ($ this ->extension ) - 1 )) . '. ' . $ this ->extension :
307
- Str::slug ($ this ->filename )
308
- ]);
309
- }
310
-
311
-
312
- public function getProxyUrlInlineAttribute ()
313
- {
314
- return route ('attachments.download ' , [
315
- 'id ' => $ this ->uuid ,
316
- 'name ' => $ this ->extension ?
317
- Str::slug (substr ($ this ->filename , 0 , -1 * strlen ($ this ->extension ) - 1 )) . '. ' . $ this ->extension :
318
- Str::slug ($ this ->filename ),
319
- 'disposition ' => 'inline ' ,
320
- ]);
321
- }
322
-
323
-
324
- public function toArray ()
325
- {
326
- $ attributes = parent ::toArray ();
327
-
328
- return array_merge ($ attributes , [
329
- 'url ' => $ this ->url ,
330
- 'url_inline ' => $ this ->url_inline ,
331
- ]);
332
- }
333
-
334
-
335
- /*
336
- * File handling
337
- */
338
-
339
179
public function output ($ disposition = 'inline ' )
340
180
{
341
181
if ($ this ->fireModelEvent ('outputting ' ) === false ) {
@@ -479,19 +319,6 @@ protected function getStorageDirectory()
479
319
}
480
320
481
321
482
- /**
483
- * If working with local storage, determine the absolute local path.
484
- *
485
- * @return string
486
- */
487
- protected function getLocalRootPath ()
488
- {
489
- return storage_path () . '/app ' ;
490
- }
491
-
492
-
493
-
494
-
495
322
/**
496
323
* Returns true if a directory contains no files.
497
324
*
@@ -590,9 +417,4 @@ protected function storageCommand($string, $filepath)
590
417
return forward_static_call_array ([$ interface , $ command ], $ args );
591
418
}
592
419
593
-
594
- public function getConnectionName ()
595
- {
596
- return config ('attachments.database.connection ' ) ?? $ this ->connection ;
597
- }
598
420
}
0 commit comments