records and edgs
This commit is contained in:
+134
-37
@@ -2,13 +2,20 @@
|
||||
|
||||
namespace Lucent\File;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Record\File;
|
||||
use Lucent\Record\FileData as RecordFile;
|
||||
use Lucent\Record\QueryRecord;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
use Lucent\Schema\Schema;
|
||||
use Lucent\Schema\Type;
|
||||
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||
|
||||
class FileService
|
||||
{
|
||||
@@ -21,43 +28,11 @@ class FileService
|
||||
|
||||
public function getPath(QueryRecord $file): string
|
||||
{
|
||||
return $this->channelService->channel->url. "/storage/".$file->_file->path;
|
||||
return $this->channelService->channel->url . "/storage/" . $file->_file->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
public static function create(Schema $schema, string $uploadFromUrl, array $file): FileUploadResult
|
||||
public function createFromUrl(FilesSchema $schema, string $url): FileUploadResult
|
||||
{
|
||||
$emptyUploadUrl = empty($uploadFromUrl);
|
||||
$emptyFileData = empty($file);
|
||||
|
||||
if ($schema->type === Type::FILES && $emptyUploadUrl && $emptyFileData) {
|
||||
throw new LucentException("No file data submitted");
|
||||
} elseif ($schema->type !== Type::FILES && !($emptyUploadUrl && $emptyFileData)) {
|
||||
throw new LucentException("You can't upload a file to a regular record");
|
||||
} elseif ($schema->type !== Type::FILES) {
|
||||
return new FileUploadResult(
|
||||
recordFile: null, duplicateId: "", isDuplicate: false
|
||||
);
|
||||
}
|
||||
|
||||
if (!$emptyUploadUrl) {
|
||||
$file = self::uploadFileFromUrl($uploadFromUrl);
|
||||
return uploadFile($schema, $file);
|
||||
}
|
||||
|
||||
return new FileUploadResult(
|
||||
recordFile: File::fromArray($file), duplicateId: "", isDuplicate: false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
private static function uploadFileFromUrl(string $url): UploadedFile
|
||||
{
|
||||
|
||||
$pathinfo = pathinfo($url);
|
||||
$contents = file_get_contents($url);
|
||||
if ($contents === false) {
|
||||
@@ -65,6 +40,128 @@ class FileService
|
||||
}
|
||||
$file = '/tmp/' . $pathinfo['basename'];
|
||||
file_put_contents($file, $contents);
|
||||
return new UploadedFile($file, $pathinfo['basename']);
|
||||
$uploadedFile = new UploadedFile($file, $pathinfo['basename']);
|
||||
return $this->upload($schema, $uploadedFile);
|
||||
}
|
||||
|
||||
public function upload(FilesSchema $schema, UploadedFile $file): FileUploadResult
|
||||
{
|
||||
$originalName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
|
||||
$extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
|
||||
$originalFilename = $file->getClientOriginalName();
|
||||
$filename = $this->createFileName($originalName, $extension);
|
||||
$mimetype = $file->getMimeType();
|
||||
|
||||
$optimizerChain = OptimizerChainFactory::create();
|
||||
$optimizerChain->setTimeout(30)->optimize($file->getPathName());
|
||||
|
||||
$checksum = sha1_file($file);
|
||||
$recordId = $this->checkDuplicate($schema->name, $checksum, $file->getSize());
|
||||
|
||||
if (!empty($recordId)) {
|
||||
return new FileUploadResult(
|
||||
recordFile: null,
|
||||
duplicateId: $recordId,
|
||||
isDuplicate: true
|
||||
);
|
||||
}
|
||||
|
||||
$disk = $this->loadDisk();
|
||||
$path = $schema->path . "/" . $filename;
|
||||
$res = $disk->put(
|
||||
$path,
|
||||
file_get_contents($file),
|
||||
// 'public' // now managed by aws policy
|
||||
);
|
||||
|
||||
if ($res === false) {
|
||||
throw new LucentException("File $filename not uploaded");
|
||||
}
|
||||
|
||||
$this->createThumbnail($disk, $schema->path, $filename, $file);
|
||||
|
||||
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
|
||||
$recordFile = new RecordFile(
|
||||
originalName: $originalFilename,
|
||||
mime: $mimetype,
|
||||
path: $path,
|
||||
size: $file->getSize(),
|
||||
width: $width,
|
||||
height: $height,
|
||||
checksum: $checksum
|
||||
);
|
||||
|
||||
return new FileUploadResult(
|
||||
recordFile: $recordFile,
|
||||
duplicateId: "",
|
||||
isDuplicate: false
|
||||
);
|
||||
}
|
||||
|
||||
private function createFileName(string $originalName, string $extension): string
|
||||
{
|
||||
return Str::slug($originalName, '-') . '-' . uniqid() . '.' . $extension;
|
||||
}
|
||||
|
||||
private function isImage(string $mimetype): bool
|
||||
{
|
||||
$imageMimes = ['image/webp', 'image/gif', 'image/jpeg', 'image/png', 'image/tiff'];
|
||||
return in_array($mimetype, $imageMimes);
|
||||
}
|
||||
|
||||
public function loadDisk(): Filesystem
|
||||
{
|
||||
return Storage::build([
|
||||
'driver' => 'local',
|
||||
// 'key' => config("filesystems.disks.s3.key"),
|
||||
// 'secret' => config("filesystems.disks.s3.secret"),
|
||||
// 'region' => config("filesystems.disks.s3.region"),
|
||||
// 'bucket' => config("filesystems.disks.s3.bucket"),
|
||||
// // 'url' => $schema->objectStorageUrl,
|
||||
// 'endpoint' => $schema->objectStorageEndpoint,
|
||||
'use_path_style_endpoint' => false,
|
||||
'visibility' => 'public', // now managed by aws policy
|
||||
'root' => storage_path('app/public'),
|
||||
'throw' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
private function checkDuplicate(string $schemaName, string $checksum, int $filesize): string
|
||||
{
|
||||
|
||||
$record = DB::table("records")
|
||||
->where("schema", $schemaName)
|
||||
->where("_file->checksum", $checksum)
|
||||
->where("_file->size", $filesize)
|
||||
->first();
|
||||
|
||||
return $record->id ?? "";
|
||||
}
|
||||
|
||||
private function createThumbnail(Filesystem $disk, string $schemaPath, string $filename, UploadedFile $file): void
|
||||
{
|
||||
|
||||
|
||||
$thumbDir = storage_path("app/public/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;
|
||||
}
|
||||
|
||||
$image->fit(300, 300);
|
||||
try {
|
||||
$image->encode('webp', 75)->save($thumbDir . $filename);
|
||||
} catch (Exception $e) {
|
||||
logger($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
|
||||
namespace Lucent\File;
|
||||
|
||||
use Lucent\Record\File;
|
||||
use Lucent\Record\FileData;
|
||||
|
||||
class FileUploadResult
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public ?File $recordFile,
|
||||
public string $duplicateId,
|
||||
public bool $isDuplicate,
|
||||
public ?FileData $recordFile,
|
||||
public string $duplicateId,
|
||||
public bool $isDuplicate,
|
||||
)
|
||||
{
|
||||
|
||||
|
||||
@@ -1,136 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\File;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Record\File as RecordFile;
|
||||
use Lucent\Schema\Schema;
|
||||
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
function uploadFile(Schema $schema, UploadedFile $file): FileUploadResult
|
||||
{
|
||||
$originalName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
|
||||
$extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
|
||||
$originalFilename = $file->getClientOriginalName();
|
||||
$filename = createFileName($originalName, $extension);
|
||||
$mimetype = $file->getMimeType();
|
||||
|
||||
$optimizerChain = OptimizerChainFactory::create();
|
||||
$optimizerChain->setTimeout(30)->optimize($file->getPathName());
|
||||
|
||||
$checksum = sha1_file($file);
|
||||
$recordId = checkDuplicate($schema->name, $checksum, $file->getSize());
|
||||
if (!empty($recordId)) {
|
||||
return new FileUploadResult(
|
||||
recordFile: null,
|
||||
duplicateId: $recordId,
|
||||
isDuplicate: true
|
||||
);
|
||||
}
|
||||
|
||||
$disk = loadDisk();
|
||||
$path = $schema->path . "/" . $filename;
|
||||
$res = $disk->put(
|
||||
$path,
|
||||
file_get_contents($file),
|
||||
// 'public' // now managed by aws policy
|
||||
);
|
||||
|
||||
if ($res === false) {
|
||||
throw new LucentException("File $filename not uploaded");
|
||||
}
|
||||
|
||||
createThumbnail($disk, $schema->path, $filename, $file);
|
||||
|
||||
list($width, $height) = isImage($mimetype) ? getimagesize($file) : [0, 0];
|
||||
$recordFile = new RecordFile(
|
||||
originalName: $originalFilename,
|
||||
mime: $mimetype,
|
||||
path: $path,
|
||||
size: $file->getSize(),
|
||||
width: $width,
|
||||
height: $height,
|
||||
checksum: $checksum
|
||||
);
|
||||
|
||||
return new FileUploadResult(
|
||||
recordFile: $recordFile,
|
||||
duplicateId: "",
|
||||
isDuplicate: false
|
||||
);
|
||||
}
|
||||
|
||||
function createFileName(string $originalName, string $extension): string
|
||||
{
|
||||
return Str::slug($originalName, '-') . '-' . uniqid() . '.' . $extension;
|
||||
}
|
||||
|
||||
function isImage(string $mimetype): bool
|
||||
{
|
||||
$imageMimes = ['image/webp', 'image/gif', 'image/jpeg', 'image/png', 'image/tiff'];
|
||||
return in_array($mimetype, $imageMimes);
|
||||
}
|
||||
|
||||
function loadDisk(): Filesystem
|
||||
{
|
||||
return Storage::build([
|
||||
'driver' => 'local',
|
||||
// 'key' => config("filesystems.disks.s3.key"),
|
||||
// 'secret' => config("filesystems.disks.s3.secret"),
|
||||
// 'region' => config("filesystems.disks.s3.region"),
|
||||
// 'bucket' => config("filesystems.disks.s3.bucket"),
|
||||
// // 'url' => $schema->objectStorageUrl,
|
||||
// 'endpoint' => $schema->objectStorageEndpoint,
|
||||
'use_path_style_endpoint' => false,
|
||||
'visibility' => 'public', // now managed by aws policy
|
||||
'root' => storage_path('app/public'),
|
||||
'throw' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
function checkDuplicate(string $schemaName, string $checksum, int $filesize): string
|
||||
{
|
||||
|
||||
$record = DB::table("records")
|
||||
->where("schema", $schemaName)
|
||||
->where("_file->checksum", $checksum)
|
||||
->where("_file->size", $filesize)
|
||||
->first();
|
||||
|
||||
return $record->id ?? "";
|
||||
}
|
||||
|
||||
function createThumbnail(Filesystem $disk, string $schemaPath, string $filename, UploadedFile $file): void
|
||||
{
|
||||
|
||||
|
||||
$thumbDir = storage_path("app/public/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;
|
||||
}
|
||||
|
||||
$image->fit(300, 300);
|
||||
try {
|
||||
$image->encode('webp', 75)->save($thumbDir .$filename);
|
||||
} catch (Exception $e) {
|
||||
logger($e->getMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user