storage and image model
This commit is contained in:
@@ -4,12 +4,14 @@ namespace Lucent\Channel;
|
||||
|
||||
use Lucent\Channel\Data\UserCommand;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
use Lucent\Schema\Schema;
|
||||
|
||||
final class Channel
|
||||
{
|
||||
public string $lucentUrl;
|
||||
public string $filesUrl;
|
||||
public array $disks;
|
||||
public string $previewTargetUrl;
|
||||
|
||||
/**
|
||||
@@ -28,6 +30,7 @@ final class Channel
|
||||
{
|
||||
$this->lucentUrl = $url . "/lucent";
|
||||
$this->filesUrl = $this->makeFilesUrl();
|
||||
$this->disks = $this->getDisksFromSchemas();
|
||||
$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
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace Lucent\File;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Log\Logger;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\LucentException;
|
||||
@@ -21,7 +21,9 @@ class FileService
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public ChannelService $channelService
|
||||
public ChannelService $channelService,
|
||||
public ImageManager $imageManager,
|
||||
public Logger $logger
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -65,7 +67,6 @@ class FileService
|
||||
isDuplicate: true
|
||||
);
|
||||
}
|
||||
|
||||
$disk = $this->loadDisk($schema);
|
||||
$path = $schema->path . "/" . $filename;
|
||||
$res = $disk->put(
|
||||
@@ -78,7 +79,8 @@ class FileService
|
||||
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];
|
||||
$recordFile = new RecordFile(
|
||||
@@ -128,29 +130,19 @@ class FileService
|
||||
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 {
|
||||
ImageManagerStatic::configure(['driver' => 'imagick']);
|
||||
$image = ImageManagerStatic::make($file);
|
||||
} catch (Exception $e) {
|
||||
logger($e->getMessage());
|
||||
return;
|
||||
$originalImage = $this->imageManager->make($disk->get($path));
|
||||
foreach (config("lucent.imageFilters") as $preset => $filterClass) {
|
||||
$image = $originalImage->filter(new $filterClass);
|
||||
$templateUri = "/templates/" . $preset . "/" . $path;
|
||||
$disk->put($templateUri, $image->encode('webp', 75));
|
||||
}
|
||||
|
||||
$image->fit(300, 300);
|
||||
try {
|
||||
$disk->put($thumbDir . $filename, $image->encode('webp', 75));
|
||||
// $image->encode('webp', 75)->save($thumbDir . $filename);
|
||||
} catch (Exception $e) {
|
||||
logger($e->getMessage());
|
||||
}
|
||||
$thumbDir = "thumbs/" . $path;
|
||||
|
||||
$image = $originalImage->fit(300, 300);
|
||||
$disk->put($thumbDir, $image->encode('webp', 75));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,25 +23,13 @@ class FileController extends Controller
|
||||
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 original(Request $request, string $disk)
|
||||
public function fromDisk(Request $request, string $disk)
|
||||
{
|
||||
$imagePath = $request->route("any");
|
||||
$disk = $this->fileService->loadDisk($disk);
|
||||
|
||||
+2
-3
@@ -14,9 +14,8 @@ use Lucent\Http\Controller\SetupController;
|
||||
|
||||
|
||||
Route::get('/lucent/setup', [SetupController::class, 'setup']);
|
||||
Route::get('/storage/templates/{any}', [FileController::class, 'template'])->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::get('/lfs-{disk}/{any}', [FileController::class, 'fromDisk'])->where('any', '.*');
|
||||
|
||||
|
||||
Route::group([
|
||||
'middleware' => ['web'],
|
||||
|
||||
@@ -83,7 +83,6 @@ class LucentServiceProvider extends ServiceProvider
|
||||
}
|
||||
|
||||
View::share('manifest', $manifest);
|
||||
View::share('image', app()->make(ImageService::class));
|
||||
View::share('file', app()->make(FileService::class));
|
||||
Blade::anonymousComponentPath(__DIR__ . '../front/views/components', "lucent");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user