-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MediaCollection enum and implement media download permissions
- Loading branch information
1 parent
550afd7
commit 0a1e7a8
Showing
18 changed files
with
330 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
enum MediaCollection: string | ||
{ | ||
case MANUSCRIPT = 'manuscript'; | ||
case SUPPLEMENTARY_FILE = 'supplementary_file'; | ||
case PUBLICATION = 'publication'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/Http/Controllers/PublicationSupplementaryFileController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Enums\SupplementaryFileType; | ||
use App\Http\Resources\MediaResource; | ||
use App\Models\Publication; | ||
use Exception; | ||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Gate; | ||
use Illuminate\Validation\Rule; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class PublicationSupplementaryFileController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index(Publication $publication) | ||
{ | ||
Gate::authorize('view', $publication); | ||
|
||
$media = $publication->getMedia('supplementary_file'); | ||
|
||
return MediaResource::collection($media->sortBy('created_at', SORT_REGULAR, true)); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(Request $request, Publication $publication) | ||
{ | ||
Gate::authorize('update', $publication); | ||
|
||
$validated = $request->validate([ | ||
'pdf' => 'required|file|mimes:pdf|max:50000', | ||
'supplementary_file_type' => ['required', Rule::enum(SupplementaryFileType::class)], | ||
'description' => 'nullable|string', | ||
]); | ||
|
||
$media = $publication->addSupplementaryFile($validated['pdf'], $validated['supplementary_file_type'], $validated['description'] ?? null); | ||
|
||
activity() | ||
->performedOn($publication) | ||
->withProperties($media->toArray()) | ||
->causedBy($request->user()) | ||
->log('publication.file.uploaded'); | ||
|
||
return new MediaResource($media); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(Request $request, Publication $publication, string $uuid) | ||
{ | ||
Gate::authorize('view', $publication); | ||
|
||
$media = $publication->getSupplementaryFile($uuid); | ||
|
||
$download = $request->query('download', false); | ||
if ($download && Gate::allow('viewSupplementaryPdf', $publication, $media)) { | ||
return $media; | ||
} | ||
|
||
return MediaResource::make($media); | ||
|
||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(Publication $publication, string $uuid) | ||
{ | ||
Gate::authorize('update', $publication); | ||
|
||
try { | ||
$publication->deleteSupplementaryFile($uuid); | ||
} catch (FileNotFoundException $e) { | ||
throw new NotFoundHttpException('File not found.'); | ||
} catch (Exception $e) { | ||
return response()->json([ | ||
'message' => 'This file is locked.', | ||
], 403); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.