diff --git a/app/Http/Controllers/PublicController.php b/app/Http/Controllers/PublicController.php index 39de3b3..4ec7dfc 100644 --- a/app/Http/Controllers/PublicController.php +++ b/app/Http/Controllers/PublicController.php @@ -104,11 +104,16 @@ class PublicController extends BaseController return $r; } + Log::error("Get Image failed", ["image_id" => $image->id]); return abort(500, "Something go wrong"); } private function tryReturnFromCache($cacheName, $tenant_id, $gallery_id, $image_id) { + $image = Image::query()->where("id", "=", $image_id)->firstOrFail(); + if($image->refreshCache) { + return null; + } if(Storage::disk('cache')->exists($cacheName)) { $this->addAccessLog($tenant_id, $gallery_id, $image_id, "Cache", Storage::disk('cache')->size($cacheName)); return Storage::disk('cache')->response($cacheName, null, ['Cache-Control'=> 'max-age='.(60*60*24).', public']); diff --git a/app/Http/Controllers/TenantController.php b/app/Http/Controllers/TenantController.php index 6394f9b..acbb007 100644 --- a/app/Http/Controllers/TenantController.php +++ b/app/Http/Controllers/TenantController.php @@ -10,6 +10,7 @@ use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Storage; @@ -59,6 +60,23 @@ class TenantController extends BaseController return view("tenant.watermark"); } + public function deleteWatermark() { + DB::beginTransaction(); + $tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail(); + $oldImage = Image::query()->where("id", "=", $tenant->watermark)->first(); + if(!is_null($oldImage)) { + Storage::disk($oldImage->driver)->delete($oldImage->path); + $oldImage->deleted_at = date("Y-m-d H:i:s"); + $oldImage->saveOrFail(); + } + $tenant->watermark = null; + $tenant->saveOrFail(); + + DB::statement("UPDATE images SET refreshCache = 1 WHERE gallery IN (SELECT `id` FROM `galleries` WHERE `tenant` = 1);"); + DB::commit(); + return \redirect("/s/watermark"); + } + public function watermarkSave(Request $request) { $validated = $request->validate([ 'files.0' => 'required|image' @@ -88,6 +106,10 @@ class TenantController extends BaseController $tenant->watermark = $image->id; $tenant->saveOrFail(); + DB::beginTransaction(); + DB::statement("UPDATE images SET refreshCache = 1 WHERE gallery IN (SELECT `id` FROM `galleries` WHERE `tenant` = 1);"); + DB::commit(); + return \redirect("/s/watermark"); } } diff --git a/app/Jobs/ResizeImage.php b/app/Jobs/ResizeImage.php index fee017c..f25da24 100644 --- a/app/Jobs/ResizeImage.php +++ b/app/Jobs/ResizeImage.php @@ -112,7 +112,10 @@ class ResizeImage implements ShouldQueue $img->save($tmpfname); + Log::info("Update cache for Image", ["id" => $image->id, "size" => $this->size]); Storage::disk("cache")->put($cacheName."_".$this->size, file_get_contents($tmpfname)); + $image->refreshCache = false; + $image->saveOrFail(); unlink($tmpfname); //Watermark @@ -120,7 +123,7 @@ class ResizeImage implements ShouldQueue } private function getImage(Tenant $tenant, Gallery $gallery, Image $image, $cacheName) { - if (Storage::disk('cache')->exists($cacheName."_orginal")) { + if (Storage::disk('cache')->exists($cacheName."_orginal") && $image->refreshCache == false) { $file = Storage::disk("cache")->get($cacheName."_orginal"); } else { $this->addAccessLog($tenant->id, $gallery->id, $image->id, "Access", $image->size); diff --git a/database/migrations/2021_01_20_211851_image_refresh_cache.php b/database/migrations/2021_01_20_211851_image_refresh_cache.php new file mode 100644 index 0000000..57852ea --- /dev/null +++ b/database/migrations/2021_01_20_211851_image_refresh_cache.php @@ -0,0 +1,30 @@ +boolean("refreshCache")->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/tenant/watermark.blade.php b/resources/views/tenant/watermark.blade.php index 9bd91a3..6bb2458 100644 --- a/resources/views/tenant/watermark.blade.php +++ b/resources/views/tenant/watermark.blade.php @@ -1,16 +1,38 @@ @extends('layout/template') @section('content') -
+