Mohammad Khazaee
2 years ago
4 changed files with 98 additions and 3 deletions
-
89app/Http/Controllers/FileController.php
-
2app/Http/Controllers/Traits/FileTrait.php
-
9app/Image/ImageProcessor.php
-
1docker-compose.yml
@ -0,0 +1,89 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers; |
|||
|
|||
use App\Http\Requests\FileStoreRequest; |
|||
use App\Http\Requests\FileUpdateRequest; |
|||
use App\Http\Resources\FileResource; |
|||
use App\Image\ImageProcessor; |
|||
use App\Models\File; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\DB; |
|||
use Illuminate\Support\Facades\Response; |
|||
use Illuminate\Support\Facades\Storage; |
|||
|
|||
|
|||
class FileController extends Controller |
|||
{ |
|||
public function show($collection, $uuid, $ext, Request $request, ImageProcessor $imageProcessor) |
|||
{ |
|||
if ($request->castParams == 'resource') { |
|||
return new FileResource(app()->file); |
|||
} |
|||
if (!is_null(array_intersect(array_keys($request->all()), $this->availableParams))) { |
|||
$data = $imageProcessor->processToBuffer(app()->file->getPath(), $request->all()); |
|||
$response = Response::make($data, 200); |
|||
$response->header('Content-Type','image/webp'); |
|||
return $response; |
|||
} |
|||
return response()->file(app()->file->getPath()); |
|||
} |
|||
|
|||
public function store(FileStoreRequest $request, ImageProcessor $imageProcessor) |
|||
{ |
|||
|
|||
$request->file = $imageProcessor->convertImage($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext); |
|||
if (!is_null(app()->collection->process)) { |
|||
$request->file = $imageProcessor->process($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext, app()->collection->process); |
|||
} |
|||
|
|||
$fileResource = null; |
|||
DB::transaction(function () use ($request, &$fileResource) { |
|||
$uuid = app()->uuid; |
|||
$fileResource = File::create([ |
|||
'uuid' => $uuid, |
|||
'original_name' => $request->name, |
|||
'public' => $request->public, |
|||
'ext' => $request->file->extension(), |
|||
'mimetype' => $request->file->getMimeType(), |
|||
'width' => getimagesize($request->file)[0], |
|||
'height' => getimagesize($request->file)[1], |
|||
'file_size' => $request->file->getSize(), |
|||
'sort' => $request->file->getSize(), |
|||
'alts' => $request->alts, |
|||
'description' => $request->description, |
|||
'user_id' => auth()->id(), |
|||
'ip' => $request->ip(), |
|||
'collection_id' => app()->collection->id, |
|||
'published_at' => $request->published_at, |
|||
'server_path' => '/' . date('y') . '/' . date('m') . '/', |
|||
]); |
|||
if (!app()->collection->tmp_support && app()->collection->count == 1) { |
|||
File::where('user_id', auth()->id())->delete(); |
|||
} |
|||
|
|||
|
|||
$storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext); |
|||
|
|||
if (app()->collection->public) { |
|||
Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public'); |
|||
} |
|||
}); |
|||
|
|||
return new FileResource($fileResource); |
|||
} |
|||
|
|||
public function update(FileUpdateRequest $request) |
|||
{ |
|||
app()->file->update($request->all()); |
|||
return new FileResource(app()->file); |
|||
} |
|||
|
|||
public function destroy() |
|||
{ |
|||
if (app()->file->trashed()) { |
|||
return app()->file->restore(); |
|||
} |
|||
return app()->file->delete(); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue