Exemple #1
0
 /**
  *
  */
 protected static function boot()
 {
     parent::boot();
     static::creating(function ($photo) {
         /** @var \Illuminate\Filesystem\FilesystemAdapter $disk */
         $disk = Storage::disk();
         if ($photo['username'] == null) {
             $photo['username'] = \Auth::user()->username;
         }
         $internalUrl = $photo['internal_url'];
         $uploadtime = time();
         $hash = uniqid($uploadtime, true);
         $photo['photo_hash'] = $hash;
         $img_ext = '';
         try {
             $stream = Photo::downloadPhoto($internalUrl, $img_ext);
             $local_imgname = $hash . (strlen($img_ext) == 0 ? '' : '.' . $img_ext);
             $disk->put('/imgtemp/' . $local_imgname, $stream);
             $filename = $photo['photo_hash'] . '.' . $img_ext;
             $photo['photo_awss3_url'] = '/api/r/image/' . $filename;
             $photo['internal_url'] = '';
         } catch (ErrorException $e) {
             $console = new ConsoleOutput();
             $console->writeln(['<info>DOWNLOAD IMAGE ERROR :</info>', $e->getMessage()]);
             $photo['photo_awss3_url'] = '';
         } finally {
             $photo['photo_extensions'] = $img_ext;
         }
     });
     static::deleting(function ($photo) {
         /** @var \Illuminate\Filesystem\FilesystemAdapter $disk */
         $disk = Storage::disk();
         $filename = $photo['photo_hash'] . '.' . $photo['photo_extensions'];
         $localPath = '/imgtemp/' . $filename;
         $disk->delete($localPath);
         return parent::delete();
     });
 }