81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Lucent\Http\Controller;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Lucent\Channel\ChannelService;
|
|
use Lucent\File\FileService;
|
|
use Lucent\File\ImageService;
|
|
use Lucent\Query\Query;
|
|
use Lucent\Record\InputData\RecordInputData;
|
|
use Lucent\Record\RecordService;
|
|
use Lucent\Record\Status;
|
|
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,
|
|
private readonly FileService $fileService,
|
|
private readonly ImageService $imageService,
|
|
private readonly Query $query
|
|
)
|
|
{
|
|
}
|
|
|
|
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();
|
|
$schema = $this->channelService->getSchema($record->schema);
|
|
$this->imageService->file($record,$template);
|
|
$disk = $this->fileService->loadDisk($schema->disk);
|
|
return response()->file($disk->path("templates/".$templatePath));
|
|
}
|
|
|
|
public function download(Request $request)
|
|
{
|
|
$disk = $this->fileService->loadDisk();
|
|
return $disk->download($request->input("path"));
|
|
}
|
|
|
|
|
|
public function upload(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'files.*' => 'required|file|max:100000',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return fail($validator->errors()->first());
|
|
}
|
|
$schema = $this->channelService->channel->schemas->firstWhere("name", $request->input("schema"));
|
|
$files = $request->file('files');
|
|
|
|
foreach ($files as $file) {
|
|
$this->recordService->createFromUploadedFile($file, new RecordInputData(
|
|
schemaName: $schema->name,
|
|
status: Status::PUBLISHED,
|
|
), []);
|
|
}
|
|
|
|
$graph = $this->query
|
|
->filter([
|
|
"schema" => $schema->name
|
|
])
|
|
->limit(15)
|
|
->skip(0)
|
|
->sort("-_sys.updatedAt")
|
|
->run();
|
|
|
|
return ok($graph->records->toArray());
|
|
}
|
|
}
|