From: Louis Jeckel Date: Wed, 9 Sep 2020 20:53:19 +0000 (+0200) Subject: downlaod pdf X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=9645ad8deb53d05c892b0353e0522e8460c8ed6a;p=psq.git downlaod pdf --- diff --git a/app/Http/Controllers/FlowpaperController.php b/app/Http/Controllers/FlowpaperController.php index 2a08031..c868d93 100644 --- a/app/Http/Controllers/FlowpaperController.php +++ b/app/Http/Controllers/FlowpaperController.php @@ -63,4 +63,12 @@ class FlowpaperController extends Controller return $file->view(); } + public function download(PdfFile $file, Request $request) + { + if (! $request->hasValidSignature()) { + abort(401); + } + return $file->downloadFile(); + } + } diff --git a/app/PdfFile.php b/app/PdfFile.php index 02495ca..1579881 100644 --- a/app/PdfFile.php +++ b/app/PdfFile.php @@ -7,6 +7,7 @@ use A17\Twill\Models\Behaviors\HasSlug; use A17\Twill\Models\Behaviors\Sortable; use A17\Twill\Models\Model as TwillModel; use App\Flowpaper\Pdf2Json; +use Aws\S3\S3Client; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; @@ -380,6 +381,25 @@ class PdfFile extends TwillModel implements Sortable } + /** + * @param false $download + * @return \Symfony\Component\HttpFoundation\StreamedResponse + */ + public function downloadFile($download = false) + { + $disposition = $download ? + 'attachment' : + 'inline'; + $fs = Storage::cloud()->getDriver(); + $stream = $fs->readStream($this->binPath); + return \Response::stream(function() use ($stream) { + fpassthru($stream); + }, 200, [ + 'Content-Type' => 'application/pdf', + 'Content-Length' => $fs->getSize($this->binPath), + 'Content-Disposition' => sprintf('%s; filename="%s.pdf"', $disposition, $this->title), + ]); + } /** diff --git a/routes/web.php b/routes/web.php index 2a660f8..36fc934 100644 --- a/routes/web.php +++ b/routes/web.php @@ -47,13 +47,15 @@ Route::domain(env('CLIENT_DOMAIN_NAME'))->group(function() { ->get('/view/{file:slug}', 'FlowpaperController@view') ->name('flowpaper.view'); - //For discover users - Route::get('/discover/{file:slug}', 'FlowpaperController@discover')->name('flowpaper.discover'); - //For admin preview - Route::get('/preview/{file:slug}', 'FlowpaperController@preview')->name('flowpaper.preview'); + Route::get('edition/{file:slug}', 'FileController@show'); + /** For discover users */ + Route::get('discover/{file:slug}', 'FlowpaperController@discover')->name('flowpaper.discover'); + /** For admin preview */ + Route::get('preview/{file:slug}', 'FlowpaperController@preview')->name('flowpaper.preview'); + /** Download file */ + Route::get('download/{file:slug}', 'FlowpaperController@download')->name('pdf.download'); - Route::get('edition/{file:slug}', 'FileController@show'); Route::get('search', 'FileController@search')->name('archives');