storage and image model

This commit is contained in:
2024-09-27 14:28:20 +03:00
parent 6d15591601
commit 63232585ab
7 changed files with 33 additions and 120 deletions
+3 -4
View File
@@ -3,11 +3,11 @@ export function imgurl(channel,record) {
if (record._file.mime === "image/svg+xml") { if (record._file.mime === "image/svg+xml") {
return fileurl(channel, record); return fileurl(channel, record);
} }
return channel.filesUrl + `/thumbs/${record._file.disk}/${record._file.path}`; return channel.disks[record._file.disk] + `/thumbs/${record._file.path}`;
} }
export function fileurl(channel, record) { export function fileurl(channel, record) {
return channel.filesUrl + `/${record._file.disk}/${record._file.path}`; return channel.disks[record._file.disk] + `/${record._file.path}`;
} }
export function htmlurl(channel,record, preset) { export function htmlurl(channel,record, preset) {
@@ -18,9 +18,8 @@ export function htmlurl(channel,record, preset) {
if (record._file.width > 0) { if (record._file.width > 0) {
let presetUrl = url; let presetUrl = url;
if (preset) { if (preset) {
presetUrl = channel.filesUrl + `/templates/${preset}/${record._file.disk}/${record._file.path}`; presetUrl = channel.disks[record._file.disk] + `/templates/${preset}/${record._file.path}`;
} }
html = `<img src="${presetUrl}" alt="${record._file.path}" />` html = `<img src="${presetUrl}" alt="${record._file.path}" />`
} else if (record._file.mime === "image/svg+xml") { } else if (record._file.mime === "image/svg+xml") {
html = `<img src="${url}" alt="${record._file.path}"/>` html = `<img src="${url}" alt="${record._file.path}"/>`
+10
View File
@@ -4,12 +4,14 @@ namespace Lucent\Channel;
use Lucent\Channel\Data\UserCommand; use Lucent\Channel\Data\UserCommand;
use Lucent\Primitive\Collection; use Lucent\Primitive\Collection;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema; use Lucent\Schema\Schema;
final class Channel final class Channel
{ {
public string $lucentUrl; public string $lucentUrl;
public string $filesUrl; public string $filesUrl;
public array $disks;
public string $previewTargetUrl; public string $previewTargetUrl;
/** /**
@@ -28,6 +30,7 @@ final class Channel
{ {
$this->lucentUrl = $url . "/lucent"; $this->lucentUrl = $url . "/lucent";
$this->filesUrl = $this->makeFilesUrl(); $this->filesUrl = $this->makeFilesUrl();
$this->disks = $this->getDisksFromSchemas();
$this->previewTargetUrl = $url . "/" . $previewTarget; $this->previewTargetUrl = $url . "/" . $previewTarget;
} }
@@ -42,4 +45,11 @@ final class Channel
} }
private function getDisksFromSchemas()
{
return $this->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->reduce(function (array $carry, Schema $schema) {
$carry[$schema->disk] = config("filesystems.disks." . $schema->disk . ".url");
return $carry;
}, []);
}
} }
+17 -25
View File
@@ -2,12 +2,12 @@
namespace Lucent\File; namespace Lucent\File;
use Exception;
use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Log\Logger;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Intervention\Image\ImageManagerStatic; use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService; use Lucent\Channel\ChannelService;
use Lucent\Database\Database; use Lucent\Database\Database;
use Lucent\LucentException; use Lucent\LucentException;
@@ -21,7 +21,9 @@ class FileService
{ {
public function __construct( public function __construct(
public ChannelService $channelService public ChannelService $channelService,
public ImageManager $imageManager,
public Logger $logger
) )
{ {
} }
@@ -65,7 +67,6 @@ class FileService
isDuplicate: true isDuplicate: true
); );
} }
$disk = $this->loadDisk($schema); $disk = $this->loadDisk($schema);
$path = $schema->path . "/" . $filename; $path = $schema->path . "/" . $filename;
$res = $disk->put( $res = $disk->put(
@@ -78,7 +79,8 @@ class FileService
throw new LucentException("File $filename not uploaded"); throw new LucentException("File $filename not uploaded");
} }
$this->createThumbnail($disk, $schema->path, $filename, $file); // $this->createThumbnail($disk, $schema->path, $filename, $file);
$this->createTemplates($disk, $path, $file);
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0]; list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
$recordFile = new RecordFile( $recordFile = new RecordFile(
@@ -128,29 +130,19 @@ class FileService
return $record->id ?? ""; return $record->id ?? "";
} }
private function createThumbnail(Filesystem $disk, string $schemaPath, string $filename, UploadedFile $file): void private function createTemplates(Filesystem $disk, string $path, UploadedFile $file): void
{ {
$thumbDir = "thumbs/" . $schemaPath . "/";
// if (!file_exists($thumbDir)) {
// make_dir_r($thumbDir);
// }
try { $originalImage = $this->imageManager->make($disk->get($path));
ImageManagerStatic::configure(['driver' => 'imagick']); foreach (config("lucent.imageFilters") as $preset => $filterClass) {
$image = ImageManagerStatic::make($file); $image = $originalImage->filter(new $filterClass);
} catch (Exception $e) { $templateUri = "/templates/" . $preset . "/" . $path;
logger($e->getMessage()); $disk->put($templateUri, $image->encode('webp', 75));
return;
} }
$image->fit(300, 300); $thumbDir = "thumbs/" . $path;
try {
$disk->put($thumbDir . $filename, $image->encode('webp', 75)); $image = $originalImage->fit(300, 300);
// $image->encode('webp', 75)->save($thumbDir . $filename); $disk->put($thumbDir, $image->encode('webp', 75));
} catch (Exception $e) {
logger($e->getMessage());
}
} }
} }
-74
View File
@@ -1,74 +0,0 @@
<?php
namespace Lucent\File;
use Exception;
use Illuminate\Log\Logger;
use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService;
use Lucent\Record\QueryRecord;
class ImageService
{
private string $notFoundImage = "/not-found.jpg";
public function __construct(
public ImageManager $imageManager,
public FileService $fileService,
public ChannelService $channelService,
public Logger $logger
)
{
}
public function file(?QueryRecord $record, string $template = ""): string
{
if (empty($record)) {
return $this->notFoundImage;
}
$originalPath = $record->_file->path;
$templateUri = $this->findTemplate($originalPath, $template);
if ($templateUri === false) {
$templateUri = $this->createTemplate($originalPath, $template);
}
return $this->channelService->channel->filesUrl . "/" . $templateUri;
}
private function findTemplate(string $originalPath, string $template): string|false
{
$templateUri = "templates/" . $template . "/" . $originalPath;
$templateFilePath = public_path("storage/" . $templateUri);
if (file_exists($templateFilePath)) {
return $templateUri;
}
return false;
}
private function createTemplate(string $originalPath, string $template): string
{
try {
$image = $this->imageManager->make( $this->fileService->loadDisk()->get($originalPath));
} catch (Exception $e) {
$this->logger->error($e->getMessage());
return $originalPath;
}
$image = $image->filter(new $this->channelService->channel->imageFilters[$template]);
try {
$templateUri = "/templates/" . $template . "/" . $originalPath;
$this->fileService->loadDisk()->put($templateUri, $image->encode('webp', 75));
} catch (Exception $e) {
$this->logger->error($e->getMessage());
return $this->notFoundImage;
}
return $templateUri;
}
}
+1 -13
View File
@@ -23,25 +23,13 @@ class FileController extends Controller
private readonly ChannelService $channelService, private readonly ChannelService $channelService,
private readonly RecordService $recordService, private readonly RecordService $recordService,
private readonly FileService $fileService, private readonly FileService $fileService,
private readonly ImageService $imageService,
private readonly Query $query 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 original(Request $request, string $disk) public function fromDisk(Request $request, string $disk)
{ {
$imagePath = $request->route("any"); $imagePath = $request->route("any");
$disk = $this->fileService->loadDisk($disk); $disk = $this->fileService->loadDisk($disk);
+2 -3
View File
@@ -14,9 +14,8 @@ use Lucent\Http\Controller\SetupController;
Route::get('/lucent/setup', [SetupController::class, 'setup']); Route::get('/lucent/setup', [SetupController::class, 'setup']);
Route::get('/storage/templates/{any}', [FileController::class, 'template'])->where('any', '.*'); Route::get('/lfs-{disk}/{any}', [FileController::class, 'fromDisk'])->where('any', '.*');
Route::get('/lfs-{disk}/thumbs/{any}', [FileController::class, 'thumb'])->where('any', '.*');
Route::get('/lfs-{disk}/{disk}/{any}', [FileController::class, 'original'])->where('any', '.*');
Route::group([ Route::group([
'middleware' => ['web'], 'middleware' => ['web'],
-1
View File
@@ -83,7 +83,6 @@ class LucentServiceProvider extends ServiceProvider
} }
View::share('manifest', $manifest); View::share('manifest', $manifest);
View::share('image', app()->make(ImageService::class));
View::share('file', app()->make(FileService::class)); View::share('file', app()->make(FileService::class));
Blade::anonymousComponentPath(__DIR__ . '../front/views/components', "lucent"); Blade::anonymousComponentPath(__DIR__ . '../front/views/components', "lucent");