Exemplo n.º 1
0
 /**
  * Get image from storage
  * 
  * @param Request $request
  * @return file
  */
 public function getImage(Request $request)
 {
     /**
      * Check the cache
      */
     $cacheKey = 'image:' . $request->imageName . ':' . $request->imageExtension;
     /**
      * File cached
      */
     if (Cache::has($cacheKey)) {
         $imageMeta = Cache::get($cacheKey);
     } else {
         /**
          * Get META information
          */
         $imageMeta = Image::where(['url' => $request->imageName, 'image_extension' => $request->imageExtension])->first(['image_mime_type', 'image_size', 'id', 'updated_at', 'image_etag']);
         /**
          * File does not exist
          */
         if (empty($imageMeta) == TRUE) {
             App::abort(404);
         }
         /**
          * Save meta information to cache
          */
         Cache::forever($cacheKey, $imageMeta);
     }
     /**
      * Get filename
      */
     $filename = Helpers::getStorageFilename(env('APP_IMAGE_STORAGE_DIRECTORY', 'images'), $imageMeta->id);
     /**
      * Prepare stream
      */
     $stream = Storage::readStream($filename);
     /**
      * File headers
      */
     $headers = array('Content-Description' => 'File Transfer', 'Content-Type' => $imageMeta->image_mime_type, 'Content-Transfer-Encoding' => 'binary', 'Pragma' => 'public', 'Expires' => Carbon::createFromTimestamp(time() + 3600)->toRfc2822String(), 'Last-Modified' => $imageMeta->updated_at->toRfc2822String(), 'Etag' => $imageMeta->image_etag);
     /**
      * Response code cached
      */
     if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $imageMeta->image_etag || isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $imageMeta->updated_at->toRfc2822String()) {
         $responseCode = 304;
     } else {
         $responseCode = 200;
     }
     /**
      * Stream to browser
      */
     return Response::stream(function () use($stream) {
         fpassthru($stream);
     }, $responseCode, $headers);
 }
Exemplo n.º 2
0
 /**
  * Save media to storage
  * 
  * @param object $object
  * @param Request $request
  * @param boolean $update
  * @return boolean
  */
 public function saveMediaToStorage($object, $request, $update = FALSE)
 {
     /**
      * Get filename
      */
     $filename = Helpers::getStorageFilename(env('APP_IMAGE_STORAGE_DIRECTORY', 'images'), $object->id);
     /*
      * Check if requested and then save
      */
     if ($update == FALSE || $update == TRUE && $request->has('image')) {
         $resource = fopen($request->image, 'r');
         Storage::put($filename, $resource);
         fclose($resource);
         /**
          * Update meta information
          */
         return TRUE;
     }
     return FALSE;
 }