Laravel move url downloaded file to storage clean solution

Viewed 19

Problem statement

  • I am using Laravel Framework 8.83.23
  • being given an url, a file A gets downloaded using wget to a file system

A brief example just to get an idea:

$cmd = sprintf(
            '"%s" --no-check-certificate "%s" -O "%s" 2>&1',
            $wgetPath,
            $url,
            $filePath
        );
        $ret = exec($cmd, $output, $result_code);
  • the wget is executed using exec() on a linux file system
  • file A then needs to be moved to a Storage, let's call it Storage::disk('posters')
  • I want the Storage facade to handle the file, because, for example, if wget would download the file directly to the storage path using known file system path string, the Storage facade would not be triggered - you no longer can use Storage::fake('posters') in tests, because the file would get downloaded to the same directory, not the fake one

Proposed solution idea

  • I create a new disk, called Storage::disk('temp')
  • the wget will download the file to that directory (disc) using string path. The Storage facade will not trigger using code like storage_path(sprintf("app{$DS}temp{$DS}%s.%s", $fname, $extension)); where $DS is directory separator
  • then, move the file from Storage::disk('temp') to Storage::disk('posters')
  • working code
Storage::disk('posters')->put(
              $fName . '.jpg',
                Storage::disk('temp')->get($fName . '.jpg')
            );
  • the code above will use the Storage facade, and therefore, Storage::fake('posters') can be used in tests

Question

  • is there a cleaner solution you would propose? Creating a new storage disc for temporary files directory does not seem clean to me
  • I am not sure, whether there is some temporary storage already available for me to use?
0 Answers
Related