From 6ef747b0275ef076b7ae4c04cb92e14385e3e1a2 Mon Sep 17 00:00:00 2001 From: Kekskurse Date: Wed, 20 Jan 2021 22:35:41 +0100 Subject: [PATCH] Stuff --- app/Http/Controllers/PublicController.php | 5 ++++ app/Http/Controllers/TenantController.php | 22 ++++++++++++++ app/Jobs/ResizeImage.php | 5 +++- .../2021_01_20_211851_image_refresh_cache.php | 30 +++++++++++++++++++ resources/views/tenant/watermark.blade.php | 26 ++++++++++++++-- routes/web.php | 1 + 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2021_01_20_211851_image_refresh_cache.php 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') -

Watermark

+

Watermark for {{ $current_tenant->name }}

+

+

+ If you set/change/delete the Watermark it will effect your space and traffic in the following ways: + +
+ +

+

Update Watermark

@csrf
+ + @if (!is_null($current_tenant->watermark)) +

Delete Watermark

+ Delete Watermark + @endif +
+
+ Current Watermark:
+ @if (!is_null($current_tenant->watermark)) + + @endif
-
Current Watermark:
@if ($errors->any())
diff --git a/routes/web.php b/routes/web.php index 6b952fa..a23a4d1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -48,6 +48,7 @@ Route::middleware([\App\Http\Middleware\TenanMiddleware::class])->group(function Route::get("/s/watermark", [\App\Http\Controllers\TenantController::class, 'watermarkView']); Route::post("/s/watermark", [\App\Http\Controllers\TenantController::class, 'watermarkSave']); Route::get("/s/watermark/image", [\App\Http\Controllers\PublicController::class, "returnWatermakeFile"]); + Route::get("/s/watermark/delete", [\App\Http\Controllers\TenantController::class, "deleteWatermark"]); }); Route::get("/{name}", [\App\Http\Controllers\PublicController::class, 'listGalleriesView'])->middleware([\App\Http\Middleware\TenanMiddleware::class]);