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;
|
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,
|
2023-10-02 23:10:49 +03:00
|
|
|
private readonly Query $query
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|