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
* @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');
$path = "uploads/{$type}/thumbs/{$folder}";
if (!file_exists(public_path($path))) {
mkdir(public_path($path), 0775, true);
}
return $path;
return "uploads/{$type}/thumbs/{$folder}";
}
/**
* @param $path
* @param $size
* @param $type
* @param bool $deleteOriginal
* @return string
* @throws \Exception
* Resize an image and upload to S3/MinIO (or save locally).
*
* @param string $path Relative path of the source image (from 'public' disk = storage/app/public/)
* @param int $size Target width in pixels
* @param string $type Subfolder name (posters, screens, brands …)
* @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
$srcPath = file_exists(public_path($path))
? public_path($path)
: storage_path('app/public/' . $path);
// 1. Locate the source file (stored via ->store('temp', 'public'))
$srcPath = storage_path('app/public/' . $path);
if (!file_exists($srcPath)) {
if (file_exists($path)) {
$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);
}
}
throw new \Exception("Source image not found: {$srcPath}");
}
// 2. Prepare thumb destination
$name = basename($path);
$folder = $this->mkdir($type);
$path_thumb = "{$folder}/{$name}";
$absPathThumb = public_path($path_thumb);
// 2. Resize into a temp file in storage/app/public/tmp/
$tmpDir = storage_path('app/public/tmp');
if (!is_dir($tmpDir)) {
mkdir($tmpDir, 0775, true);
}
$thumbFilename = basename($path);
$tmpThumb = "{$tmpDir}/{$thumbFilename}";
// 3. Resize and Save Thumb
$img = Imagee::make($srcPath);
$img->resize($size, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save($absPathThumb, 100);
$img->save($tmpThumb, 90);
// 4. Handle S3 Upload
if (env('FILESYSTEM_DISK') == 's3') {
Storage::disk('s3')->put($path_thumb, file_get_contents($absPathThumb));
// Remove local thumb after S3 upload
if (file_exists($absPathThumb)) {
unlink($absPathThumb);
// 3. Upload thumb to S3/MinIO
$thumbKey = $this->thumbFolder($type) . '/' . $thumbFilename;
if (env('FILESYSTEM_DISK') === 's3') {
Storage::disk('s3')->put($thumbKey, file_get_contents($tmpThumb));
} 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)) {
unlink($srcPath);
}
return $path_thumb;
return $thumbKey;
}
}

View File

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

View File

@@ -155,14 +155,7 @@ class Controller extends ExController
$this->authorize('delete', 'categories');
$category = Category::findOrFail($category);
if (is_file($category->image)) {
unlink($category->image);
}
// delete image from s3
if (env('FILESYSTEM_DISK') == 's3') {
Storage::disk('s3')->delete($category->image);
}
Storage::delete($category->image);
$category->delete();
$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\Update as UpdateJob;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
class Controller extends ExController
@@ -48,19 +47,8 @@ class Controller extends ExController
public function update(UpdateRequest $request, Partner $partner)
{
if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') {
// 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');
Storage::delete($partner->image);
$path = $request->file('image')->store('uploads/partners');
} else {
$path = $partner->image;
}
@@ -75,17 +63,7 @@ class Controller extends ExController
{
$this->authorize('delete', 'partners');
if (env('FILESYSTEM_DISK') == 's3') {
// delete old image from s3
Storage::disk('s3')->delete($partner->image);
} else {
$imagePath = public_path($partner->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
Storage::delete($partner->image);
$partner->delete();
$this->info(trans('admin.messages.deleted'));

View File

@@ -57,14 +57,8 @@ class Controller extends ExController
}
if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') {
Storage::disk('s3')->delete($post->image);
} else {
if (is_file($post->image)) {
unlink($post->image);
}
}
$path = $request->file('image')->store('uploads/posts');
Storage::delete($post->image);
$path = $request->file('image')->store('uploads/posts');
} else {
$path = $post->image;
}
@@ -122,14 +116,7 @@ class Controller extends ExController
public function delete(Post $post)
{
$this->authorize('delete', 'posts');
if (env('FILESYSTEM_DISK') == 's3') {
Storage::disk('s3')->delete($post->image);
} else {
if (is_file($post->image)) {
unlink($post->image);
}
}
Storage::delete($post->image);
$post->delete();
$this->info(trans('admin.messages.deleted'));
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\Models\Problem;
use App\Models\ServiceProblem;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
class Controller extends ExController
@@ -62,18 +61,8 @@ class Controller extends ExController
public function update(UpdateRequest $request, Service $service)
{
if ($request->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') {
$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');
Storage::delete($service->image);
$path = $request->file('image')->store('uploads/services');
} else {
$path = $service->image;
}
@@ -106,17 +95,7 @@ class Controller extends ExController
{
$this->authorize('delete', 'services');
if (env('FILESYSTEM_DISK') == 's3') {
$imagePath = $service->image;
Storage::disk('s3')->delete($imagePath);
} else {
$imagePath = public_path($service->image);
if (File::exists($imagePath)) {
File::delete($imagePath);
}
}
Storage::delete($service->image);
ServiceProblem::where('service_id', $service->id)->delete();
$service->delete();

View File

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

View File

@@ -43,20 +43,8 @@ class Update extends FormRequest
public function getImage(Category $category): string
{
if ($this->hasFile('image')) {
if (env('FILESYSTEM_DISK') == 's3') {
// delete old image from s3
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');
}
Storage::delete($category->image);
return (string) $this->file('image')->store('uploads/categories');
} else {
return 'null';
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,6 +9,14 @@ server {
root /quyoshli/public;
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
location / {
try_files $uri /$uri /index.php$is_args$args;