5
5
use Carbon \Carbon ;
6
6
use Illuminate \Http \UploadedFile ;
7
7
use Intervention \Image \Facades \Image ;
8
- use Illuminate \Support \Facades \Storage ;
9
8
10
9
11
10
final class AttachmentService extends Service
@@ -131,56 +130,29 @@ function mkdir_if_not_exists($dirPath)
131
130
}
132
131
}
133
132
134
-
135
-
136
- /**
137
- * Creates a file object from a file an uploaded file.
138
- *
139
- * @param UploadedFile $uploadedFile source file
140
- * @param string $disk target storage disk
141
- *
142
- * @return $this|null
143
- */
144
- public function fromPost ($ uploadedFile , $ disk = null )
133
+ public function fromPost ($ file , $ disk = null )
145
134
{
146
- if ($ uploadedFile === null ) {
135
+ if ($ file === null ) {
147
136
return null ;
148
137
}
149
138
150
- $ this ->filename = $ uploadedFile ->getClientOriginalName ();
151
- $ this ->filesize = method_exists ($ uploadedFile , 'getSize ' ) ? $ uploadedFile ->getSize () : $ uploadedFile ->getClientSize ();
152
- $ this ->filetype = $ uploadedFile ->getMimeType ();
139
+ $ this ->filename = $ file ->getClientOriginalName ();
140
+ $ this ->filesize = method_exists ($ file , 'getSize ' ) ? $ file ->getSize () : $ file ->getClientSize ();
141
+ $ this ->filetype = $ file ->getMimeType ();
153
142
$ this ->filepath = $ this ->filepath ?: ($ this ->getStorageDirectory () . $ this ->getPartitionDirectory () . $ this ->getDiskName ());
154
143
$ this ->filename = $ file ->getFilename ();
155
144
$ this ->filesize = $ file ->getSize ();
156
145
$ this ->filetype = $ file ->getMimeType ();
157
146
$ this ->filepath = $ this ->filepath ?: ($ this ->getStorageDirectory () . $ this ->getPartitionDirectory () . $ this ->getDiskName ());
158
147
$ this ->putFile ($ file ->getRealPath (), $ this ->filepath );
159
148
160
- $ this ->putFile ($ uploadedFile ->getRealPath (), $ this ->filepath );
149
+ $ this ->putFile ($ file ->getRealPath (), $ this ->filepath );
161
150
162
151
return $ this ;
163
152
}
164
153
165
-
166
-
167
- /**
168
- * Register an outputting model event with the dispatcher.
169
- *
170
- * @param \Closure|string $callback
171
- *
172
- * @return void
173
- */
174
- public static function outputting ($ callback )
175
- {
176
- static ::registerModelEvent ('outputting ' , $ callback );
177
- }
178
-
179
154
public function output ($ disposition = 'inline ' )
180
155
{
181
- if ($ this ->fireModelEvent ('outputting ' ) === false ) {
182
- return false ;
183
- }
184
156
185
157
header ("Content-type: " . $ this ->filetype );
186
158
header ('Content-Disposition: ' . $ disposition . '; filename=" ' . $ this ->filename . '" ' );
@@ -193,42 +165,6 @@ public function output($disposition = 'inline')
193
165
exit ($ this ->getContents ());
194
166
}
195
167
196
-
197
- /**
198
- * Get file contents from storage device.
199
- */
200
- public function getContents ()
201
- {
202
- return $ this ->storageCommand ('get ' , $ this ->filepath );
203
- }
204
-
205
-
206
- /**
207
- * Get a metadata value by key with dot notation
208
- *
209
- * @param string $key The metadata key, supports dot notation
210
- * @param mixed $default The default value to return if key is not found
211
- *
212
- * @return array|mixed
213
- */
214
- public function metadata ($ key , $ default = null )
215
- {
216
- if (is_null ($ key )) {
217
- return $ this ->metadata ;
218
- }
219
-
220
- return Arr::get ($ this ->metadata , $ key , $ default );
221
- }
222
-
223
-
224
- /**
225
- * Saves a file
226
- *
227
- * @param string $sourcePath An absolute local path to a file name to read from.
228
- * @param string $filePath A storage file path to save to.
229
- *
230
- * @return bool
231
- */
232
168
protected function putFile ($ sourcePath , $ filePath = null )
233
169
{
234
170
if ( ! $ filePath ) {
@@ -252,169 +188,4 @@ protected function putFile($sourcePath, $filePath = null)
252
188
return FileHelper::copy ($ sourcePath , $ destinationPath . basename ($ filePath ));
253
189
}
254
190
255
-
256
- protected function deleteFile ()
257
- {
258
- $ this ->storageCommand ('delete ' , $ this ->filepath );
259
- $ this ->deleteEmptyDirectory ($ this ->path );
260
- }
261
-
262
-
263
- /**
264
- * Generates a disk name from the supplied file name.
265
- */
266
- protected function getDiskName ()
267
- {
268
- if ($ this ->filepath !== null ) {
269
- return $ this ->filepath ;
270
- }
271
-
272
- $ ext = strtolower ($ this ->getExtension ());
273
- $ name = str_replace ('. ' , '' , $ this ->uuid );
274
-
275
- return $ this ->filepath = $ ext !== null ? $ name . '. ' . $ ext : $ name ;
276
- }
277
-
278
- /**
279
- * Generate a temporary url at which the current file can be downloaded until $expire
280
- *
281
- * @param Carbon $expire
282
- * @param bool $inline
283
- *
284
- * @return string
285
- */
286
- public function getTemporaryUrl (Carbon $ expire , $ inline = false )
287
- {
288
-
289
- $ payload = Crypt::encryptString (collect ([
290
- 'id ' => $ this ->uuid ,
291
- 'expire ' => $ expire ->getTimestamp (),
292
- 'shared_at ' => Carbon::now ()->getTimestamp (),
293
- 'disposition ' => $ inline ? 'inline ' : 'attachment ' ,
294
- ])->toJson ());
295
-
296
- return route ('attachments.download-shared ' , ['token ' => $ payload ]);
297
-
298
- }
299
-
300
-
301
- /**
302
- * Generates a partition for the file.
303
- * return /ABC/DE1/234 for an name of ABCDE1234.
304
- *
305
- * @return mixed
306
- */
307
- protected function getPartitionDirectory ()
308
- {
309
- return implode ('/ ' , array_slice (str_split ($ this ->uuid , 3 ), 0 , 3 )) . '/ ' ;
310
- }
311
-
312
-
313
- /**
314
- * Define the internal storage path, override this method to define.
315
- */
316
- protected function getStorageDirectory ()
317
- {
318
- return config ('attachments.storage_directory.prefix ' , 'attachments ' ) . '/ ' ;
319
- }
320
-
321
-
322
- /**
323
- * Returns true if a directory contains no files.
324
- *
325
- * @param string|null $dir the directory path
326
- *
327
- * @return bool
328
- */
329
- protected function isDirectoryEmpty ($ dir )
330
- {
331
- if ( ! $ dir || ! $ this ->storageCommand ('exists ' , $ dir )) {
332
- return null ;
333
- }
334
-
335
- return count ($ this ->storageCommand ('allFiles ' , $ dir )) === 0 ;
336
- }
337
-
338
-
339
- /**
340
- * Copy the local file to Storage
341
- *
342
- * @param string $localPath
343
- * @param string $storagePath
344
- *
345
- * @return bool
346
- */
347
- protected function copyToStorage ($ localPath , $ storagePath )
348
- {
349
- return Storage::disk ($ this ->disk )->put ($ storagePath , FileHelper::get ($ localPath ));
350
- }
351
-
352
-
353
- /**
354
- * Checks if directory is empty then deletes it,
355
- * three levels up to match the partition directory.
356
- *
357
- * @param string|null $dir the directory path
358
- *
359
- * @return void
360
- */
361
- protected function deleteEmptyDirectory ($ dir = null )
362
- {
363
- if ( ! $ this ->isDirectoryEmpty ($ dir )) {
364
- return ;
365
- }
366
-
367
- $ this ->storageCommand ('deleteDirectory ' , $ dir );
368
-
369
- $ dir = dirname ($ dir );
370
-
371
- if ( ! $ this ->isDirectoryEmpty ($ dir )) {
372
- return ;
373
- }
374
-
375
- $ this ->storageCommand ('deleteDirectory ' , $ dir );
376
-
377
- $ dir = dirname ($ dir );
378
-
379
- if ( ! $ this ->isDirectoryEmpty ($ dir )) {
380
- return ;
381
- }
382
-
383
- $ this ->storageCommand ('deleteDirectory ' , $ dir );
384
- }
385
-
386
-
387
- /**
388
- * Calls a method against File or Storage depending on local storage.
389
- * This allows local storage outside the storage/app folder and is
390
- * also good for performance. For local storage, *every* argument
391
- * is prefixed with the local root path.
392
- *
393
- * @param string $string the command string
394
- * @param string $filepath the path on storage
395
- *
396
- * @return mixed
397
- */
398
- protected function storageCommand ($ string , $ filepath )
399
- {
400
- $ args = func_get_args ();
401
- $ command = array_shift ($ args );
402
-
403
- if ($ this ->isLocalStorage ()) {
404
- $ interface = 'File ' ;
405
- $ path = $ this ->getLocalRootPath ();
406
- $ args = array_map (function ($ value ) use ($ path ) {
407
- return $ path . '/ ' . $ value ;
408
- }, $ args );
409
- } else {
410
- if (substr ($ filepath , 0 , 1 ) !== '/ ' ) {
411
- $ args [0 ] = $ filepath = '/ ' . $ filepath ;
412
- }
413
-
414
- $ interface = Storage::disk ($ this ->disk );
415
- }
416
-
417
- return forward_static_call_array ([$ interface , $ command ], $ args );
418
- }
419
-
420
191
}
0 commit comments