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.
 
 
 
 

98 lines
3.1 KiB

<?php
namespace App\Http\Controllers;
use App\Http\Requests\FileStoreRequest;
use App\Http\Resources\FileResource;
use App\Jobs\FileConversionQueue;
use App\Models\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
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)
{
// skip policy ---and first level----------
$validated = $request->validate([
"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_size,
"min:" . app()->collection->min_size,
],
"alts" => [app()->collection->alt_required ? "required" : "null", 'array'],
"alts.*" => [app()->collection->alt_required ? "required" : "null"],
"description" => [app()->collection->description_required ? "required" : "null"],
]);
DB::beginTransaction();
$urlStorage = '/' . date('y') . '/' . date('m') . app()->getFileUuid;
try {
$file = File::create([
'uuid' => app()->getFileUuid,
'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' => $urlStorage
]);
if (!app()->collection->tmp_support && app()->collection->count == 1) {
File::where('user_id', auth()->id())->delete();
}
$file = $request->file->storeAs($urlStorage, app()->getFileUuid, app()->collection->disk);
if (app()->collection->public) {
Storage::setVisibility($file, 'public');
}
DB::commit();
} catch (\Exception $e) {
DB::rollback();
}
FileConversionQueue::dispatch($file, app()->collection);
return new FileResource($file);
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}