This commit is contained in:
Kekskurse 2021-01-20 22:35:41 +01:00
parent 49ac7f48e0
commit 6ef747b027
6 changed files with 86 additions and 3 deletions

View file

@ -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']);

View file

@ -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");
}
}

View file

@ -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);

View file

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ImageRefreshCache extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table("images", function (Blueprint $table) {
$table->boolean("refreshCache")->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View file

@ -1,16 +1,38 @@
@extends('layout/template')
@section('content')
<h1>Watermark</h1>
<h1>Watermark for {{ $current_tenant->name }}</h1>
<p>
<div class="alert alert-info">
If you set/change/delete the Watermark it will effect your space and traffic in the following ways:
<ul>
<li>Your Space will increase by the size of the Watermark</li>
<li>All cached Images need to be regenerate that will increase your traffic (one time)</li>
<li>If you add new Images the Watermark need to be downloaded, that will increase your traffic (each time)</li>
</ul>
</div>
</p>
<div class="row">
<div class="col-md-6">
<h3>Update Watermark</h3>
<form method="post" enctype="multipart/form-data">
@csrf
<input type="file" name="files[]" class="form-control" >
<input type="submit" value="Upload Watermark" class="btn btn-outline-success">
</form>
@if (!is_null($current_tenant->watermark))
<h3>Delete Watermark</h3>
<a href="/s/watermark/delete" class="btn btn-outline-danger">Delete Watermark</a>
@endif
</div>
<div class="col-md-6">
Current Watermark:<br>
@if (!is_null($current_tenant->watermark))
<img src="/s/watermark/image" style="max-width: 100%;">
@endif
</div>
<div class="col-md-6">Current Watermark:<br><img src="/s/watermark/image" style="max-width: 100%;"></div>
</div>
@if ($errors->any())
<div class="alert alert-danger">

View file

@ -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]);