Skip to content

Commit

Permalink
proxy in download paper pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
eucyt committed Dec 24, 2023
1 parent c91575c commit e0aa378
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 49 deletions.
22 changes: 22 additions & 0 deletions laravel/app/Http/Controllers/PaperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'
]);
}
}
61 changes: 34 additions & 27 deletions laravel/app/Services/PaperService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PaperService
{
/**
* get all papers
* @param string|null $search_sentence
* @return LengthAwarePaginator
*/
public function search(?string $search_sentence): LengthAwarePaginator
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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);
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions laravel/database/factories/PaperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}
}
2 changes: 1 addition & 1 deletion laravel/resources/views/paper/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:

@if($paper->pdf_url)
<div class="mb-0 flex items-end">
<a href="{{ $paper->pdf_url }}" target="_blank">
<a href="{{ route('papers.pdf.download', $paper->id) }}" target="_blank">
<button type="button"
class="text-gray-900 hover:text-white border border-gray-800 hover:bg-gray-900 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2 dark:border-gray-600 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-800">
PDF Download
Expand Down
42 changes: 24 additions & 18 deletions laravel/resources/views/paper/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,31 @@ class="font-medium text-blue-600 dark:text-blue-500 hover:underline">{{ $paper->
{{ $paper->author }}
</td>
<td class="py-4 mr-4 ml-auto w-20 flex w-full justify-between">
<a href={{ $paper->url }} target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-box-arrow-up-right" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5z"/>
<path fill-rule="evenodd"
d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0v-5z"/>
</svg>
</a>
@if($paper->url)
<a href={{ $paper->url }} target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-box-arrow-up-right" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5z"/>
<path fill-rule="evenodd"
d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0v-5z"/>
</svg>
</a>
@else
<div class="w-4"></div>
@endif

<a href="{{ $paper->pdf_url }}" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-download" viewBox="0 0 16 16">
<path
d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
<path
d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
</svg>
</a>
@if($paper->pdf_url)
<a href="{{ route("papers.pdf.download", $paper->id) }}" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-download" viewBox="0 0 16 16">
<path
d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
<path
d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
</svg>
</a>
@endif

<form action="{{ route('papers.destroy', $paper->id) }}" method="POST">
@method('DELETE')
Expand Down
2 changes: 1 addition & 1 deletion laravel/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

Route::resource('papers', PaperController::class);
Route::delete('/papers/{id}', [PaperController::class, 'unregisterPdf'])->name('papers.unregisterPdf');
Route::get('/papers/{id}/pdf', [PaperController::class, 'downloadPdf'])->name('papers.pdf.download');

Route::redirect('/', route('papers.index'));

Expand Down

0 comments on commit e0aa378

Please sign in to comment.