16
16
use Intervention \Image \ImageManager ;
17
17
use League \Flysystem \Util ;
18
18
use Symfony \Component \HttpFoundation \File \UploadedFile ;
19
+ use Symfony \Component \HttpFoundation \StreamedResponse ;
19
20
20
21
class ImageService
21
22
{
@@ -39,11 +40,20 @@ public function __construct(Image $image, ImageManager $imageTool, FileSystem $f
39
40
/**
40
41
* Get the storage that will be used for storing images.
41
42
*/
42
- protected function getStorage (string $ imageType = '' ): FileSystemInstance
43
+ protected function getStorageDisk (string $ imageType = '' ): FileSystemInstance
43
44
{
44
45
return $ this ->fileSystem ->disk ($ this ->getStorageDiskName ($ imageType ));
45
46
}
46
47
48
+ /**
49
+ * Check if local secure image storage (Fetched behind authentication)
50
+ * is currently active in the instance.
51
+ */
52
+ protected function usingSecureImages (): bool
53
+ {
54
+ return $ this ->getStorageDiskName ('gallery ' ) === 'local_secure_images ' ;
55
+ }
56
+
47
57
/**
48
58
* Change the originally provided path to fit any disk-specific requirements.
49
59
* This also ensures the path is kept to the expected root folders.
@@ -126,7 +136,7 @@ public function saveNewFromBase64Uri(string $base64Uri, string $name, string $ty
126
136
*/
127
137
public function saveNew (string $ imageName , string $ imageData , string $ type , int $ uploadedTo = 0 ): Image
128
138
{
129
- $ storage = $ this ->getStorage ($ type );
139
+ $ storage = $ this ->getStorageDisk ($ type );
130
140
$ secureUploads = setting ('app-secure-images ' );
131
141
$ fileName = $ this ->cleanImageFileName ($ imageName );
132
142
@@ -243,7 +253,7 @@ public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRat
243
253
return $ this ->getPublicUrl ($ thumbFilePath );
244
254
}
245
255
246
- $ storage = $ this ->getStorage ($ image ->type );
256
+ $ storage = $ this ->getStorageDisk ($ image ->type );
247
257
if ($ storage ->exists ($ this ->adjustPathForStorageDisk ($ thumbFilePath , $ image ->type ))) {
248
258
return $ this ->getPublicUrl ($ thumbFilePath );
249
259
}
@@ -307,7 +317,7 @@ protected function resizeImage(string $imageData, $width = 220, $height = null,
307
317
*/
308
318
public function getImageData (Image $ image ): string
309
319
{
310
- $ storage = $ this ->getStorage ();
320
+ $ storage = $ this ->getStorageDisk ();
311
321
312
322
return $ storage ->get ($ this ->adjustPathForStorageDisk ($ image ->path , $ image ->type ));
313
323
}
@@ -330,7 +340,7 @@ public function destroy(Image $image)
330
340
protected function destroyImagesFromPath (string $ path , string $ imageType ): bool
331
341
{
332
342
$ path = $ this ->adjustPathForStorageDisk ($ path , $ imageType );
333
- $ storage = $ this ->getStorage ($ imageType );
343
+ $ storage = $ this ->getStorageDisk ($ imageType );
334
344
335
345
$ imageFolder = dirname ($ path );
336
346
$ imageFileName = basename ($ path );
@@ -417,7 +427,7 @@ public function imageUriToBase64(string $uri): ?string
417
427
}
418
428
419
429
$ storagePath = $ this ->adjustPathForStorageDisk ($ storagePath );
420
- $ storage = $ this ->getStorage ();
430
+ $ storage = $ this ->getStorageDisk ();
421
431
$ imageData = null ;
422
432
if ($ storage ->exists ($ storagePath )) {
423
433
$ imageData = $ storage ->get ($ storagePath );
@@ -435,6 +445,31 @@ public function imageUriToBase64(string $uri): ?string
435
445
return 'data:image/ ' . $ extension . ';base64, ' . base64_encode ($ imageData );
436
446
}
437
447
448
+ /**
449
+ * Check if the given path exists in the local secure image system.
450
+ * Returns false if local_secure is not in use.
451
+ */
452
+ public function pathExistsInLocalSecure (string $ imagePath ): bool
453
+ {
454
+ $ disk = $ this ->getStorageDisk ('gallery ' );
455
+
456
+ // Check local_secure is active
457
+ return $ this ->usingSecureImages ()
458
+ // Check the image file exists
459
+ && $ disk ->exists ($ imagePath )
460
+ // Check the file is likely an image file
461
+ && strpos ($ disk ->getMimetype ($ imagePath ), 'image/ ' ) === 0 ;
462
+ }
463
+
464
+ /**
465
+ * For the given path, if existing, provide a response that will stream the image contents.
466
+ */
467
+ public function streamImageFromStorageResponse (string $ imageType , string $ path ): StreamedResponse
468
+ {
469
+ $ disk = $ this ->getStorageDisk ($ imageType );
470
+ return $ disk ->response ($ path );
471
+ }
472
+
438
473
/**
439
474
* Get a storage path for the given image URL.
440
475
* Ensures the path will start with "uploads/images".
0 commit comments