139 lines
4.3 KiB
PHP
139 lines
4.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Lucent\Http\Controller;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Validator;
|
||
|
|
use Intervention\Image\ImageManager;
|
||
|
|
use Lucent\Channel\ChannelRepo;
|
||
|
|
use Lucent\Channel\ChannelService;
|
||
|
|
use Lucent\File\FileUploadResult;
|
||
|
|
use Lucent\Query\Query;
|
||
|
|
use Lucent\Record\RecordService;
|
||
|
|
use function Lucent\File\loadDisk;
|
||
|
|
use function Lucent\File\uploadFile;
|
||
|
|
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 Query $query
|
||
|
|
)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
public function download(Request $request)
|
||
|
|
{
|
||
|
|
$disk = loadDisk();
|
||
|
|
return $disk->download($request->input("path"));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function get(Request $request)
|
||
|
|
{
|
||
|
|
$manager = new ImageManager(['driver' => 'imagick']);
|
||
|
|
$filesystem = loadDisk();
|
||
|
|
$path = $request->route("path");
|
||
|
|
|
||
|
|
if ($filesystem->exists($path)) {
|
||
|
|
$image = $manager->make($filesystem->get($path));
|
||
|
|
return $image->response();
|
||
|
|
}
|
||
|
|
$arr = explode(".", $path);
|
||
|
|
$ext = end($arr);
|
||
|
|
$pathWithoutExtension = str_replace("." . $ext, "", $path);
|
||
|
|
|
||
|
|
$pathArguments = (function ($path) {
|
||
|
|
$pathWithArgumentsAr = explode("-", $path);
|
||
|
|
return collect($pathWithArgumentsAr)
|
||
|
|
->filter(fn($ar) => str_contains($ar, "_"))
|
||
|
|
->reduce(function ($carry, $arg) {
|
||
|
|
[$k, $v] = explode("_", $arg);
|
||
|
|
$carry[$k] = $v;
|
||
|
|
return $carry;
|
||
|
|
});
|
||
|
|
|
||
|
|
})($pathWithoutExtension);
|
||
|
|
|
||
|
|
$originalPath = (function ($path) use ($ext) {
|
||
|
|
$arr = explode("-o-", $path);
|
||
|
|
return $arr[0] . "." . $ext;
|
||
|
|
})($path);
|
||
|
|
$image = $manager->make($filesystem->get($originalPath));
|
||
|
|
if (empty($pathArguments["mode"])) {
|
||
|
|
|
||
|
|
if (empty($pathArguments["w"])) {
|
||
|
|
$image->resize(null, $pathArguments["h"], function ($constraint) {
|
||
|
|
$constraint->aspectRatio();
|
||
|
|
});
|
||
|
|
} elseif (empty($pathArguments["h"])) {
|
||
|
|
$image->resize($pathArguments["w"], null, function ($constraint) {
|
||
|
|
$constraint->aspectRatio();
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
$image->resize($pathArguments["w"], $pathArguments["h"]);
|
||
|
|
}
|
||
|
|
} else if ($pathArguments["mode"] === "fit") {
|
||
|
|
$image->fit($pathArguments["w"], $pathArguments["h"]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$disk = loadDisk();
|
||
|
|
// $disk->put("cache/" . $path, $image);
|
||
|
|
$image->save(storage_path("app/public/cache/" . $path));
|
||
|
|
return $image->response();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
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');
|
||
|
|
|
||
|
|
|
||
|
|
$uploadResults = collect($files)->map(fn($file) => uploadFile($schema, $file))->toArray();
|
||
|
|
collect($uploadResults)
|
||
|
|
->filter(fn(FileUploadResult $res) => !$res->isDuplicate)
|
||
|
|
->values()
|
||
|
|
->map(function (FileUploadResult $uploadResult) use ($schema) {
|
||
|
|
|
||
|
|
return $this->recordService->create(
|
||
|
|
schemaName: $schema->name,
|
||
|
|
data: [],
|
||
|
|
file: (array)$uploadResult->recordFile,
|
||
|
|
edges: [],
|
||
|
|
status: "published",
|
||
|
|
uploadFromUrl: ""
|
||
|
|
);
|
||
|
|
|
||
|
|
})->toArray();
|
||
|
|
|
||
|
|
|
||
|
|
$queryResult = $this->query
|
||
|
|
->filter([
|
||
|
|
"_sys.schema" => $schema->name
|
||
|
|
])
|
||
|
|
->limit(15)
|
||
|
|
->skip(0)
|
||
|
|
->sort("-_sys.updatedAt")
|
||
|
|
->childrenDepth(0)
|
||
|
|
->parentsDepth(0)
|
||
|
|
->run();
|
||
|
|
|
||
|
|
$graph = $queryResult->getQueryRecords();
|
||
|
|
return ok($graph->records->toArray());
|
||
|
|
}
|
||
|
|
}
|