This commit is contained in:
Kekskurse 2021-01-17 03:04:16 +01:00
parent cdd6d483cd
commit 7c60cedfa2

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Jobs\ResizeImage;
use App\Models\Access; use App\Models\Access;
use App\Models\Gallery; use App\Models\Gallery;
use App\Models\Image; use App\Models\Image;
@ -24,6 +25,8 @@ class PublicController extends BaseController
const SIZE_MEDIUM = 600; const SIZE_MEDIUM = 600;
const SIZE_BIG = 1200; const SIZE_BIG = 1200;
const SIZE_PIXEL = 100; const SIZE_PIXEL = 100;
private $size = ["pixel" => 100, "small" => 300, "medium" => 600, "big" => 1200];
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function listGalleriesView($name) { public function listGalleriesView($name) {
@ -40,79 +43,54 @@ class PublicController extends BaseController
} }
public function returnImageFile($tenant, $gallery, $image, Request $request) { public function returnImageFile($tenant, $gallery, $image, Request $request) {
$uid = uniqid(); $sizeName = $request->input("size", "medium");
Log::info($uid.": Start", [$image]); if(array_key_exists($sizeName, $this->size)) {
$globalstart = microtime(true); abort(400, "Size not exists");
$tenant = Tenant::query()->where("url", "=", $tenant)->firstOrFail(); }
$gallery = Gallery::getByTenantAndUrl($tenant->id, $gallery); $size = $this->size[$sizeName];
$image = Image::query()->where("gallery", "=", $gallery->id)->where("id", "=", $image)->firstOrFail();
$size = $request->input("size", "medium");
//$size = "medium";
$cacheName = "cache/".$tenant->id."_".$gallery->id."_".$image->id; $image = Image::query()->where("id", "=", $image);
$gallery = Gallery::query()->where("id", "=", $gallery);
$tenant = Tenant::query()->where("id", "=", $tenant);
$cacheName = "cache/".$tenant->url."_".$gallery->url."_".$image->id;
//File exists in the right size if($gallery != $gallery->url) {
if (Storage::disk('cache')->exists($cacheName."_".$size)) { abort(404, "Gallery not match");
$this->addAccessLog($tenant->id, $gallery->id, $image->id, "Cache", Storage::disk('cache')->size($cacheName."_".$size));
Log::info($uid.": Return ".$size." from Cache, Gesamtzeit: ".(microtime(true)-$globalstart), [$image->id]);
return Storage::disk('cache')->response($cacheName."_".$size, null, ['Cache-Control'=> 'max-age='.(60*60*24).', public']);
} }
//If File exists in the needed format return if($tenant != $tenant->url) {
$file = null; abort(404, "Tenant not match");
if (Storage::disk('cache')->exists($cacheName."_orginal")) {
$file = Storage::disk("cache")->get($cacheName."_orginal");
} else {
Log::info("Get from S3");
$start = microtime(true);
$this->addAccessLog($tenant->id, $gallery->id, $image->id, "Access", $image->size);
$file = Storage::disk($image->driver)->get($image->path);
Storage::disk("cache")->put($cacheName."_orginal", $file);
Log::info("Dauer s3: ".(microtime(true) - $start), [$image->filename]);
} }
//Resize //Check if exsits in cache and return from it
if($size == "small") { $r = $this->tryReturnFromCache($cacheName."_".$size, $tenant->id, $gallery->id, $image->id);
$start = microtime(true); if($r != null) {
$image = ImageResize::createFromString($file); return $r;
$image->resizeToLongSide(self::SIZE_SMALL);
Storage::disk("cache")->put($cacheName."_small", $image->getImageAsString());
$ende = microtime(true);
Log::info("Dauer umwandlung small: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
return Storage::disk('cache')->response($cacheName."_small");
}
if($size == "medium") {
$start = microtime(true);
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_MEDIUM);
Storage::disk("cache")->put($cacheName."_medium", $image->getImageAsString());
$ende = microtime(true);
Log::info("Dauer umwandlung medium: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
return Storage::disk('cache')->response($cacheName."_medium");
} }
if($size == "big") {
$start = microtime(true); //Return from s3, takes a loooong time
$image = ImageResize::createFromString($file); $resizeJob = new ResizeImage($image->id, $size);
$image->resizeToLongSide(self::SIZE_BIG); ResizeImage::dispatchSync($resizeJob);
Storage::disk("cache")->put($cacheName."_big", $image->getImageAsString());
$ende = microtime(true); $r = $this->tryReturnFromCache($cacheName."_".$size, $tenant->id, $gallery->id, $image->id);
Log::info("Dauer umwandlung big: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart)); if($r != null) {
return Storage::disk('cache')->response($cacheName."_big"); return $r;
} }
if($size == "pixel") { return abort(500, "Something go wrong");
$start = microtime(true);
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_PIXEL);
Storage::disk("cache")->put($cacheName."_pixel", $image->getImageAsString());
$ende = microtime(true);
Log::info("Dauer umwandlung pixel: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
return Storage::disk('cache')->response($cacheName."_pixel");
}
return abort(500);
} }
private function tryReturnFromCache($cacheName, $tenant_id, $gallery_id, $image_id) {
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']);
}
return null;
}
private function addAccessLog(int $tenant, int $gallery, int $image, string $typ, int $size) { private function addAccessLog(int $tenant, int $gallery, int $image, string $typ, int $size) {
$access = new Access(); $access = new Access();
$access->year = date("Y"); $access->year = date("Y");