kuvia/app/Http/Controllers/PublicController.php

122 lines
5.3 KiB
PHP
Raw Normal View History

2021-01-12 14:49:45 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\Access;
use App\Models\Gallery;
use App\Models\Image;
use App\Models\Tenant;
use Gumlet\ImageResize;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use phpDocumentor\Reflection\Types\Integer;
class PublicController extends BaseController
{
const SIZE_SMALL = 300;
const SIZE_MEDIUM = 600;
const SIZE_BIG = 1200;
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function listGalleriesView($name) {
$tenant = Tenant::query()->where("url", "=", $name)->firstOrFail();
$galleries = Gallery::query()->where("tenant", "=", $tenant->id)->get();
2021-01-12 22:14:38 +00:00
return view("themes.tenant.kuvia-blog.list", ["galleries" => $galleries, "tenant" => $tenant]);
2021-01-12 14:49:45 +00:00
}
public function listGalleryImagesView($tenant, $gallery) {
$tenant = Tenant::query()->where("url", "=", $tenant)->firstOrFail();
$gallery = Gallery::getByTenantAndUrl($tenant->id, $gallery);
$images = Image::query()->where("gallery", "=", $gallery->id)->get();
2021-01-12 22:35:21 +00:00
return view("themes.gallery.gallery.list", ["gallery" => $gallery, "tenant" => $tenant, "images" => $images]);
2021-01-12 14:49:45 +00:00
}
public function returnImageFile($tenant, $gallery, $image, Request $request) {
2021-01-13 17:16:57 +00:00
$globalstart = microtime(true);
Log::info("Getting Image start", ["time" => microtime(true)]);
2021-01-12 14:49:45 +00:00
$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");
$cacheName = "cache_".$tenant->id."_".$gallery->id."_".$image->id;
//File exists in the right size
if (Storage::disk('cache')->exists($cacheName."_".$size)) {
Log::info("Get from Cache");
$this->addAccessLog($tenant->id, $gallery->id, $image->id, "Cache", Storage::disk('cache')->size($cacheName."_".$size));
2021-01-12 18:26:00 +00:00
return Storage::disk('cache')->response($cacheName."_".$size, null, ['Cache-Control'=> 'max-age='.(60*60*24).', public']);
2021-01-12 14:49:45 +00:00
}
2021-01-13 17:26:42 +00:00
//If File exists in the needed format return
2021-01-12 14:49:45 +00:00
$file = null;
2021-01-12 20:17:21 +00:00
if (Storage::disk('cache')->exists($cacheName."_".$size)) {
2021-01-12 14:49:45 +00:00
$file = Storage::disk("cache")->get($cacheName."_orginal");
$this->addAccessLog($tenant->id, $gallery->id, $image->id, "Cache", $image->size);
2021-01-13 17:26:42 +00:00
Log::info("Return from Cache, Gesamtzeit: ".(microtime(true)-$globalstart));
2021-01-12 14:49:45 +00:00
return Storage::disk('cache')->response($cacheName."_".$size);
} else {
Log::info("Get from S3");
2021-01-13 17:18:59 +00:00
$start = microtime(true);
2021-01-12 14:49:45 +00:00
$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);
2021-01-13 17:19:48 +00:00
Log::info("Dauer s3: ".(microtime(true) - $start));
2021-01-12 14:49:45 +00:00
}
//Resize
2021-01-12 20:11:09 +00:00
if($size == "small") {
2021-01-13 17:23:19 +00:00
$start = microtime(true);
2021-01-12 20:11:09 +00:00
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_SMALL);
Storage::disk("cache")->put($cacheName."_small", $image->getImageAsString());
2021-01-13 17:23:19 +00:00
$ende = microtime(true);
2021-01-13 17:24:34 +00:00
Log::info("Dauer umwandlung small: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
2021-01-12 20:11:09 +00:00
return Storage::disk('cache')->response($cacheName."_small");
}
if($size == "medium") {
2021-01-13 17:16:57 +00:00
$start = microtime(true);
2021-01-12 20:11:09 +00:00
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_MEDIUM);
Storage::disk("cache")->put($cacheName."_medium", $image->getImageAsString());
2021-01-13 17:16:57 +00:00
$ende = microtime(true);
2021-01-13 17:24:34 +00:00
Log::info("Dauer umwandlung medium: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
2021-01-12 20:11:09 +00:00
return Storage::disk('cache')->response($cacheName."_medium");
}
2021-01-12 14:49:45 +00:00
2021-01-12 20:11:09 +00:00
if($size == "big") {
2021-01-13 17:23:19 +00:00
$start = microtime(true);
2021-01-12 20:11:09 +00:00
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_BIG);
Storage::disk("cache")->put($cacheName."_big", $image->getImageAsString());
2021-01-13 17:23:19 +00:00
$ende = microtime(true);
2021-01-13 17:24:34 +00:00
Log::info("Dauer umwandlung big: ".($ende-$start). "/" . "Gesamtzeit: ".(microtime(true)-$globalstart));
2021-01-12 20:11:09 +00:00
return Storage::disk('cache')->response($cacheName."_big");
}
2021-01-12 14:49:45 +00:00
2021-01-12 20:11:09 +00:00
return abort(500);
2021-01-12 14:49:45 +00:00
}
private function addAccessLog(int $tenant, int $gallery, int $image, string $typ, int $size) {
$access = new Access();
$access->year = date("Y");
$access->month = date("m");
$access->day = date("d");
$access->hour = date("H");
$access->image = $image;
$access->gallery = $gallery;
$access->tenant = $tenant;
$access->typ = $typ;
$access->size = $size;
$access->saveOrFail();
}
}