2025-05-12 00:19:35 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service\common\upload;
|
|
|
|
|
|
|
2026-08-10 18:32:06 +08:00
|
|
|
|
use App\BaseApp\BaseNotAuthService;
|
|
|
|
|
|
use App\Service\common\oss\OssStorageInterface;
|
2025-05-12 00:19:35 +08:00
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
2026-08-10 18:32:06 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 本地磁盘上传;支持 domain / path_prefix 覆盖
|
|
|
|
|
|
*/
|
|
|
|
|
|
class LocalhostStorageService extends BaseNotAuthService implements OssStorageInterface
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
2026-08-10 18:32:06 +08:00
|
|
|
|
protected array $config = [
|
|
|
|
|
|
'domain' => '',
|
|
|
|
|
|
'path_prefix' => '',
|
|
|
|
|
|
];
|
2025-05-12 00:19:35 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-10 18:32:06 +08:00
|
|
|
|
* 注入运行时配置(链式)
|
2025-05-12 00:19:35 +08:00
|
|
|
|
*/
|
2026-08-10 18:32:06 +08:00
|
|
|
|
public function withConfig(array $config): static
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
2026-08-10 18:32:06 +08:00
|
|
|
|
$this->config = array_merge($this->config, $config);
|
|
|
|
|
|
return $this;
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-10 18:32:06 +08:00
|
|
|
|
public function uploadImage($filePath, string $key): bool|array
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
return $this->uploadFile($filePath, $key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-10 18:32:06 +08:00
|
|
|
|
public function uploadVideo($filePath, string $key): bool|array
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
return $this->uploadFile($filePath, $key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function deleteImage($key): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->deleteFile($key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function deleteVideo($key): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->deleteFile($key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-10 18:32:06 +08:00
|
|
|
|
* 写入本地 storage;兼容 UploadedFile 与路径字符串
|
2025-05-12 00:19:35 +08:00
|
|
|
|
*/
|
2026-08-10 18:32:06 +08:00
|
|
|
|
private function uploadFile(mixed $filePath, string $key): bool|array
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
2026-08-10 18:32:06 +08:00
|
|
|
|
$prefix = trim((string) ($this->config['path_prefix'] ?? ''), '/');
|
|
|
|
|
|
if ($prefix !== '' && !str_starts_with($key, $prefix . '/')) {
|
|
|
|
|
|
$key = $prefix . '/' . ltrim($key, '/');
|
|
|
|
|
|
}
|
|
|
|
|
|
$path = is_object($filePath) && method_exists($filePath, 'getRealPath')
|
|
|
|
|
|
? $filePath->getRealPath()
|
|
|
|
|
|
: (string) $filePath;
|
|
|
|
|
|
if (!Storage::put($key, file_get_contents($path))) {
|
|
|
|
|
|
return false;
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|
2026-08-10 18:32:06 +08:00
|
|
|
|
$domain = rtrim((string) ($this->config['domain'] ?? ''), '/');
|
|
|
|
|
|
$url = $domain !== '' ? ($domain . '/' . $key) : asset('/storage/' . $key);
|
|
|
|
|
|
return [
|
|
|
|
|
|
'key' => $key,
|
|
|
|
|
|
'url' => $url,
|
|
|
|
|
|
];
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function deleteFile(string $key): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return Storage::delete($key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|