WIP uploading files
This commit is contained in:
+62
-53
@@ -10,46 +10,53 @@ use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Id\Id;
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Record\FileData as RecordFile;
|
||||
use Lucent\Data\File as DataFile;
|
||||
use Lucent\Record\QueryRecord;
|
||||
use Lucent\Schema\FilesSchema;
|
||||
use Lucent\Schema\Schema;
|
||||
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||
|
||||
class FileService
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public ChannelService $channelService,
|
||||
public ImageManager $imageManager,
|
||||
public Logger $logger
|
||||
)
|
||||
{
|
||||
}
|
||||
public ImageManager $imageManager,
|
||||
public Logger $logger,
|
||||
) {}
|
||||
|
||||
public function getPath(QueryRecord $file): string
|
||||
{
|
||||
return $this->channelService->channel->filesUrl . "/" . $file->_file->path;
|
||||
return $this->channelService->channel->filesUrl .
|
||||
"/" .
|
||||
$file->_file->path;
|
||||
}
|
||||
|
||||
public function createFromUrl(FilesSchema $schema, string $url): FileUploadResult
|
||||
{
|
||||
public function createFromUrl(
|
||||
FilesSchema $schema,
|
||||
string $url,
|
||||
): FileUploadResult {
|
||||
$pathinfo = pathinfo($url);
|
||||
$contents = file_get_contents($url);
|
||||
if ($contents === false) {
|
||||
throw new LucentException("Failed to upload file from url");
|
||||
}
|
||||
$file = '/tmp/' . $pathinfo['basename'];
|
||||
$file = "/tmp/" . $pathinfo["basename"];
|
||||
file_put_contents($file, $contents);
|
||||
$uploadedFile = new UploadedFile($file, $pathinfo['basename']);
|
||||
$uploadedFile = new UploadedFile($file, $pathinfo["basename"]);
|
||||
return $this->upload($schema, $uploadedFile);
|
||||
}
|
||||
|
||||
public function upload(FilesSchema $schema, UploadedFile $file): FileUploadResult
|
||||
public function upload(string $recordId, UploadedFile $file): DataFile
|
||||
{
|
||||
$originalName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
|
||||
$extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
|
||||
$originalName = pathinfo(
|
||||
$file->getClientOriginalName(),
|
||||
PATHINFO_FILENAME,
|
||||
);
|
||||
$extension = pathinfo(
|
||||
$file->getClientOriginalName(),
|
||||
PATHINFO_EXTENSION,
|
||||
);
|
||||
$originalFilename = $file->getClientOriginalName();
|
||||
$filename = $this->createFileName($originalName, $extension);
|
||||
$mimetype = $file->getMimeType();
|
||||
@@ -58,72 +65,74 @@ class FileService
|
||||
$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($schema);
|
||||
$path = $schema->path . "/" . $filename;
|
||||
$disk = $this->loadDisk();
|
||||
$path = "files/" . $recordId . "/" . $filename;
|
||||
$res = $disk->put(
|
||||
$path,
|
||||
file_get_contents($file),
|
||||
// 'public' // now managed by aws policy
|
||||
// 'public' // now managed by aws policy
|
||||
);
|
||||
|
||||
if ($res === false) {
|
||||
throw new LucentException("File $filename not uploaded");
|
||||
}
|
||||
|
||||
if($this->isImage($mimetype)){
|
||||
if ($this->isImage($mimetype)) {
|
||||
$this->createTemplates($disk, $path);
|
||||
}
|
||||
|
||||
|
||||
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
|
||||
$recordFile = new RecordFile(
|
||||
[$width, $height] = $this->isImage($mimetype)
|
||||
? getimagesize($file)
|
||||
: [0, 0];
|
||||
return new DataFile(
|
||||
id: Id::new(),
|
||||
recordId: $recordId,
|
||||
originalName: $originalFilename,
|
||||
mime: $mimetype,
|
||||
path: $path,
|
||||
disk: $schema->disk,
|
||||
size: $file->getSize(),
|
||||
width: $width,
|
||||
height: $height,
|
||||
checksum: $checksum
|
||||
);
|
||||
|
||||
return new FileUploadResult(
|
||||
recordFile: $recordFile,
|
||||
duplicateId: "",
|
||||
isDuplicate: false
|
||||
checksum: $checksum,
|
||||
);
|
||||
}
|
||||
|
||||
private function createFileName(string $originalName, string $extension): string
|
||||
{
|
||||
return Str::slug($originalName, '-') . '-' . uniqid() . '.' . $extension;
|
||||
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'];
|
||||
$imageMimes = [
|
||||
"image/webp",
|
||||
"image/gif",
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/tiff",
|
||||
];
|
||||
return in_array($mimetype, $imageMimes);
|
||||
}
|
||||
|
||||
public function loadDisk(Schema|string $schema): Filesystem
|
||||
public function loadDisk(): Filesystem
|
||||
{
|
||||
return Storage::disk($schema->disk ?? $schema);
|
||||
|
||||
return Storage::disk(config("lucent.disk"));
|
||||
}
|
||||
|
||||
private function checkDuplicate(string $schemaName, string $checksum, int $filesize): string
|
||||
{
|
||||
|
||||
$record = Database::make()->table("lucent_records")
|
||||
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)
|
||||
@@ -137,14 +146,14 @@ class FileService
|
||||
$originalImage = $this->imageManager->make($disk->get($path));
|
||||
foreach (config("lucent.imageFilters") as $preset => $filterClass) {
|
||||
$imageClone = clone $originalImage;
|
||||
$image = $imageClone->filter(new $filterClass);
|
||||
$image = $imageClone->filter(new $filterClass());
|
||||
$templateUri = "/templates/" . $preset . "/" . $path;
|
||||
$disk->put($templateUri, $image->encode('webp', 75));
|
||||
$disk->put($templateUri, $image->encode("webp", 75));
|
||||
}
|
||||
|
||||
$thumbDir = "thumbs/" . $path;
|
||||
|
||||
$image = $originalImage->fit(300, 300);
|
||||
$disk->put($thumbDir, $image->encode('webp', 75));
|
||||
$disk->put($thumbDir, $image->encode("webp", 75));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user