43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use RuntimeException;
|
|
|
|
class Uploads
|
|
{
|
|
public static function store(UploadedFile $file, string $path, ?string $disk = null): string
|
|
{
|
|
$diskName = $disk ?: config('filesystems.default');
|
|
$storedPath = $disk ? $file->store($path, $disk) : $file->store($path);
|
|
|
|
if (!$storedPath) {
|
|
throw new RuntimeException("File upload failed: {$path}");
|
|
}
|
|
|
|
if (!Storage::disk($diskName)->exists($storedPath)) {
|
|
throw new RuntimeException("Uploaded file was not found on disk [{$diskName}]: {$storedPath}");
|
|
}
|
|
|
|
return $storedPath;
|
|
}
|
|
|
|
public static function put(string $path, string $contents, ?string $disk = null): void
|
|
{
|
|
$stored = $disk
|
|
? Storage::disk($disk)->put($path, $contents)
|
|
: Storage::put($path, $contents);
|
|
|
|
if (!$stored) {
|
|
throw new RuntimeException("File upload failed: {$path}");
|
|
}
|
|
|
|
$diskName = $disk ?: config('filesystems.default');
|
|
if (!Storage::disk($diskName)->exists($path)) {
|
|
throw new RuntimeException("Uploaded file was not found on disk [{$diskName}]: {$path}");
|
|
}
|
|
}
|
|
}
|