103 lines
3.4 KiB
PHP
103 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Dashboard\UpdateArtworkRequest;
|
|
use App\Models\Artwork;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\View\View;
|
|
|
|
class ArtworkController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$artworks = $request->user()
|
|
->artworks()
|
|
->latest()
|
|
->paginate(20);
|
|
|
|
return view('artworks.index', [
|
|
'artworks' => $artworks,
|
|
'page_title' => 'My Artworks',
|
|
]);
|
|
}
|
|
|
|
public function edit(Request $request, int $id): View
|
|
{
|
|
$artwork = $request->user()->artworks()->whereKey($id)->firstOrFail();
|
|
$this->authorize('update', $artwork);
|
|
|
|
return view('artworks.edit', [
|
|
'artwork' => $artwork,
|
|
'page_title' => 'Edit Artwork',
|
|
]);
|
|
}
|
|
|
|
public function update(UpdateArtworkRequest $request, int $id): RedirectResponse
|
|
{
|
|
$artwork = $request->user()->artworks()->whereKey($id)->firstOrFail();
|
|
$this->authorize('update', $artwork);
|
|
|
|
$data = $request->validated();
|
|
|
|
$artwork->title = $data['title'];
|
|
$artwork->description = $data['description'] ?? null;
|
|
|
|
if ($request->hasFile('file')) {
|
|
$file = $request->file('file');
|
|
|
|
// Remove prior stored file if it's on the public disk.
|
|
if (! empty($artwork->file_path) && Storage::disk('public')->exists($artwork->file_path)) {
|
|
Storage::disk('public')->delete($artwork->file_path);
|
|
}
|
|
|
|
$size = $file->getSize() ?? 0;
|
|
$mime = $file->getMimeType() ?? 'application/octet-stream';
|
|
|
|
$dimensions = @getimagesize($file->getRealPath());
|
|
$width = is_array($dimensions) ? (int) ($dimensions[0] ?? 0) : 0;
|
|
$height = is_array($dimensions) ? (int) ($dimensions[1] ?? 0) : 0;
|
|
|
|
$path = $file->storePublicly('artworks', 'public');
|
|
|
|
$artwork->file_name = $file->getClientOriginalName();
|
|
$artwork->file_path = $path;
|
|
$artwork->file_size = (int) $size;
|
|
$artwork->mime_type = (string) $mime;
|
|
$artwork->width = max(1, $width);
|
|
$artwork->height = max(1, $height);
|
|
|
|
// If a file is replaced, clear CDN-derived fields unless a separate pipeline repopulates them.
|
|
$artwork->hash = null;
|
|
$artwork->thumb_ext = null;
|
|
$artwork->file_ext = $file->guessExtension() ?: $file->getClientOriginalExtension() ?: null;
|
|
}
|
|
|
|
$artwork->save();
|
|
|
|
return redirect()
|
|
->route('dashboard.artworks.edit', $artwork->id)
|
|
->with('status', 'Artwork updated.');
|
|
}
|
|
|
|
public function destroy(Request $request, int $id): RedirectResponse
|
|
{
|
|
$artwork = $request->user()->artworks()->whereKey($id)->firstOrFail();
|
|
$this->authorize('delete', $artwork);
|
|
|
|
// Best-effort remove stored file.
|
|
if (! empty($artwork->file_path) && Storage::disk('public')->exists($artwork->file_path)) {
|
|
Storage::disk('public')->delete($artwork->file_path);
|
|
}
|
|
|
|
$artwork->delete();
|
|
|
|
return redirect()
|
|
->route('dashboard.artworks.index')
|
|
->with('status', 'Artwork deleted.');
|
|
}
|
|
}
|