Browse Source

Use Storage::disk() where possible

Bubka 3 years ago
parent
commit
e540e2bb26

+ 2 - 1
app/Api/v1/Controllers/IconController.php

@@ -22,6 +22,7 @@ class IconController extends Controller
         ]);
         
         $path = $request->file('icon')->store('public/icons');
+        $path = $request->file('icon')->store('', 'icons');
         $response['filename'] = pathinfo($path)['basename'];
 
         return response()->json($response, 201);
@@ -36,7 +37,7 @@ class IconController extends Controller
      */
     public function delete($icon)
     {
-        Storage::delete('public/icons/' . $icon); 
+        Storage::disk('icons')->delete($icon); 
 
         return response()->json(null, 204);
     }

File diff suppressed because it is too large
+ 1 - 1
app/Console/Commands/Utils/IconGenerator.php


+ 1 - 1
app/Listeners/CleanIconStorage.php

@@ -26,7 +26,7 @@ class CleanIconStorage
      */
     public function handle(TwoFAccountDeleted $event)
     {
-        Storage::delete('public/icons/' . $event->twofaccount->icon);
+        Storage::disk('icons')->delete($event->twofaccount->icon);
         Log::info(sprintf('Icon cleaned for deleted TwoFAccount #%d', $event->twofaccount->id));
     }
 }

+ 20 - 10
app/Models/TwoFAccount.php

@@ -28,6 +28,7 @@ use Illuminate\Support\Facades\Storage;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use ParagonIE\ConstantTime\Base32;
 use Illuminate\Support\Facades\App;
+use Illuminate\Support\Facades\Http;
 
 class TwoFAccount extends Model implements Sortable
 {
@@ -535,25 +536,34 @@ class TwoFAccount extends Model implements Sortable
     private function storeImageAsIcon(string $url)
     {
         try {
-            $remoteImageURL = $url;
-            $path_parts = pathinfo($remoteImageURL);
-            $newFilename = Str::random(40) . '.' . $path_parts['extension'];
+            $path_parts = pathinfo($url);
+            $newFilename = Str::random(40).'.'.$path_parts['extension'];
             $imageFile = self::IMAGELINK_STORAGE_PATH . $newFilename;
-            $iconFile = self::ICON_STORAGE_PATH . $newFilename;
 
-            Storage::disk('local')->put($imageFile, file_get_contents($remoteImageURL));
+            try {
+                $response = Http::retry(3, 100)->get($url);
+                
+                if ($response->successful()) {
+                    Storage::disk('imagesLink')->put($newFilename, $response->body());
+                }
+            }
+            catch (\Exception $exception) {
+                Log::error(sprintf('Cannot fetch imageLink at "%s"', $url));
+            }
 
             if ( in_array(Storage::mimeType($imageFile), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp']) 
                 && getimagesize(storage_path() . '/app/' . $imageFile) )
             {
-                // Should be a valid image
-                Storage::move($imageFile, $iconFile);
-
+                // Should be a valid image, we move it to the icons disk
+                if (Storage::disk('icons')->put($newFilename, Storage::disk('imagesLink')->get($newFilename))) {
+                    Storage::disk('imagesLink')->delete($newFilename);
+                }
+                
                 Log::info(sprintf('Icon file %s stored', $newFilename));
             }
             else {
                 // @codeCoverageIgnoreStart
-                Storage::delete($imageFile);
+                Storage::disk('imagesLink')->delete($newFilename);
                 throw new \Exception('Unsupported mimeType or missing image on storage');
                 // @codeCoverageIgnoreEnd
             }
@@ -561,7 +571,7 @@ class TwoFAccount extends Model implements Sortable
             return $newFilename;
         }
         // @codeCoverageIgnoreStart
-        catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
+        catch (\Exception|\Throwable $ex) {
             Log::error(sprintf('Icon storage failed: %s', $ex->getMessage()));
             return null;
         }

Some files were not shown because too many files changed in this diff