wip upload files and select
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\File;
|
||||
|
||||
use Lucent\Data\File as DataFile;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Data\File;
|
||||
|
||||
class FileRepo
|
||||
{
|
||||
public function __construct() {}
|
||||
|
||||
public static function create(DataFile $file): void
|
||||
{
|
||||
Database::make()->table("lucent_files")->insert($file->toDB());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return File[]
|
||||
*/
|
||||
public static function byRecordId(string $recordId): array
|
||||
{
|
||||
return Database::make()
|
||||
->table("lucent_files")
|
||||
->where("recordId", $recordId)
|
||||
->get()
|
||||
->map(File::fromDB(...))
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
@@ -85,10 +85,10 @@ class FileService
|
||||
[$width, $height] = $this->isImage($mimetype)
|
||||
? getimagesize($file)
|
||||
: [0, 0];
|
||||
return new DataFile(
|
||||
$dataFile = new DataFile(
|
||||
id: Id::new(),
|
||||
recordId: $recordId,
|
||||
originalName: $originalFilename,
|
||||
filename: $originalFilename,
|
||||
mime: $mimetype,
|
||||
path: $path,
|
||||
size: $file->getSize(),
|
||||
@@ -96,6 +96,9 @@ class FileService
|
||||
height: $height,
|
||||
checksum: $checksum,
|
||||
);
|
||||
|
||||
FileRepo::create($dataFile);
|
||||
return $dataFile;
|
||||
}
|
||||
|
||||
private function createFileName(
|
||||
@@ -112,10 +115,11 @@ class FileService
|
||||
private function isImage(string $mimetype): bool
|
||||
{
|
||||
$imageMimes = [
|
||||
"image/webp",
|
||||
"image/gif",
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/avif",
|
||||
"image/webp",
|
||||
"image/gif",
|
||||
"image/tiff",
|
||||
];
|
||||
return in_array($mimetype, $imageMimes);
|
||||
@@ -156,4 +160,12 @@ class FileService
|
||||
$image = $originalImage->fit(300, 300);
|
||||
$disk->put($thumbDir, $image->encode("webp", 75));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DataFile[]
|
||||
*/
|
||||
public function filesForRecord(string $recordId): array
|
||||
{
|
||||
return FileRepo::byRecordId($recordId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user