kuvia/app/Http/Controllers/PublicController.php

109 lines
4.5 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 21:46:12 +00:00
return view("themes.gallery.kuvia-gallery.list", ["gallery" => $gallery, "tenant" => $tenant, "images" => $images]);
2021-01-12 14:49:45 +00:00
}
public function returnImageFile($tenant, $gallery, $image, Request $request) {
$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
}
//ORginal File exists need to be resized
$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
Log::info("Get orginal size from Cache");
$file = Storage::disk("cache")->get($cacheName."_orginal");
$this->addAccessLog($tenant->id, $gallery->id, $image->id, "Cache", $image->size);
return Storage::disk('cache')->response($cacheName."_".$size);
} else {
Log::info("Get from S3");
$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);
}
//Resize
2021-01-12 20:11:09 +00:00
if($size == "small") {
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_SMALL);
Storage::disk("cache")->put($cacheName."_small", $image->getImageAsString());
return Storage::disk('cache')->response($cacheName."_small");
}
if($size == "medium") {
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_MEDIUM);
Storage::disk("cache")->put($cacheName."_medium", $image->getImageAsString());
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") {
$image = ImageResize::createFromString($file);
$image->resizeToLongSide(self::SIZE_BIG);
Storage::disk("cache")->put($cacheName."_big", $image->getImageAsString());
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();
}
}