Implement creator studio and upload updates
This commit is contained in:
@@ -7,11 +7,17 @@ namespace App\Services\Uploads;
|
||||
use App\DTOs\Uploads\UploadStoredFile;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use RuntimeException;
|
||||
|
||||
final class UploadStorageService
|
||||
{
|
||||
public function localOriginalsRoot(): string
|
||||
{
|
||||
return rtrim((string) config('uploads.local_originals_root'), DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
public function sectionPath(string $section): string
|
||||
{
|
||||
$root = rtrim((string) config('uploads.storage_root'), DIRECTORY_SEPARATOR);
|
||||
@@ -76,6 +82,18 @@ final class UploadStorageService
|
||||
return $dir;
|
||||
}
|
||||
|
||||
public function ensureLocalOriginalHashDirectory(string $hash): string
|
||||
{
|
||||
$segments = $this->hashSegments($hash);
|
||||
$dir = $this->localOriginalsRoot() . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $segments);
|
||||
|
||||
if (! File::exists($dir)) {
|
||||
File::makeDirectory($dir, 0755, true);
|
||||
}
|
||||
|
||||
return $dir;
|
||||
}
|
||||
|
||||
public function sectionRelativePath(string $section, string $hash, string $filename): string
|
||||
{
|
||||
$segments = $this->hashSegments($hash);
|
||||
@@ -84,6 +102,105 @@ final class UploadStorageService
|
||||
return $section . '/' . implode('/', $segments) . '/' . ltrim($filename, '/');
|
||||
}
|
||||
|
||||
public function localOriginalPath(string $hash, string $filename): string
|
||||
{
|
||||
return $this->ensureLocalOriginalHashDirectory($hash) . DIRECTORY_SEPARATOR . ltrim($filename, DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
public function objectDiskName(): string
|
||||
{
|
||||
return (string) config('uploads.object_storage.disk', 's3');
|
||||
}
|
||||
|
||||
public function objectBasePrefix(): string
|
||||
{
|
||||
return trim((string) config('uploads.object_storage.prefix', 'artworks'), '/');
|
||||
}
|
||||
|
||||
public function objectPathForVariant(string $variant, string $hash, string $filename): string
|
||||
{
|
||||
$segments = implode('/', $this->hashSegments($hash));
|
||||
$basePrefix = $this->objectBasePrefix();
|
||||
$normalizedVariant = trim($variant, '/');
|
||||
|
||||
if ($normalizedVariant === 'original') {
|
||||
return sprintf('%s/original/%s/%s', $basePrefix, $segments, ltrim($filename, '/'));
|
||||
}
|
||||
|
||||
return sprintf('%s/%s/%s/%s', $basePrefix, $normalizedVariant, $segments, ltrim($filename, '/'));
|
||||
}
|
||||
|
||||
public function putObjectFromPath(string $sourcePath, string $objectPath, string $contentType, array $extraOptions = []): void
|
||||
{
|
||||
$stream = fopen($sourcePath, 'rb');
|
||||
if ($stream === false) {
|
||||
throw new RuntimeException('Unable to open source file for object storage upload.');
|
||||
}
|
||||
|
||||
try {
|
||||
$written = Storage::disk($this->objectDiskName())->put($objectPath, $stream, array_merge([
|
||||
'visibility' => 'public',
|
||||
'CacheControl' => 'public, max-age=31536000, immutable',
|
||||
'ContentType' => $contentType,
|
||||
], $extraOptions));
|
||||
} finally {
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
if ($written !== true) {
|
||||
throw new RuntimeException('Object storage upload failed.');
|
||||
}
|
||||
}
|
||||
|
||||
public function putObjectContents(string $objectPath, string $contents, string $contentType, array $extraOptions = []): void
|
||||
{
|
||||
$written = Storage::disk($this->objectDiskName())->put($objectPath, $contents, array_merge([
|
||||
'visibility' => 'public',
|
||||
'CacheControl' => 'public, max-age=31536000, immutable',
|
||||
'ContentType' => $contentType,
|
||||
], $extraOptions));
|
||||
|
||||
if ($written !== true) {
|
||||
throw new RuntimeException('Object storage upload failed.');
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteObject(string $objectPath): void
|
||||
{
|
||||
if ($objectPath === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
Storage::disk($this->objectDiskName())->delete($objectPath);
|
||||
}
|
||||
|
||||
public function readObject(string $objectPath): ?string
|
||||
{
|
||||
if ($objectPath === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$disk = Storage::disk($this->objectDiskName());
|
||||
if (! $disk->exists($objectPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$contents = $disk->get($objectPath);
|
||||
|
||||
return is_string($contents) && $contents !== '' ? $contents : null;
|
||||
}
|
||||
|
||||
public function deleteLocalFile(?string $path): void
|
||||
{
|
||||
if (! is_string($path) || trim($path) === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (File::exists($path)) {
|
||||
File::delete($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function originalHashExists(string $hash): bool
|
||||
{
|
||||
$segments = $this->hashSegments($hash);
|
||||
|
||||
Reference in New Issue
Block a user