This commit is contained in:
2026-04-16 11:25:40 +05:00
parent 2ba5677c45
commit 3aa4601229
13 changed files with 80 additions and 234 deletions

View File

@@ -10,79 +10,71 @@ class ImageResize
{ {
/** /**
* @param string $type * @param string $type
* @return string * @return string relative path used as S3 key, e.g. uploads/posters/thumbs/2024/01/01
*/ */
private function mkdir(string $type) private function thumbFolder(string $type): string
{ {
$folder = Carbon::now()->format('Y/m/d'); $folder = Carbon::now()->format('Y/m/d');
$path = "uploads/{$type}/thumbs/{$folder}"; return "uploads/{$type}/thumbs/{$folder}";
if (!file_exists(public_path($path))) {
mkdir(public_path($path), 0775, true);
}
return $path;
} }
/** /**
* @param $path * Resize an image and upload to S3/MinIO (or save locally).
* @param $size *
* @param $type * @param string $path Relative path of the source image (from 'public' disk = storage/app/public/)
* @param bool $deleteOriginal * @param int $size Target width in pixels
* @return string * @param string $type Subfolder name (posters, screens, brands …)
* @throws \Exception * @param bool $deleteOriginal Delete the source file after resizing
* @return string Relative path (S3 key) of the generated thumb
*/ */
public function resize($path, $size, $type, $deleteOriginal = false) public function resize(string $path, int $size, string $type, bool $deleteOriginal = false): string
{ {
// 1. Resolve source path // 1. Locate the source file (stored via ->store('temp', 'public'))
$srcPath = file_exists(public_path($path)) $srcPath = storage_path('app/public/' . $path);
? public_path($path)
: storage_path('app/public/' . $path);
if (!file_exists($srcPath)) { if (!file_exists($srcPath)) {
if (file_exists($path)) { throw new \Exception("Source image not found: {$srcPath}");
$srcPath = $path;
} else {
// Last ditch effort: try to get from S3 if it's not local
if (env('FILESYSTEM_DISK') == 's3' && Storage::disk('s3')->exists($path)) {
$fileContent = Storage::disk('s3')->get($path);
$tmpLocal = storage_path('app/public/temp_' . basename($path));
file_put_contents($tmpLocal, $fileContent);
$srcPath = $tmpLocal;
$deleteOriginal = true; // Clean up the temp download
} else {
throw new \Exception("Source image not found: " . $path);
}
}
} }
// 2. Prepare thumb destination // 2. Resize into a temp file in storage/app/public/tmp/
$name = basename($path); $tmpDir = storage_path('app/public/tmp');
$folder = $this->mkdir($type); if (!is_dir($tmpDir)) {
$path_thumb = "{$folder}/{$name}"; mkdir($tmpDir, 0775, true);
$absPathThumb = public_path($path_thumb); }
$thumbFilename = basename($path);
$tmpThumb = "{$tmpDir}/{$thumbFilename}";
// 3. Resize and Save Thumb
$img = Imagee::make($srcPath); $img = Imagee::make($srcPath);
$img->resize($size, null, function ($constraint) { $img->resize($size, null, function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
$img->save($absPathThumb, 100); $img->save($tmpThumb, 90);
// 4. Handle S3 Upload // 3. Upload thumb to S3/MinIO
if (env('FILESYSTEM_DISK') == 's3') { $thumbKey = $this->thumbFolder($type) . '/' . $thumbFilename;
Storage::disk('s3')->put($path_thumb, file_get_contents($absPathThumb));
// Remove local thumb after S3 upload if (env('FILESYSTEM_DISK') === 's3') {
if (file_exists($absPathThumb)) { Storage::disk('s3')->put($thumbKey, file_get_contents($tmpThumb));
unlink($absPathThumb); } else {
// Local: move to public/uploads/…/thumbs/…
$localDir = public_path(dirname($thumbKey));
if (!is_dir($localDir)) {
mkdir($localDir, 0775, true);
} }
rename($tmpThumb, public_path($thumbKey));
} }
// 5. Cleanup original if requested // 4. Clean up tmp thumb
if (file_exists($tmpThumb)) {
unlink($tmpThumb);
}
// 5. Clean up original temp file if requested
if ($deleteOriginal && file_exists($srcPath)) { if ($deleteOriginal && file_exists($srcPath)) {
unlink($srcPath); unlink($srcPath);
} }
return $path_thumb; return $thumbKey;
} }
} }

View File

@@ -54,17 +54,8 @@ class Controller extends ExController
} }
if ($request->hasFile('image')) { if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($brand->image);
// delete old image from s3
Storage::disk('s3')->delete($brand->image);
$path = $this->storeImageToS3($request->file('image')); $path = $this->storeImageToS3($request->file('image'));
} else {
// detele old image
if (is_file($brand->image)) {
unlink($brand->image);
}
$path = $request->file('image')->store('uploads/brands');
}
} else { } else {
$path = $brand->image; $path = $brand->image;
} }
@@ -87,12 +78,7 @@ class Controller extends ExController
} }
if ($request->hasFile('image')) { if ($request->hasFile('image')) {
$file = $request->file('image'); $path = $this->storeImageToS3($request->file('image'));
if (env('FILESYSTEM_DISK') == 's3') {
$path = $this->storeImageToS3($file);
} else {
$path = $file->store('uploads/brands');
}
} }
$this->dispatchSync(StoreJob::fromRequest($request, $path)); $this->dispatchSync(StoreJob::fromRequest($request, $path));
@@ -109,14 +95,7 @@ class Controller extends ExController
{ {
$this->authorize('delete', 'brands'); $this->authorize('delete', 'brands');
if (is_file($brand->image)) { Storage::delete($brand->image);
unlink($brand->image);
}
// delete image from s3
if (env('FILESYSTEM_DISK') == 's3') {
Storage::disk('s3')->delete($brand->image);
}
Product::where('brand_id', $brand->id)->withTrashed()->update([ Product::where('brand_id', $brand->id)->withTrashed()->update([
'brand_id' => null 'brand_id' => null

View File

@@ -155,14 +155,7 @@ class Controller extends ExController
$this->authorize('delete', 'categories'); $this->authorize('delete', 'categories');
$category = Category::findOrFail($category); $category = Category::findOrFail($category);
if (is_file($category->image)) { Storage::delete($category->image);
unlink($category->image);
}
// delete image from s3
if (env('FILESYSTEM_DISK') == 's3') {
Storage::disk('s3')->delete($category->image);
}
$category->delete(); $category->delete();
$this->info(trans('admin.messages.deleted')); $this->info(trans('admin.messages.deleted'));

View File

@@ -10,7 +10,6 @@ use App\Http\Requests\Dashboard\Partner\Store as StoreRequest;
use App\Jobs\Dashboard\Partner\Store as StoreJob; use App\Jobs\Dashboard\Partner\Store as StoreJob;
use App\Jobs\Dashboard\Partner\Update as UpdateJob; use App\Jobs\Dashboard\Partner\Update as UpdateJob;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
class Controller extends ExController class Controller extends ExController
@@ -48,18 +47,7 @@ class Controller extends ExController
public function update(UpdateRequest $request, Partner $partner) public function update(UpdateRequest $request, Partner $partner)
{ {
if ($request->hasFile('image')) { if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($partner->image);
// delete old image from s3
Storage::disk('s3')->delete($partner->image);
} else {
// detele old image
$imagePath = public_path($partner->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
$path = $request->file('image')->store('uploads/partners'); $path = $request->file('image')->store('uploads/partners');
} else { } else {
$path = $partner->image; $path = $partner->image;
@@ -75,17 +63,7 @@ class Controller extends ExController
{ {
$this->authorize('delete', 'partners'); $this->authorize('delete', 'partners');
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($partner->image);
// delete old image from s3
Storage::disk('s3')->delete($partner->image);
} else {
$imagePath = public_path($partner->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
$partner->delete(); $partner->delete();
$this->info(trans('admin.messages.deleted')); $this->info(trans('admin.messages.deleted'));

View File

@@ -57,13 +57,7 @@ class Controller extends ExController
} }
if ($request->hasFile('image')) { if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($post->image);
Storage::disk('s3')->delete($post->image);
} else {
if (is_file($post->image)) {
unlink($post->image);
}
}
$path = $request->file('image')->store('uploads/posts'); $path = $request->file('image')->store('uploads/posts');
} else { } else {
$path = $post->image; $path = $post->image;
@@ -122,14 +116,7 @@ class Controller extends ExController
public function delete(Post $post) public function delete(Post $post)
{ {
$this->authorize('delete', 'posts'); $this->authorize('delete', 'posts');
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($post->image);
Storage::disk('s3')->delete($post->image);
} else {
if (is_file($post->image)) {
unlink($post->image);
}
}
$post->delete(); $post->delete();
$this->info(trans('admin.messages.deleted')); $this->info(trans('admin.messages.deleted'));
return redirect()->back(); return redirect()->back();

View File

@@ -10,7 +10,6 @@ use App\Jobs\Dashboard\Service\Store as StoreJob;
use App\Jobs\Dashboard\Service\Update as UpdateJob; use App\Jobs\Dashboard\Service\Update as UpdateJob;
use App\Models\Problem; use App\Models\Problem;
use App\Models\ServiceProblem; use App\Models\ServiceProblem;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
class Controller extends ExController class Controller extends ExController
@@ -62,17 +61,7 @@ class Controller extends ExController
public function update(UpdateRequest $request, Service $service) public function update(UpdateRequest $request, Service $service)
{ {
if ($request->hasFile('image')) { if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($service->image);
$imagePath = $service->image;
Storage::disk('s3')->delete($imagePath);
} else {
$imagePath = public_path($service->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
$path = $request->file('image')->store('uploads/services'); $path = $request->file('image')->store('uploads/services');
} else { } else {
$path = $service->image; $path = $service->image;
@@ -106,17 +95,7 @@ class Controller extends ExController
{ {
$this->authorize('delete', 'services'); $this->authorize('delete', 'services');
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($service->image);
$imagePath = $service->image;
Storage::disk('s3')->delete($imagePath);
} else {
$imagePath = public_path($service->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
ServiceProblem::where('service_id', $service->id)->delete(); ServiceProblem::where('service_id', $service->id)->delete();
$service->delete(); $service->delete();

View File

@@ -49,17 +49,7 @@ class Controller extends ExController
public function update(UpdateRequest $request, UsefulInfo $usefulinfo) public function update(UpdateRequest $request, UsefulInfo $usefulinfo)
{ {
if ($request->hasFile('image')) { if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($usefulinfo->image);
$imagePath = $usefulinfo->image;
Storage::disk('s3')->delete($imagePath);
} else {
$imagePath = public_path($usefulinfo->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
$path = $request->file('image')->store('uploads/usefulinfos'); $path = $request->file('image')->store('uploads/usefulinfos');
} else { } else {
$path = $usefulinfo->image; $path = $usefulinfo->image;
@@ -84,17 +74,7 @@ class Controller extends ExController
$item->delete(); $item->delete();
} }
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($usefulinfo->image);
$imagePath = $usefulinfo->image;
Storage::disk('s3')->delete($imagePath);
} else {
$imagePath = public_path($usefulinfo->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
$usefulinfo->delete(); $usefulinfo->delete();
$this->info(trans('admin.messages.deleted')); $this->info(trans('admin.messages.deleted'));

View File

@@ -43,20 +43,8 @@ class Update extends FormRequest
public function getImage(Category $category): string public function getImage(Category $category): string
{ {
if ($this->hasFile('image')) { if ($this->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($category->image);
// delete old image from s3 return (string) $this->file('image')->store('uploads/categories');
Storage::disk('s3')->delete($category->image);
$folder = "uploads/categories";
return (string) $this->file('image')->store($folder);
} else {
// detele old image
if (is_file($category->image)) {
unlink($category->image);
}
return $this->file('image')->store('uploads/categories', 'local');
}
} else { } else {
return 'null'; return 'null';
} }

View File

@@ -63,15 +63,7 @@ class Update extends FormRequest
public function getPoster(Product $product): string public function getPoster(Product $product): string
{ {
if ($this->hasFile('poster')) { if ($this->hasFile('poster')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($product->poster);
// delete old file from s3
Storage::disk('s3')->delete($product->poster);
} else {
// delete old file
$oldPath = public_path($product->poster);
if (is_file($oldPath)) unlink($oldPath);
}
$folder = "uploads/posters/" . Carbon::now()->format('Y/m/d'); $folder = "uploads/posters/" . Carbon::now()->format('Y/m/d');
return (string) $this->file('poster')->store($folder); return (string) $this->file('poster')->store($folder);
} }
@@ -85,13 +77,9 @@ class Update extends FormRequest
public function getCalc(Product $product): string|null public function getCalc(Product $product): string|null
{ {
if ($this->hasFile('calc')) { if ($this->hasFile('calc')) {
if (env('FILESYSTEM_DISK') == 's3' and $product->calc) { if ($product->calc) {
// delete old file from s3 Storage::delete($product->calc);
Storage::disk('s3')->delete($product->calc);
} elseif($product->calc) {
unlink($product->calc);
} }
$folder = "uploads/calc/" . Carbon::now()->format('Y/m/d'); $folder = "uploads/calc/" . Carbon::now()->format('Y/m/d');
return (string) $this->file('calc')->store($folder); return (string) $this->file('calc')->store($folder);
} }
@@ -101,11 +89,9 @@ class Update extends FormRequest
public function getDataSheet(Product $product) public function getDataSheet(Product $product)
{ {
if ($this->hasFile('data_sheet')) { if ($this->hasFile('data_sheet')) {
if ($product->data_sheet) {
if (is_file($this->get('data_sheet'))) { Storage::delete($product->data_sheet);
unlink($this->get("data_sheet"));
} }
$folder = "uploads/datasheet/" . Carbon::now()->format('Y/m/d'); $folder = "uploads/datasheet/" . Carbon::now()->format('Y/m/d');
return (string) $this->file('data_sheet')->store($folder); return (string) $this->file('data_sheet')->store($folder);
} }
@@ -119,18 +105,8 @@ class Update extends FormRequest
public function getPosterThumb(Product $product): string public function getPosterThumb(Product $product): string
{ {
if ($this->hasFile('poster')) { if ($this->hasFile('poster')) {
if (env('FILESYSTEM_DISK') == 's3') { Storage::delete($product->poster_thumb);
// delete old file from s3 $tempPath = $this->file('poster')->store('temp', 'public');
Storage::disk('s3')->delete($product->poster_thumb);
} else {
// delete old file local
$oldPath = public_path($product->poster_thumb);
if (is_file($oldPath)) unlink($oldPath);
}
$image = $this->file('poster');
$tempPath = $image->store('temp', 'public');
$resizer = new ImageResize(); $resizer = new ImageResize();
return $resizer->resize($tempPath, 322, 'posters', true); return $resizer->resize($tempPath, 322, 'posters', true);
} }

View File

@@ -60,12 +60,7 @@ class Child
$tempPath = $screen['image']->store('temp', 'public'); $tempPath = $screen['image']->store('temp', 'public');
$thumbPath = $this->image->resize($tempPath, 322, 'screens', true); $thumbPath = $this->image->resize($tempPath, 322, 'screens', true);
// Get screen size (local or s3) $this->size = Storage::size($path);
if (env('FILESYSTEM_DISK') == 's3') {
$this->size = Storage::disk('s3')->size($path);
} else {
$this->size = filesize(public_path($path));
}
Screen::create([ Screen::create([
'path' => $path, 'path' => $path,

View File

@@ -5,6 +5,7 @@ namespace App\Jobs\Dashboard\Product;
use App\Http\Requests\Dashboard\Product\Update as Request; use App\Http\Requests\Dashboard\Product\Update as Request;
use App\Models\Screen; use App\Models\Screen;
use App\Models\Product; use App\Models\Product;
use Illuminate\Support\Facades\Storage;
class Deletes class Deletes
{ {
@@ -29,14 +30,7 @@ class Deletes
foreach ($this->request->deletes['screens'] as $screen) { foreach ($this->request->deletes['screens'] as $screen) {
$sc = Screen::where('id', $screen)->first(); $sc = Screen::where('id', $screen)->first();
if (!empty($sc)) { if (!empty($sc)) {
if (is_file($sc->path)) { Storage::delete([$sc->path, $sc->path_thumb]);
unlink($sc->path);
}
if (is_file($sc->path_thumb)) {
unlink($sc->path_thumb);
}
$sc->delete(); $sc->delete();
} }
} }

View File

@@ -96,13 +96,10 @@ class ContractService
// 1 - store temp file // 1 - store temp file
$path = storage_path('app/public/' . $oldPath); $path = storage_path('app/public/' . $oldPath);
// Store the image on S3 or keep local Storage::put($oldPath, file_get_contents($path));
if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio'])) {
Storage::disk('s3')->put($oldPath, file_get_contents($path));
}
// 3 - delete resized file // 3 - delete resized file
if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio']) && is_file($path)) { if (is_file($path)) {
unlink($path); unlink($path);
} }
} }

View File

@@ -9,6 +9,14 @@ server {
root /quyoshli/public; root /quyoshli/public;
index index.php; index index.php;
# MinIO bucket proxy — serves files over HTTPS
location /quyoshli/ {
proxy_pass http://quyoshli-minio:9100/quyoshli/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_buffering off;
}
# try to serve file directly, fallback to start.php # try to serve file directly, fallback to start.php
location / { location / {
try_files $uri /$uri /index.php$is_args$args; try_files $uri /$uri /index.php$is_args$args;