file uploads
This commit is contained in:
@@ -4,7 +4,6 @@ namespace Lucent\Channel;
|
||||
|
||||
use Lucent\Channel\Data\UserCommand;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
use Lucent\Schema\Schema;
|
||||
|
||||
final class Channel
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
|
||||
class GenerateFileSchema extends Command
|
||||
{
|
||||
|
||||
protected $signature = 'lucent:generate:file {name}';
|
||||
|
||||
protected $description = 'Generate a lucent file schema';
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$schema = new FilesSchema(
|
||||
name: $name,
|
||||
label: $name,
|
||||
fields: Collection::make(),
|
||||
disk: "lucent",
|
||||
path: $name,
|
||||
groups: []
|
||||
);
|
||||
|
||||
$json = json_encode($schema, JSON_PRETTY_PRINT);
|
||||
$configDir = base_path(config('lucent.schemas_path'));
|
||||
$schemaPath = $configDir . "/" . $name . '.json';
|
||||
|
||||
if (file_exists($schemaPath)) {
|
||||
$this->error("The schema file already exists.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
file_put_contents($schemaPath, $json);
|
||||
$this->info("The schema file has been created.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\File\FileRepo;
|
||||
use Lucent\File\FileService;
|
||||
use Lucent\Query\Query;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
@@ -26,35 +27,16 @@ class RebuildThumbnails extends Command
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle(ChannelService $channelService): int
|
||||
public function handle(): void
|
||||
{
|
||||
$channelService->channel->schemas
|
||||
->filter(
|
||||
fn(Schema $schema) => get_class($schema) === FilesSchema::class,
|
||||
)
|
||||
->map([$this, "rebuildThumbnails"]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function rebuildThumbnails(FilesSchema $schema): void
|
||||
{
|
||||
$this->info("Rebuilding thumbnails for " . $schema->name);
|
||||
$records = $this->query
|
||||
->limit(0)
|
||||
->filter(["schema" => $schema->name])
|
||||
->run()->records;
|
||||
$disk = $this->fileService->loadDisk($schema->disk);
|
||||
foreach ($records as $record) {
|
||||
$this->info("Rebuilding thumbnails ");
|
||||
$files = FileRepo::query()->get();
|
||||
$disk = $this->fileService->loadDisk();
|
||||
foreach ($files as $file) {
|
||||
try {
|
||||
$this->fileService->createTemplates(
|
||||
$disk,
|
||||
$record->_file->path,
|
||||
);
|
||||
$this->fileService->createTemplates($disk, $file->path);
|
||||
} catch (Exception $e) {
|
||||
echo "File " .
|
||||
$record->_file->originalName .
|
||||
" could not be rebuilt \n";
|
||||
echo "File " . $file->filename . " could not be rebuilt \n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,19 +71,23 @@ class SetupDatabase extends Command
|
||||
) {
|
||||
$table->uuid("id")->primary();
|
||||
$table->string("schema");
|
||||
$table->integer("version");
|
||||
$table->string("status");
|
||||
$table->jsonb("data");
|
||||
$table->jsonb("_sys");
|
||||
$table->timestampTz("createdAt");
|
||||
$table->timestampTz("updatedAt");
|
||||
$table->string("createdBy");
|
||||
$table->string("updatedBy");
|
||||
$table->text("search")->default("");
|
||||
// $table->index(["schema", "_sys->updatedAt", "status"]);
|
||||
$table->index(["schema", "updatedAt", "status"]);
|
||||
$table->index("search");
|
||||
});
|
||||
|
||||
DB::statement(
|
||||
"CREATE INDEX ON " .
|
||||
$this->prefix .
|
||||
'records (schema, ((_sys->>\'updatedAt\')), status)',
|
||||
);
|
||||
// DB::statement(
|
||||
// "CREATE INDEX ON " .
|
||||
// $this->prefix .
|
||||
// 'records (schema, ((_sys->>\'updatedAt\')), status)',
|
||||
// );
|
||||
}
|
||||
|
||||
if (!$schema->hasTable($this->prefix . "edges")) {
|
||||
@@ -140,8 +144,11 @@ class SetupDatabase extends Command
|
||||
$table->uuid("recordId");
|
||||
$table->string("schema");
|
||||
$table->jsonb("data");
|
||||
$table->jsonb("_sys");
|
||||
$table->jsonb("_file");
|
||||
$table->integer("version");
|
||||
$table->timestampTz("createdAt");
|
||||
$table->timestampTz("updatedAt");
|
||||
$table->string("createdBy");
|
||||
$table->string("updatedBy");
|
||||
$table->jsonb("_edges");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Lucent\Database\Database;
|
||||
|
||||
class UpgradeFiles122 extends Command
|
||||
{
|
||||
|
||||
protected $signature = 'lucent:upgrade:files_1_2_2 {schema} {disk}';
|
||||
protected $description = 'Upgrade to the new filesystem';
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$schema = $this->argument('schema');
|
||||
$disk = $this->argument('disk');
|
||||
$db = Database::make();
|
||||
$records = $db->table("lucent_records")->where("schema", $schema)->get();
|
||||
foreach ($records as $record) {
|
||||
$array = json_decode($record->_file, true);
|
||||
$array["disk"] = $disk;
|
||||
$db->table("lucent_records")->where("id", $record->id)->update(["_file" => json_encode($array)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Lucent\File;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Lucent\Data\File as DataFile;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Data\File;
|
||||
@@ -15,6 +16,11 @@ class FileRepo
|
||||
Database::make()->table("lucent_files")->insert($file->toDB());
|
||||
}
|
||||
|
||||
public static function query(): Builder
|
||||
{
|
||||
return Database::make()->table("lucent_files");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return File[]
|
||||
*/
|
||||
|
||||
@@ -14,7 +14,6 @@ use Lucent\Id\Id;
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Data\File as DataFile;
|
||||
use Lucent\Record\QueryRecord;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||
|
||||
class FileService
|
||||
@@ -25,15 +24,8 @@ class FileService
|
||||
public Logger $logger,
|
||||
) {}
|
||||
|
||||
public function getPath(QueryRecord $file): string
|
||||
{
|
||||
return $this->channelService->channel->filesUrl .
|
||||
"/" .
|
||||
$file->_file->path;
|
||||
}
|
||||
|
||||
public function createFromUrl(
|
||||
FilesSchema $schema,
|
||||
string $recordId,
|
||||
string $url,
|
||||
): FileUploadResult {
|
||||
$pathinfo = pathinfo($url);
|
||||
@@ -44,7 +36,7 @@ class FileService
|
||||
$file = "/tmp/" . $pathinfo["basename"];
|
||||
file_put_contents($file, $contents);
|
||||
$uploadedFile = new UploadedFile($file, $pathinfo["basename"]);
|
||||
return $this->upload($schema, $uploadedFile);
|
||||
return $this->upload($recordId, $uploadedFile);
|
||||
}
|
||||
|
||||
public function upload(string $recordId, UploadedFile $file): DataFile
|
||||
@@ -130,21 +122,6 @@ class FileService
|
||||
return Storage::disk(config("lucent.disk"));
|
||||
}
|
||||
|
||||
private function checkDuplicate(
|
||||
string $schemaName,
|
||||
string $checksum,
|
||||
int $filesize,
|
||||
): string {
|
||||
$record = Database::make()
|
||||
->table("lucent_records")
|
||||
->where("schema", $schemaName)
|
||||
->where("_file->checksum", $checksum)
|
||||
->where("_file->size", $filesize)
|
||||
->first();
|
||||
|
||||
return $record->id ?? "";
|
||||
}
|
||||
|
||||
public function createTemplates(Filesystem $disk, string $path): void
|
||||
{
|
||||
$originalImage = $this->imageManager->make($disk->get($path));
|
||||
|
||||
@@ -58,7 +58,6 @@ class RecordController extends Controller
|
||||
|
||||
$users = $this->accountService->all();
|
||||
$schema = $this->channelService->getSchema($schemaName)->get();
|
||||
|
||||
$urlParams = $request->all();
|
||||
$sort = data_get($urlParams, "sort") ?? $schema->sortBy;
|
||||
$filter = data_get($urlParams, "filter") ?? [];
|
||||
@@ -86,8 +85,8 @@ class RecordController extends Controller
|
||||
->childrenFields($schema?->visible ?? [])
|
||||
->childrenDepth(1)
|
||||
->parentsDepth(0)
|
||||
->runWithCount();
|
||||
|
||||
->runWithCount();
|
||||
$records = $graph->getRootRecords()->toArray();
|
||||
|
||||
$data = [
|
||||
|
||||
@@ -89,8 +89,6 @@ class LucentServiceProvider extends ServiceProvider
|
||||
RemoveOrphanEdges::class,
|
||||
SetupDatabase::class,
|
||||
GenerateCollectionSchema::class,
|
||||
GenerateFileSchema::class,
|
||||
UpgradeFiles122::class,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -78,6 +78,7 @@ final class Query
|
||||
->table("lucent_records")
|
||||
->whereIn("id", $edgesIds)
|
||||
->whereIn("status", $this->options->status)
|
||||
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
@@ -295,7 +296,8 @@ final class Query
|
||||
public function orderByQuery(Builder $query): Builder
|
||||
{
|
||||
foreach ($this->options->sort as $item) {
|
||||
$field = str_replace(".", "->", ltrim($item, "-"));
|
||||
$field = str_replace("_sys.", "", ltrim($item, "-"));
|
||||
$field = str_replace(".", "->", $field);
|
||||
$dir = str_starts_with($item, "-") ? "desc" : "asc";
|
||||
if ($field) {
|
||||
$query->orderBy($field, $dir);
|
||||
|
||||
+18
-14
@@ -2,24 +2,25 @@
|
||||
|
||||
namespace Lucent\Record;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Lucent\LucentException;
|
||||
|
||||
class QueryRecord
|
||||
{
|
||||
|
||||
function __construct(
|
||||
public string $id,
|
||||
public string $schema,
|
||||
public Status $status,
|
||||
public System $_sys,
|
||||
public string $id,
|
||||
public string $schema,
|
||||
public Status $status,
|
||||
public RecordData $data,
|
||||
public bool $isRoot,
|
||||
public ?FileData $_file = null,
|
||||
public array $_children = [],
|
||||
public array $_parents = [],
|
||||
)
|
||||
{
|
||||
}
|
||||
public Carbon $createdAt,
|
||||
public Carbon $updatedAt,
|
||||
public string $createdBy,
|
||||
public string $updatedBy,
|
||||
public int $version,
|
||||
public bool $isRoot,
|
||||
public array $_children = [],
|
||||
public array $_parents = [],
|
||||
) {}
|
||||
|
||||
public static function fromRecord(Record $record): QueryRecord
|
||||
{
|
||||
@@ -27,10 +28,13 @@ class QueryRecord
|
||||
id: $record->id,
|
||||
schema: $record->schema,
|
||||
status: $record->status,
|
||||
_sys: $record->_sys,
|
||||
data: $record->data,
|
||||
createdAt: $record->createdAt,
|
||||
updatedAt: $record->updatedAt,
|
||||
createdBy: $record->createdBy,
|
||||
updatedBy: $record->updatedBy,
|
||||
version: $record->version,
|
||||
isRoot: false,
|
||||
_file: $record->_file,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+41
-37
@@ -2,36 +2,46 @@
|
||||
|
||||
namespace Lucent\Record;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use JsonSerializable;
|
||||
use stdClass;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Record implements JsonSerializable
|
||||
{
|
||||
|
||||
|
||||
function __construct(
|
||||
public string $id,
|
||||
public string $schema,
|
||||
public Status $status,
|
||||
public System $_sys,
|
||||
public string $id,
|
||||
public string $schema,
|
||||
public Status $status,
|
||||
public Carbon $createdAt,
|
||||
public Carbon $updatedAt,
|
||||
public string $createdBy,
|
||||
public string $updatedBy,
|
||||
public int $version,
|
||||
public RecordData $data,
|
||||
public ?FileData $_file = null,
|
||||
)
|
||||
) {}
|
||||
|
||||
private function indexValues(array $arrObject)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
private function indexValues(array $arrObject){
|
||||
|
||||
return trim(Str::lower(collect($arrObject)
|
||||
->map(function($value){
|
||||
if(is_array($value)){
|
||||
return $this->indexValues($value ?? []);
|
||||
}
|
||||
return str_replace(array("\r", "\n"), '', strip_tags((string)$value));
|
||||
})
|
||||
->values()->join(" ")." ". $this->_file?->originalName ?? ""));
|
||||
return trim(
|
||||
Str::lower(
|
||||
collect($arrObject)
|
||||
->map(function ($value) {
|
||||
if (is_array($value)) {
|
||||
return $this->indexValues($value ?? []);
|
||||
}
|
||||
return str_replace(
|
||||
["\r", "\n"],
|
||||
"",
|
||||
strip_tags((string) $value),
|
||||
);
|
||||
})
|
||||
->values()
|
||||
->join(" ") .
|
||||
" " .
|
||||
"",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function toDB(): array
|
||||
@@ -41,8 +51,11 @@ class Record implements JsonSerializable
|
||||
"id" => $this->id,
|
||||
"status" => $this->status->value,
|
||||
"schema" => $this->schema,
|
||||
"_sys" => json_encode($this->_sys),
|
||||
"_file" => json_encode($this->_file),
|
||||
"createdAt" => $this->createdAt->toDateTimeString(),
|
||||
"updatedAt" => $this->updatedAt->toDateTimeString(),
|
||||
"createdBy" => $this->createdBy,
|
||||
"updatedBy" => $this->updatedBy,
|
||||
"version" => $this->version,
|
||||
"data" => json_encode($this->data),
|
||||
"search" => $searchIndex,
|
||||
];
|
||||
@@ -50,30 +63,21 @@ class Record implements JsonSerializable
|
||||
|
||||
public static function fromDB(stdClass $data): Record
|
||||
{
|
||||
|
||||
$file = json_decode($data->_file, true);
|
||||
if (!empty($file)) {
|
||||
|
||||
$file = FileData::fromArray($file);
|
||||
} else {
|
||||
$file = null;
|
||||
}
|
||||
|
||||
return new Record(
|
||||
id: $data->id,
|
||||
schema: $data->schema,
|
||||
status: Status::from($data->status),
|
||||
_sys: System::fromArray(json_decode($data->_sys, true)),
|
||||
createdAt: Carbon::parse($data->createdAt),
|
||||
updatedAt: Carbon::parse($data->updatedAt),
|
||||
createdBy: $data->createdBy,
|
||||
updatedBy: $data->updatedBy,
|
||||
version: $data->version,
|
||||
data: new RecordData(json_decode($data->data, true)),
|
||||
_file: $file,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function jsonSerialize(): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Lucent\Record;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use Lucent\Account\AuthService;
|
||||
@@ -16,7 +17,7 @@ use Lucent\Record\InputData\EdgeInputData;
|
||||
use Lucent\Record\InputData\RecordInputData;
|
||||
use Lucent\Revision\RevisionService;
|
||||
use Lucent\Schema\FieldInterface;
|
||||
use Lucent\Schema\Schema;
|
||||
use Lucent\Data\Schema;
|
||||
use Lucent\Schema\Type;
|
||||
use Lucent\Schema\Validator\Validator;
|
||||
use Lucent\Schema\Validator\ValidatorException;
|
||||
@@ -115,9 +116,12 @@ readonly class RecordService
|
||||
id: $newRecordId,
|
||||
schema: $data->schemaName,
|
||||
status: $data->status,
|
||||
_sys: System::newRecord($this->authService->currentUserId()),
|
||||
version: 1,
|
||||
createdBy: $this->authService->currentUserId(),
|
||||
updatedBy: $this->authService->currentUserId(),
|
||||
createdAt: Carbon::now(),
|
||||
updatedAt: Carbon::now(),
|
||||
data: $formattedData,
|
||||
_file: !empty($file) ? FileData::fromArray(toArray($file)) : null,
|
||||
);
|
||||
|
||||
if ($data->status === Status::PUBLISHED) {
|
||||
@@ -173,9 +177,12 @@ readonly class RecordService
|
||||
id: $record->id,
|
||||
schema: $record->schema,
|
||||
status: $status,
|
||||
_sys: $record->_sys->update($this->authService->currentUserId()),
|
||||
version: $record->version + 1,
|
||||
createdBy: $record->createdBy,
|
||||
updatedBy: $this->authService->currentUserId(),
|
||||
createdAt: $record->createdAt,
|
||||
updatedAt: Carbon::now(),
|
||||
data: $record->data->merge($formattedData),
|
||||
_file: $record->_file,
|
||||
);
|
||||
|
||||
RecordRepo::update($newRecord);
|
||||
@@ -205,12 +212,12 @@ readonly class RecordService
|
||||
$record->schema,
|
||||
new RecordData($data),
|
||||
);
|
||||
|
||||
if ($status === Status::PUBLISHED) {
|
||||
$errors = $this->recordValidator->check(
|
||||
$record->schema,
|
||||
$formattedData,
|
||||
);
|
||||
|
||||
if ($errors->isNotEmpty()) {
|
||||
$this->recordValidator->throwException($errors);
|
||||
}
|
||||
@@ -220,9 +227,12 @@ readonly class RecordService
|
||||
id: $record->id,
|
||||
schema: $record->schema,
|
||||
status: $status,
|
||||
_sys: $record->_sys->update($this->authService->currentUserId()),
|
||||
version: $record->version + 1,
|
||||
createdBy: $record->createdBy,
|
||||
updatedBy: $this->authService->currentUserId(),
|
||||
createdAt: $record->createdAt,
|
||||
updatedAt: Carbon::now(),
|
||||
data: $record->data->merge($formattedData),
|
||||
_file: $record->_file,
|
||||
);
|
||||
|
||||
RecordRepo::update($newRecord);
|
||||
@@ -278,7 +288,6 @@ readonly class RecordService
|
||||
data: $record->data->toArray(),
|
||||
status: Status::DRAFT,
|
||||
),
|
||||
file: $record->_file,
|
||||
edges: $newEdgesData,
|
||||
);
|
||||
}
|
||||
@@ -337,9 +346,12 @@ readonly class RecordService
|
||||
id: Id::new(),
|
||||
schema: $schema->name,
|
||||
status: Status::DRAFT,
|
||||
_sys: System::newRecord($this->authService->currentUserId()),
|
||||
version: 1,
|
||||
createdBy: $this->authService->currentUserId(),
|
||||
updatedBy: $this->authService->currentUserId(),
|
||||
createdAt: Carbon::now(),
|
||||
updatedAt: Carbon::now(),
|
||||
data: $formattedData,
|
||||
_file: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Record;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Lucent\Schema\Schema;
|
||||
|
||||
readonly class System
|
||||
{
|
||||
|
||||
|
||||
function __construct(
|
||||
public int $version,
|
||||
public string $createdBy,
|
||||
public string $updatedBy,
|
||||
public string $createdAt,
|
||||
public string $updatedAt,
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function fromArray(array $data): System
|
||||
{
|
||||
return new System(
|
||||
version: data_get($data, "version"),
|
||||
createdBy: data_get($data, "createdBy"),
|
||||
updatedBy: data_get($data, "updatedBy"),
|
||||
createdAt: data_get($data, "createdAt"),
|
||||
updatedAt: data_get($data, "updatedAt"),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static function newRecord(string $userId): System
|
||||
{
|
||||
$now = Carbon::now()->toJson();
|
||||
return new System(
|
||||
version: 1,
|
||||
createdBy: $userId,
|
||||
updatedBy: $userId,
|
||||
createdAt: $now,
|
||||
updatedAt: $now,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function update(string $userId): System
|
||||
{
|
||||
$now = Carbon::now()->toJson();
|
||||
return new System(
|
||||
version: $this->version + 1,
|
||||
createdBy: $this->createdBy,
|
||||
updatedBy: $userId,
|
||||
createdAt: $this->createdAt,
|
||||
updatedAt: $now,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+17
-21
@@ -2,51 +2,47 @@
|
||||
|
||||
namespace Lucent\Revision;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Record\FileData;
|
||||
use Lucent\Record\Record;
|
||||
use Lucent\Record\RecordData;
|
||||
use Lucent\Record\System;
|
||||
|
||||
readonly class Revision
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $recordId
|
||||
* @param string $schema
|
||||
* @param System $_sys
|
||||
* @param RecordData $data
|
||||
* @param list<Edge> $_edges
|
||||
* @param FileData|null $_file
|
||||
*/
|
||||
function __construct(
|
||||
public string $id,
|
||||
public string $recordId,
|
||||
public string $schema,
|
||||
public System $_sys,
|
||||
public string $id,
|
||||
public string $recordId,
|
||||
public string $schema,
|
||||
public Carbon $createdAt,
|
||||
public Carbon $updatedAt,
|
||||
public string $createdBy,
|
||||
public string $updatedBy,
|
||||
public int $version,
|
||||
public RecordData $data,
|
||||
public array $_edges,
|
||||
public ?FileData $_file = null,
|
||||
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public array $_edges,
|
||||
) {}
|
||||
|
||||
public static function fromRecord(Record $record, array $edges): Revision
|
||||
{
|
||||
return new Revision(
|
||||
id: (string)Str::uuid(),
|
||||
id: (string) Str::uuid(),
|
||||
recordId: $record->id,
|
||||
schema: $record->schema,
|
||||
_sys: $record->_sys,
|
||||
createdAt: $record->createdAt,
|
||||
updatedAt: $record->updatedAt,
|
||||
createdBy: $record->createdBy,
|
||||
updatedBy: $record->updatedBy,
|
||||
version: $record->version,
|
||||
data: $record->data,
|
||||
_edges: $edges,
|
||||
_file: $record->_file
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,15 +5,12 @@ namespace Lucent\Revision;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\FileData;
|
||||
use Lucent\Record\RecordData;
|
||||
use Lucent\Record\System;
|
||||
use PhpOption\Option;
|
||||
use stdClass;
|
||||
|
||||
class RevisionRepo
|
||||
{
|
||||
|
||||
private string $table = "lucent_revisions";
|
||||
|
||||
public function create(Revision $revision): string
|
||||
@@ -23,17 +20,17 @@ class RevisionRepo
|
||||
return $revision->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Collection<Revision>
|
||||
**/
|
||||
public function getByRecordId(string $rid): Collection
|
||||
{
|
||||
$revisions = Database::make()->table($this->table)
|
||||
$revisions = Database::make()
|
||||
->table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->get()
|
||||
->map([$this, 'fromDB'])
|
||||
->sortByDesc("_sys.version")
|
||||
->map([$this, "fromDB"])
|
||||
->sortByDesc("version")
|
||||
->toArray();
|
||||
|
||||
return new Collection($revisions);
|
||||
@@ -41,29 +38,31 @@ class RevisionRepo
|
||||
|
||||
public function cleanupRecord(string $rid, int $numKeep): void
|
||||
{
|
||||
$revisionIds = Database::make()->table($this->table)
|
||||
$revisionIds = Database::make()
|
||||
->table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->orderBy("_sys->version", "desc")
|
||||
->orderBy("version", "desc")
|
||||
->limit(100)
|
||||
->skip($numKeep)
|
||||
->get()
|
||||
->pluck("id");
|
||||
|
||||
Database::make()->table($this->table)
|
||||
Database::make()
|
||||
->table($this->table)
|
||||
->whereIn("id", $revisionIds)
|
||||
->delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Option<Revision>
|
||||
*/
|
||||
public function getByRecordIdAndVersion(string $rid, int $version): Option
|
||||
{
|
||||
|
||||
$res = Database::make()->table($this->table)
|
||||
$res = Database::make()
|
||||
->table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->where('_sys->version', $version)->first();
|
||||
->where("version", $version)
|
||||
->first();
|
||||
|
||||
if (empty($res)) {
|
||||
return none();
|
||||
@@ -78,8 +77,11 @@ class RevisionRepo
|
||||
"id" => $revision->id,
|
||||
"recordId" => $revision->recordId,
|
||||
"schema" => $revision->schema,
|
||||
"_sys" => json_encode($revision->_sys),
|
||||
"_file" => json_encode($revision->_file),
|
||||
"createdAt" => $revision->createdAt->toDateTimeString(),
|
||||
"updatedAt" => $revision->updatedAt->toDateTimeString(),
|
||||
"createdBy" => $revision->createdBy,
|
||||
"updatedBy" => $revision->updatedBy,
|
||||
"version" => $revision->version,
|
||||
"data" => json_encode($revision->data),
|
||||
"_edges" => json_encode($revision->_edges),
|
||||
];
|
||||
@@ -87,24 +89,22 @@ class RevisionRepo
|
||||
|
||||
public function fromDB(stdClass $data): Revision
|
||||
{
|
||||
$file = json_decode($data->_file, true);
|
||||
if (!empty($file)) {
|
||||
|
||||
$file = FileData::fromArray($file);
|
||||
} else {
|
||||
$file = null;
|
||||
}
|
||||
|
||||
$edges = array_map(fn($e) => Edge::fromArray($e), json_decode($data->_edges ?? "[]", true));
|
||||
$edges = array_map(
|
||||
fn($e) => Edge::fromArray($e),
|
||||
json_decode($data->_edges ?? "[]", true),
|
||||
);
|
||||
|
||||
return new Revision(
|
||||
id: $data->id,
|
||||
recordId: $data->recordId,
|
||||
schema: $data->schema,
|
||||
_sys: System::fromArray(json_decode($data->_sys, true)),
|
||||
createdBy: $data->createdBy,
|
||||
updatedBy: $data->updatedBy,
|
||||
createdAt: $data->createdAt,
|
||||
updatedAt: $data->updatedAt,
|
||||
version: $data->version,
|
||||
data: new RecordData(json_decode($data->data, true)),
|
||||
_edges: $edges,
|
||||
_file: $file
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,21 @@ namespace Lucent\Schema;
|
||||
final class Nullable
|
||||
{
|
||||
public function __construct(
|
||||
public bool $nullable,
|
||||
public bool $nullable,
|
||||
public mixed $value,
|
||||
public mixed $default,
|
||||
)
|
||||
{
|
||||
) {}
|
||||
|
||||
public static function make(
|
||||
bool $nullable,
|
||||
mixed $value,
|
||||
mixed $default,
|
||||
): Nullable {
|
||||
return new self($nullable, $value, $default);
|
||||
}
|
||||
|
||||
public function value(): mixed
|
||||
{
|
||||
|
||||
if (!empty($this->value)) {
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,16 @@ class SchemaService
|
||||
|
||||
public function fromArray(array $schemaArr): Schema
|
||||
{
|
||||
$schemaArr["fields"] = [
|
||||
[
|
||||
"ui" => "text",
|
||||
"name" => "name",
|
||||
"label" => "Name",
|
||||
"required" => true,
|
||||
],
|
||||
...$schemaArr["fields"],
|
||||
];
|
||||
|
||||
return new Schema(
|
||||
name: $schemaArr["name"],
|
||||
label: $schemaArr["label"],
|
||||
|
||||
+3
-45
@@ -8,11 +8,8 @@ readonly class System
|
||||
public string $name,
|
||||
public string $label,
|
||||
public string $ui,
|
||||
public bool $files,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public bool $files,
|
||||
) {}
|
||||
|
||||
public static function getById(string $id): System
|
||||
{
|
||||
@@ -26,7 +23,7 @@ readonly class System
|
||||
{
|
||||
return [
|
||||
"_sys.status" => new System(
|
||||
name: "status",
|
||||
name: "_sys.status",
|
||||
label: "Status",
|
||||
ui: "status",
|
||||
files: false,
|
||||
@@ -55,45 +52,6 @@ readonly class System
|
||||
ui: "datetime",
|
||||
files: false,
|
||||
),
|
||||
"_file.path" => new System(
|
||||
name: "_file.path",
|
||||
label: "Filename",
|
||||
ui: "text",
|
||||
files: true,
|
||||
),
|
||||
"_file.mime" => new System(
|
||||
name: "_file.mime",
|
||||
label: "Mime Type",
|
||||
ui: "text",
|
||||
files: true,
|
||||
),
|
||||
|
||||
"_file.width" => new System(
|
||||
name: "_file.width",
|
||||
label: "Dimensions",
|
||||
ui: "text",
|
||||
files: true,
|
||||
),
|
||||
"_file.size" => new System(
|
||||
name: "_file.size",
|
||||
label: "Size",
|
||||
ui: "text",
|
||||
files: true,
|
||||
),
|
||||
"_file.originalName" => new System(
|
||||
name: "_file.originalName",
|
||||
label: "Original Name",
|
||||
ui: "text",
|
||||
files: true,
|
||||
),
|
||||
"_file.checksum" => new System(
|
||||
name: "_file.checksum",
|
||||
label: "Checksum",
|
||||
ui: "text",
|
||||
files: true,
|
||||
),
|
||||
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ class File implements FieldInterface, MinMaxInterface
|
||||
{
|
||||
public FieldInfo $info;
|
||||
|
||||
|
||||
/**
|
||||
* @param string[] $collections
|
||||
*/
|
||||
@@ -20,17 +19,18 @@ class File implements FieldInterface, MinMaxInterface
|
||||
public string $label,
|
||||
public string $mime = "",
|
||||
public string $help = "",
|
||||
public ?int $min = null,
|
||||
public ?int $max = null,
|
||||
public array $collections = [],
|
||||
public ?int $min = null,
|
||||
public ?int $max = null,
|
||||
public array $collections = [],
|
||||
public string $group = "",
|
||||
)
|
||||
{
|
||||
) {
|
||||
$this->info = new FieldInfo("file", "File", FieldType::FILE);
|
||||
}
|
||||
|
||||
public function format(array $input, array $output): array
|
||||
{
|
||||
$value = $input[$this->name] ?? [];
|
||||
$output[$this->name] = $value;
|
||||
return $output;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,4 @@ class File implements FieldInterface, MinMaxInterface
|
||||
|
||||
return count($value) < $this->min;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+15
-12
@@ -16,35 +16,38 @@ class Slug implements FieldInterface, RequiredInterface
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $label,
|
||||
public bool $required = false,
|
||||
public bool $nullable = false,
|
||||
public ?int $min = null,
|
||||
public ?int $max = null,
|
||||
public bool $required = false,
|
||||
public bool $nullable = false,
|
||||
public ?int $min = null,
|
||||
public ?int $max = null,
|
||||
public string $default = "",
|
||||
public string $help = "",
|
||||
public bool $readonly = false,
|
||||
public bool $readonly = false,
|
||||
public string $source = "",
|
||||
public string $group = "",
|
||||
)
|
||||
{
|
||||
) {
|
||||
$this->info = new FieldInfo("slug", "Slug", FieldType::STRING);
|
||||
}
|
||||
|
||||
public function format(array $input, array $output): array
|
||||
{
|
||||
$value = !empty($input[$this->name]) ? (string)$input[$this->name] : null;
|
||||
if(empty($value)){
|
||||
$value = !empty($input[$this->name])
|
||||
? (string) $input[$this->name]
|
||||
: null;
|
||||
if (empty($value)) {
|
||||
$value = Str::slug($input[$this->source]);
|
||||
}
|
||||
|
||||
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
|
||||
$output[$this->name] = new Nullable(
|
||||
$this->nullable,
|
||||
$value,
|
||||
"",
|
||||
)->value();
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
public function failRequired(mixed $value): bool
|
||||
{
|
||||
return empty(trim($value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-18
@@ -3,7 +3,7 @@
|
||||
use PhpOption\None;
|
||||
use PhpOption\Some;
|
||||
|
||||
if (!function_exists('some')) {
|
||||
if (!function_exists("some")) {
|
||||
/**
|
||||
* @template T
|
||||
* @param T $value
|
||||
@@ -15,51 +15,50 @@ if (!function_exists('some')) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('none')) {
|
||||
if (!function_exists("none")) {
|
||||
function none(): None
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('toArray')) {
|
||||
if (!function_exists("toArray")) {
|
||||
function toArray(mixed $data): array
|
||||
{
|
||||
return \json_decode(\json_encode($data), true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('make_dir_r')) {
|
||||
if (!function_exists("make_dir_r")) {
|
||||
function make_dir_r(string $path): void
|
||||
{
|
||||
is_dir($path) || mkdir($path, 0777, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('schemas_path')) {
|
||||
if (!function_exists("schemas_path")) {
|
||||
function schemas_path(): string
|
||||
{
|
||||
return storage_path("lucent/lucent.schemas.json");
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('lucent_file')) {
|
||||
function lucent_file(\Lucent\Record\QueryRecord $record): string
|
||||
if (!function_exists("lucent_file")) {
|
||||
function lucent_file(\Lucent\Data\File $file): string
|
||||
{
|
||||
$path = $record->_file->path;
|
||||
return app()->make(\Lucent\Channel\ChannelService::class)->channel->disks[$record->_file->disk] ."/". $path;
|
||||
$path = $file->path;
|
||||
return app()->make(\Lucent\Channel\ChannelService::class)->channel
|
||||
->filesUrl .
|
||||
"/" .
|
||||
$path;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('lucent_image')) {
|
||||
function lucent_image(\Lucent\Record\QueryRecord $record, string $template): string
|
||||
if (!function_exists("lucent_image")) {
|
||||
function lucent_image(\Lucent\Data\File $file, string $template): string
|
||||
{
|
||||
$path = $record->_file->path;
|
||||
return app()->make(\Lucent\Channel\ChannelService::class)->channel->disks[$record->_file->disk] . "/templates/$template/$path";
|
||||
$path = $file->path;
|
||||
return app()->make(\Lucent\Channel\ChannelService::class)->channel
|
||||
->filesUrl . "/templates/$template/$path";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user