コード例 #1
0
ファイル: ImageController.php プロジェクト: jdrda/olapus
 /**
  * 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);
 }
コード例 #2
0
ファイル: Stream.php プロジェクト: boomcms/boom-core
 /**
  * This code has been adapted the code at http://www.tuxxin.com/php-mp4-streaming.
  */
 public function getResponse()
 {
     $stream = fopen($this->asset->getFilename(), 'rb');
     $size = $length = $this->asset->getFilesize();
     $start = 0;
     $end = $size - 1;
     $code = 200;
     $headers = ['Content-Type' => $this->asset->getMimetype(), 'Accept-Ranges' => 'bytes'];
     if ($range = Request::header('Range')) {
         $c_start = $start;
         $c_end = $end;
         list(, $range) = explode('=', $range, 2);
         if (strpos($range, ',') !== false) {
             abort(416, null, ['Content-Range' => "bytes {$start}-{$end}/{$size}"]);
         }
         if ($range === '-') {
             $c_start = $size - substr($range, 1);
         } else {
             $range = explode('-', $range);
             $c_start = $range[0];
             $c_end = isset($range[1]) && is_numeric($range[1]) ? $range[1] : $size;
         }
         $c_end = $c_end > $end ? $end : $c_end;
         if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
             abort(416, null, ['Content-Range' => "bytes {$start}-{$end}/{$size}"]);
         }
         $start = $c_start;
         $end = $c_end;
         $length = $end - $start + 1;
         fseek($stream, $start);
         $code = 206;
     }
     $headers['Content-Range'] = "bytes {$start}-{$end}/{$size}";
     $headers['Content-Length'] = $length;
     return Response::stream(function () use($stream, $end) {
         $buffer = 1024 * 8;
         while (!feof($stream) && ($p = ftell($stream)) <= $end) {
             if ($p + $buffer > $end) {
                 $buffer = $end - $p + 1;
             }
             set_time_limit(0);
             echo fread($stream, $buffer);
             flush();
         }
         fclose($stream);
     }, $code, $headers);
 }
コード例 #3
0
ファイル: Templates.php プロジェクト: imanghafoori1/boom-core
 /**
  * Display a list of pages which use a given template.
  */
 public function pages(Template $template)
 {
     $pages = Helpers::getPages(['template' => $template, 'order' => 'title asc']);
     if ($this->request->route()->getParameter('format') !== 'csv') {
         return view($this->viewPrefix . '.pages', ['pages' => $pages, 'template' => $template]);
     }
     $headers = ['Content-type' => 'text/csv', 'Content-Disposition' => "attachment; filename=pages_with_template_{$template->getFilename()}.csv"];
     $callback = function () use($pages) {
         $fh = fopen('php://output', 'w');
         fputcsv($fh, ['Title', 'URL', 'Visible?', 'Last edited']);
         foreach ($pages as $p) {
             $data = ['title' => $p->getTitle(), 'url' => (string) $p->url(), 'visible' => $p->isVisible() ? 'Yes' : 'No', 'last_edited' => $p->getLastModified()->format('Y-m-d H:i:s')];
             fputcsv($fh, $data);
         }
         fclose($fh);
     };
     return Response::stream($callback, 200, $headers);
 }
コード例 #4
0
ファイル: Stream.php プロジェクト: robbytaylor/boom-core
 public function getResponse()
 {
     $size = $fullsize = $this->asset->getFilesize();
     $stream = fopen($this->asset->getFilename(), 'r');
     $code = 200;
     $headers = ['Content-type' => $this->asset->getMimetype()];
     if ($range = Request::header('Range')) {
         $eqPos = strpos($range, '=');
         $toPos = strpos($range, '-');
         $unit = substr($range, 0, $eqPos);
         $start = intval(substr($range, $eqPos + 1, $toPos));
         $success = fseek($stream, $start);
         if ($success == 0) {
             $size = $fullsize - $start;
             $code = 206;
             $headers['Accept-Ranges'] = $unit;
             $headers['Content-Range'] = $unit . ' ' . $start . '-' . ($fullsize - 1) . '/' . $fullsize;
         }
     }
     $headers['Content-Length'] = $size;
     return Response::stream(function () use($stream) {
         fpassthru($stream);
     }, $code, $headers);
 }
コード例 #5
0
ファイル: VideoController.php プロジェクト: nandeeshmp/app
 public function stream($video)
 {
     if (!Auth::user()->can('view-video')) {
         return view('errors.denied');
     }
     $filesystem = new Filesystem(new Adapter(base_path()));
     $location = '/files/video/' . $video;
     $stream = $filesystem->readStream($location);
     $headers = ["Content-Type" => $filesystem->getMimetype($location), "Content-Length" => $filesystem->getSize($location), "Content-disposition" => "attachment; filename=\"" . basename($location) . "\""];
     return Response::stream(function () use($stream) {
         fpassthru($stream);
     }, 200, $headers);
 }
コード例 #6
0
ファイル: SessionController.php プロジェクト: emadmrz/tinker
 /**
  * Created By Dara on 21/2/2016
  * get the video of the session
  */
 public function showVideo($videoName)
 {
     $path = storage_path('app') . '/' . $videoName;
     $file = File::get($path);
     $type = File::mimeType($path);
     $headers = ['Content-Type' => 'video/mp4', 'Content-Length' => File::size($path)];
     return Response::stream(function () use($path) {
         try {
             $stream = fopen($path, 'r');
             fpassthru($stream);
         } catch (Exception $e) {
             Log::error($e);
         }
     }, 200, $headers);
 }