Files
lucent-laravel/src/Http/Controller/FileController.php
T

81 lines
2.4 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Lucent\Channel\ChannelService;
2024-08-19 17:48:10 +03:00
use Lucent\File\FileService;
2024-09-13 17:16:04 +03:00
use Lucent\File\ImageService;
2023-10-02 23:10:49 +03:00
use Lucent\Query\Query;
2024-08-19 17:48:10 +03:00
use Lucent\Record\InputData\RecordInputData;
2023-10-02 23:10:49 +03:00
use Lucent\Record\RecordService;
2024-08-19 17:48:10 +03:00
use Lucent\Record\Status;
2023-10-02 23:10:49 +03:00
use function Lucent\Response\fail;
use function Lucent\Response\ok;
class FileController extends Controller
{
public function __construct(
private readonly ChannelService $channelService,
private readonly RecordService $recordService,
2024-08-19 17:48:10 +03:00
private readonly FileService $fileService,
2024-09-13 17:16:04 +03:00
private readonly ImageService $imageService,
2023-10-02 23:10:49 +03:00
private readonly Query $query
)
{
}
2024-09-13 17:16:04 +03:00
public function template(Request $request)
{
$template = $request->segment(3);
$templatePath = $request->route("any");
$filePath = str_replace($template."/", "", $templatePath);
$record = $this->query->filter(["_file.path" => $filePath])->run()->records->first();
2024-09-19 23:36:43 +03:00
$schema = $this->channelService->getSchema($record->schema);
2024-09-13 17:16:04 +03:00
$this->imageService->file($record,$template);
2024-09-19 23:36:43 +03:00
$disk = $this->fileService->loadDisk($schema->disk);
2024-09-13 17:16:04 +03:00
return response()->file($disk->path("templates/".$templatePath));
}
2023-10-02 23:10:49 +03:00
public function download(Request $request)
{
2024-08-19 17:48:10 +03:00
$disk = $this->fileService->loadDisk();
2023-10-02 23:10:49 +03:00
return $disk->download($request->input("path"));
}
public function upload(Request $request)
{
2023-10-17 18:30:41 +03:00
$validator = Validator::make($request->all(), [
2023-10-02 23:10:49 +03:00
'files.*' => 'required|file|max:100000',
]);
if ($validator->fails()) {
return fail($validator->errors()->first());
}
$schema = $this->channelService->channel->schemas->firstWhere("name", $request->input("schema"));
2023-10-17 18:30:41 +03:00
$files = $request->file('files');
2023-10-02 23:10:49 +03:00
2024-08-19 17:59:08 +03:00
foreach ($files as $file) {
$this->recordService->createFromUploadedFile($file, new RecordInputData(
schemaName: $schema->name,
status: Status::PUBLISHED,
), []);
}
2023-10-02 23:10:49 +03:00
2023-10-04 13:32:30 +03:00
$graph = $this->query
2023-10-02 23:10:49 +03:00
->filter([
2023-10-04 13:32:30 +03:00
"schema" => $schema->name
2023-10-02 23:10:49 +03:00
])
->limit(15)
->skip(0)
->sort("-_sys.updatedAt")
->run();
return ok($graph->records->toArray());
}
}