Test
This commit is contained in:
parent
cdd6d483cd
commit
7c60cedfa2
1 changed files with 39 additions and 61 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Jobs\ResizeImage;
|
||||
use App\Models\Access;
|
||||
use App\Models\Gallery;
|
||||
use App\Models\Image;
|
||||
|
@ -24,6 +25,8 @@ class PublicController extends BaseController
|
|||
const SIZE_MEDIUM = 600;
|
||||
const SIZE_BIG = 1200;
|
||||
const SIZE_PIXEL = 100;
|
||||
|
||||
private $size = ["pixel" => 100, "small" => 300, "medium" => 600, "big" => 1200];
|
||||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
|
||||
public function listGalleriesView($name) {
|
||||
|
@ -40,79 +43,54 @@ class PublicController extends BaseController
|
|||
}
|
||||
|
||||
public function returnImageFile($tenant, $gallery, $image, Request $request) {
|
||||
$uid = uniqid();
|
||||
Log::info($uid.": Start", [$image]);
|
||||
$globalstart = microtime(true);
|
||||
$tenant = Tenant::query()->where("url", "=", $tenant)->firstOrFail();
|
||||
$gallery = Gallery::getByTenantAndUrl($tenant->id, $gallery);
|
||||
$image = Image::query()->where("gallery", "=", $gallery->id)->where("id", "=", $image)->firstOrFail();
|
||||
$size = $request->input("size", "medium");
|
||||
//$size = "medium";
|
||||
$sizeName = $request->input("size", "medium");
|
||||
if(array_key_exists($sizeName, $this->size)) {
|
||||
abort(400, "Size not exists");
|
||||
}
|
||||
$size = $this->size[$sizeName];
|
||||
|
||||
$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 (Storage::disk('cache')->exists($cacheName."_".$size)) {
|
||||
$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($gallery != $gallery->url) {
|
||||
abort(404, "Gallery not match");
|
||||
}
|
||||
|
||||
//If File exists in the needed format return
|
||||
$file = null;
|
||||
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]);
|
||||
if($tenant != $tenant->url) {
|
||||
abort(404, "Tenant not match");
|
||||
}
|
||||
|
||||
//Resize
|
||||
if($size == "small") {
|
||||
$start = microtime(true);
|
||||
$image = ImageResize::createFromString($file);
|
||||
$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");
|
||||
//Check if exsits in cache and return from it
|
||||
$r = $this->tryReturnFromCache($cacheName."_".$size, $tenant->id, $gallery->id, $image->id);
|
||||
if($r != null) {
|
||||
return $r;
|
||||
}
|
||||
|
||||
if($size == "big") {
|
||||
$start = microtime(true);
|
||||
$image = ImageResize::createFromString($file);
|
||||
$image->resizeToLongSide(self::SIZE_BIG);
|
||||
Storage::disk("cache")->put($cacheName."_big", $image->getImageAsString());
|
||||
$ende = microtime(true);
|
||||
Log::info("Dauer umwandlung big: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
|
||||
return Storage::disk('cache')->response($cacheName."_big");
|
||||
|
||||
//Return from s3, takes a loooong time
|
||||
$resizeJob = new ResizeImage($image->id, $size);
|
||||
ResizeImage::dispatchSync($resizeJob);
|
||||
|
||||
$r = $this->tryReturnFromCache($cacheName."_".$size, $tenant->id, $gallery->id, $image->id);
|
||||
if($r != null) {
|
||||
return $r;
|
||||
}
|
||||
|
||||
if($size == "pixel") {
|
||||
$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, "Something go wrong");
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
$access = new Access();
|
||||
$access->year = date("Y");
|
||||
|
|
Loading…
Reference in a new issue