|
@@ -7,7 +7,8 @@ use Illuminate\Support\Facades\Storage;
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Str;
|
|
|
use Intervention\Image\ImageManager;
|
|
use Intervention\Image\ImageManager;
|
|
|
use Exception;
|
|
use Exception;
|
|
|
-
|
|
|
|
|
|
|
+use Aws\S3\S3Client;
|
|
|
|
|
+use Aws\Exception\AwsException;
|
|
|
class ImageUploadService
|
|
class ImageUploadService
|
|
|
{
|
|
{
|
|
|
/**
|
|
/**
|
|
@@ -28,7 +29,7 @@ class ImageUploadService
|
|
|
|
|
|
|
|
// Generate directory path
|
|
// Generate directory path
|
|
|
$uploadDirectory = $directory ?? config('image_upload.upload_directory', 'images/uploads');
|
|
$uploadDirectory = $directory ?? config('image_upload.upload_directory', 'images/uploads');
|
|
|
- $path = $uploadDirectory . '/' . date('Y/m/d');
|
|
|
|
|
|
|
+ $path = $uploadDirectory . '/' . date('Ym/d');
|
|
|
|
|
|
|
|
// Process and store the image
|
|
// Process and store the image
|
|
|
if (Str::contains($file->getMimeType(), 'image')) {
|
|
if (Str::contains($file->getMimeType(), 'image')) {
|
|
@@ -101,15 +102,32 @@ class ImageUploadService
|
|
|
*/
|
|
*/
|
|
|
protected function processAndStoreImage(UploadedFile $file, string $path, string $disk): array
|
|
protected function processAndStoreImage(UploadedFile $file, string $path, string $disk): array
|
|
|
{
|
|
{
|
|
|
- try {
|
|
|
|
|
|
|
+
|
|
|
$manager = new ImageManager();
|
|
$manager = new ImageManager();
|
|
|
$image = $manager->make($file)->encode('webp');
|
|
$image = $manager->make($file)->encode('webp');
|
|
|
|
|
|
|
|
$fileName = Str::random(40) . '.webp';
|
|
$fileName = Str::random(40) . '.webp';
|
|
|
$fullPath = $path . '/' . $fileName;
|
|
$fullPath = $path . '/' . $fileName;
|
|
|
|
|
|
|
|
- Storage::disk($disk)->put($fullPath, $image);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 方式一:使用原生 AWS SDK 绕过 Laravel Storage
|
|
|
|
|
+ $s3Client = new S3Client([
|
|
|
|
|
+ 'version' => 'latest',
|
|
|
|
|
+ 'region' => env('AWS_DEFAULT_REGION'),
|
|
|
|
|
+ 'credentials' => [
|
|
|
|
|
+ 'key' => env('AWS_ACCESS_KEY_ID'),
|
|
|
|
|
+ 'secret' => env('AWS_SECRET_ACCESS_KEY'),
|
|
|
|
|
+ ],
|
|
|
|
|
+ 'http' => [
|
|
|
|
|
+ 'verify' => false, // 临时禁用 SSL 验证用于测试
|
|
|
|
|
+ ],
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ $result = $s3Client->putObject([
|
|
|
|
|
+ 'Bucket' => env('AWS_BUCKET'),
|
|
|
|
|
+ 'Key' => $fullPath,
|
|
|
|
|
+ 'Body' => (string) $image,
|
|
|
|
|
+ 'ContentType' => 'image/webp',
|
|
|
|
|
+ ]);
|
|
|
return [
|
|
return [
|
|
|
'success' => true,
|
|
'success' => true,
|
|
|
'path' => $fullPath,
|
|
'path' => $fullPath,
|
|
@@ -119,9 +137,7 @@ class ImageUploadService
|
|
|
'size' => $file->getSize(),
|
|
'size' => $file->getSize(),
|
|
|
'disk' => $disk,
|
|
'disk' => $disk,
|
|
|
];
|
|
];
|
|
|
- } catch (Exception $e) {
|
|
|
|
|
- throw new Exception(__('Failed to process image: :error', ['error' => $e->getMessage()]));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -164,7 +180,7 @@ class ImageUploadService
|
|
|
public function delete(string $path, ?string $disk = null): bool
|
|
public function delete(string $path, ?string $disk = null): bool
|
|
|
{
|
|
{
|
|
|
$disk = $disk ?? config('image_upload.default_disk', 's3');
|
|
$disk = $disk ?? config('image_upload.default_disk', 's3');
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return Storage::disk($disk)->delete($path);
|
|
return Storage::disk($disk)->delete($path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -178,7 +194,7 @@ class ImageUploadService
|
|
|
public function getUrl(string $path, ?string $disk = null): string
|
|
public function getUrl(string $path, ?string $disk = null): string
|
|
|
{
|
|
{
|
|
|
$disk = $disk ?? config('image_upload.default_disk', 's3');
|
|
$disk = $disk ?? config('image_upload.default_disk', 's3');
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return Storage::disk($disk)->url($path);
|
|
return Storage::disk($disk)->url($path);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|