diff --git a/laravel/app/Http/Controllers/PaperController.php b/laravel/app/Http/Controllers/PaperController.php index 8466a09..36d82c8 100644 --- a/laravel/app/Http/Controllers/PaperController.php +++ b/laravel/app/Http/Controllers/PaperController.php @@ -6,11 +6,13 @@ use App\Http\Requests\UpdatePaperRequest; use App\Models\Paper; use App\Services\PaperService; +use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Symfony\Component\HttpFoundation\BinaryFileResponse; class PaperController extends Controller { @@ -93,4 +95,24 @@ public function destroy(Paper $paper) return redirect()->route('papers.index')->with('message', '削除できませんでした。'); } } + + /** + * download pdf + * + * @param Request $request + * @return BinaryFileResponse + * @throws BindingResolutionException + */ + public function downloadPdf(Request $request) + { + $paper = $this->paper_service->get($request->id); + if (empty($paper?->pdf_url)) { + abort(404); + } + $pdf = $this->paper_service->downloadPdf($paper); + return response()->make($pdf, 200, [ + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename="' . $this->paper_service->normalizeTitle($paper->title) . '.pdf' + ]); + } } diff --git a/laravel/app/Services/PaperService.php b/laravel/app/Services/PaperService.php index 0157e64..1b2aa23 100644 --- a/laravel/app/Services/PaperService.php +++ b/laravel/app/Services/PaperService.php @@ -14,6 +14,7 @@ class PaperService { /** * get all papers + * @param string|null $search_sentence * @return LengthAwarePaginator */ public function search(?string $search_sentence): LengthAwarePaginator @@ -40,10 +41,21 @@ public function search(?string $search_sentence): LengthAwarePaginator return $query->latest()->paginate(15); } + /** + * get the paper + * @param int $id + * @return Paper + */ + public function get(int $id): Paper + { + return Paper::findOrFail($id); + } + /** * create a new paper * @param StorePaperRequest $request * @return void + * @throws Exception */ public function create(StorePaperRequest $request) { @@ -61,6 +73,7 @@ public function create(StorePaperRequest $request) * @param UpdatePaperRequest $request * @param Paper $paper * @return void + * @throws Exception */ public function update(UpdatePaperRequest $request, Paper $paper) { @@ -74,45 +87,22 @@ public function update(UpdatePaperRequest $request, Paper $paper) } } - /** - * delete paper's pdf url * @param Paper $paper - * @return void + * @return string|null */ - public function unregisterPdf(Paper $paper) + public function downloadPdf(Paper $paper) { - $paper->pdf_url = null; - $paper->save(); + return Storage::disk('s3')->get($paper->pdf_url); } - - /** - * upload PDF - * @param UploadedFile $file - * @param string $title - * @return string - * @throws Exception - */ - private function uploadPdf(UploadedFile $file, string $title) - { - $path = '/'; - $pdf_name = $this->normalizeTitle($title) . '.pdf'; - if (Storage::disk('s3')->putFileAs($path, $file, $pdf_name) === false) { - throw new Exception('PDFのアップロードに失敗しました。'); - } - return config('filesystems.disks.s3.url') . '/' - . config('filesystems.disks.s3.bucket') . $path . $pdf_name; - } - - /** * normalize title * Return must have only lowercase alphabets and underscore. * @param string $title * @return string */ - private function normalizeTitle(string $title) + public function normalizeTitle(string $title) { $title = str_replace("\\", "¥", $title); $title = str_replace("/", "/", $title); @@ -129,6 +119,23 @@ private function normalizeTitle(string $title) return mb_strtolower($title); } + /** + * upload PDF + * @param UploadedFile $file + * @param string $title + * @return string + * @throws Exception + */ + private function uploadPdf(UploadedFile $file, string $title) + { + $path = '/'; + $pdf_name = $this->normalizeTitle($title) . '.pdf'; + if (Storage::disk('s3')->putFileAs($path, $file, $pdf_name) === false) { + throw new Exception('PDFのアップロードに失敗しました。'); + } + return $path . $pdf_name; + } + /** * delete the paper * @param Paper $paper diff --git a/laravel/database/factories/PaperFactory.php b/laravel/database/factories/PaperFactory.php index 2bfcacb..51c3b00 100644 --- a/laravel/database/factories/PaperFactory.php +++ b/laravel/database/factories/PaperFactory.php @@ -27,8 +27,7 @@ public function definition() "number" => $this->faker->numberBetween(), "pages" => $this->faker->numberBetween() . '--' . $this->faker->numberBetween(), "year" => $this->faker->year(), - "pdf_url" => config('filesystems.disks.s3.url') . '/' - . config('filesystems.disks.s3.bucket') . '/test.pdf' + "pdf_url" => '/test.pdf' ]; } } diff --git a/laravel/resources/views/paper/edit.blade.php b/laravel/resources/views/paper/edit.blade.php index 3f39718..d9a3117 100644 --- a/laravel/resources/views/paper/edit.blade.php +++ b/laravel/resources/views/paper/edit.blade.php @@ -90,7 +90,7 @@ class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus: @if($paper->pdf_url)