|
@@ -6,11 +6,9 @@ use Carbon\Carbon;
|
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Arr;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Event;
|
|
|
-use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use Longyi\Blog\Models\Category;
|
|
use Longyi\Blog\Models\Category;
|
|
|
|
|
+use Longyi\ImageUpload\Services\ImageUploadService;
|
|
|
use Webkul\Core\Eloquent\Repository;
|
|
use Webkul\Core\Eloquent\Repository;
|
|
|
-use Intervention\Image\ImageManager;
|
|
|
|
|
-use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class BlogRepository extends Repository
|
|
class BlogRepository extends Repository
|
|
|
{
|
|
{
|
|
@@ -97,31 +95,33 @@ class BlogRepository extends Repository
|
|
|
*/
|
|
*/
|
|
|
public function uploadImages($data, $blog, $type = 'src')
|
|
public function uploadImages($data, $blog, $type = 'src')
|
|
|
{
|
|
{
|
|
|
|
|
+ $imageUploadService = app(ImageUploadService::class);
|
|
|
|
|
+
|
|
|
if (isset($data[$type])) {
|
|
if (isset($data[$type])) {
|
|
|
foreach ($data[$type] as $imageId => $image) {
|
|
foreach ($data[$type] as $imageId => $image) {
|
|
|
$file = $type . '.' . $imageId;
|
|
$file = $type . '.' . $imageId;
|
|
|
|
|
|
|
|
- $dir = 'blog-images/' . $blog->id;
|
|
|
|
|
-
|
|
|
|
|
if (request()->hasFile($file)) {
|
|
if (request()->hasFile($file)) {
|
|
|
if ($blog->{$type}) {
|
|
if ($blog->{$type}) {
|
|
|
- Storage::delete($blog->{$type});
|
|
|
|
|
|
|
+ $imageUploadService->delete($blog->{$type});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $manager = new ImageManager();
|
|
|
|
|
|
|
+ $result = $imageUploadService->upload(
|
|
|
|
|
+ request()->file($file)
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
- $image = $manager->make(request()->file($file))->encode('webp');
|
|
|
|
|
-
|
|
|
|
|
- $blog->{$type} = 'blog-images/' . $blog->id . '/' . Str::random(40) . '.webp';
|
|
|
|
|
|
|
+ if (! ($result['success'] ?? false)) {
|
|
|
|
|
+ throw new \Exception($result['error'] ?? 'Blog image upload failed.');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- Storage::put($blog->{$type}, $image);
|
|
|
|
|
|
|
+ $blog->{$type} = $result['path'];
|
|
|
|
|
|
|
|
$blog->save();
|
|
$blog->save();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
if ($blog->{$type}) {
|
|
if ($blog->{$type}) {
|
|
|
- Storage::delete($blog->{$type});
|
|
|
|
|
|
|
+ $imageUploadService->delete($blog->{$type});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$blog->{$type} = null;
|
|
$blog->{$type} = null;
|
|
@@ -140,13 +140,15 @@ class BlogRepository extends Repository
|
|
|
{
|
|
{
|
|
|
$blogItem = $this->find($id);
|
|
$blogItem = $this->find($id);
|
|
|
|
|
|
|
|
- $blogItemImage = $blogItem->src;
|
|
|
|
|
|
|
+ $imageUploadService = app(ImageUploadService::class);
|
|
|
|
|
|
|
|
- $blogItemMobileImage = $blogItem->m_src;
|
|
|
|
|
-
|
|
|
|
|
- Storage::delete($blogItemImage);
|
|
|
|
|
|
|
+ if ($blogItem->src) {
|
|
|
|
|
+ $imageUploadService->delete($blogItem->src);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- Storage::delete($blogItemMobileImage);
|
|
|
|
|
|
|
+ if ($blogItem->m_src) {
|
|
|
|
|
+ $imageUploadService->delete($blogItem->m_src);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return $this->model->destroy($id);
|
|
return $this->model->destroy($id);
|
|
|
}
|
|
}
|
|
@@ -208,4 +210,4 @@ class BlogRepository extends Repository
|
|
|
|
|
|
|
|
return $blogs;
|
|
return $blogs;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|