You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

129 lines
5.0 KiB

<?php
namespace App\Http\Controllers;
use App\Http\Requests\FileStoreRequest;
use App\Http\Resources\FileResource;
use App\Image\ImageProcessor;
use App\Jobs\FileConversionQueue;
use App\Models\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\Console\Input\Input;
class FileController extends Controller
{
public function show($collection, $uuid, $ext)
{
dump($collection, $uuid, $ext);
}
public function private($collection, $path)
{
// Storage::disk(app()->collection->disk)->download($path);
}
public function store(Request $request, ImageProcessor $imageProcessor)
{
if (!app()->collection->tmp_support && !$request->model_id) {
return abort(403);
}
if (app()->collection->count !== 1 && (app()->collection->count <= File::where('model_id', auth()->id())->count()) && !app()->collection->tmp_support) {
return abort(403);
}
if (!isset(app()->file) && is_null($request->file('file'))) {
return abort(403);
}
$fieldsValidate = [
"alts" => [app()->collection->alt_required ? "required" : "nullable", 'array'],
"alts.*" => [app()->collection->alt_required ? "required" : "nullable", 'max:1000'],
"description" => [app()->collection->description_required ? "required" : "nullable", 'max:300'],
'original_name' => ["string", "nullable", 'max:300'],
'public' => ['boolean', 'nullable'],
'published_at' => ['date_format:Y-m-d H:i:s', 'nullable'],
];
$fieldsValidate['file'] =
is_null($request->file('file')) ?
[
'string',
'required',
'max:300'
] :
[
"mimes:" . app()->collection->getExts(),
"mimetypes:" . app()->collection->getMimeTypes(),
!$this->isImage($request->file->path()) ?: "dimensions:min_width=" . app()->collection->min_width . ",min_height=" . app()->collection->min_height . ',max_width=' . app()->collection->max_width . ',max_height=' . app()->collection->max_height,
"max:" . app()->collection->max_file_size,
"min:" . app()->collection->min_file_size,
];
$validated = $request->validate($fieldsValidate);
if (is_null($request->file('file'))) {
$storedImage = new \Illuminate\Http\File(Storage::disk('local')->path(app()->file->server_path));
$validated = Validator::make(['file' => $storedImage], ["file" => [
"mimes:" . app()->collection->getExts(),
"mimetypes:" . app()->collection->getMimeTypes(),
"dimensions:min_width=" . app()->collection->min_width . ",min_height=" . app()->collection->min_height . ',max_width=' . app()->collection->max_width . ',max_height=' . app()->collection->max_height,
"max:" . app()->collection->max_file_size,
"min:" . app()->collection->min_file_size
]])->validate();
}
if (!is_null(app()->collection->process)) {
$imageProcessor->process($request->file->path(), $request->file->path(), app()->collection->process);
}
DB::transaction(function () use ($request) {
$uuid = app()->uuid;
$request->resourceFile = 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') . '/' . $uuid . '.' . app()->collection->ext,
]);
if (!app()->collection->tmp_support && app()->collection->count == 1) {
File::where('user_id', auth()->id())->delete();
}
$storedImage = $request->file->storeAs($request->resourceFile->server_path, app()->uuid . '.' . app()->collection->ext, app()->collection->disk);
if (app()->collection->public) {
Storage::setVisibility($storedImage, 'public');
}
});
return new FileResource($request->resourceFile);
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}