2021-01-12 14:49:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-01-17 02:04:16 +00:00
|
|
|
use App\Jobs\ResizeImage;
|
2021-01-12 14:49:45 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2021-01-17 02:20:21 +00:00
|
|
|
public static $size = ["pixel" => 100, "small" => 300, "medium" => 600, "big" => 1200];
|
2021-01-17 02:04:16 +00:00
|
|
|
|
2021-01-12 14:49:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-17 02:07:52 +00:00
|
|
|
public function returnImageFile($tenant_url, $gallery_url, $image_id, Request $request) {
|
2021-01-17 02:04:16 +00:00
|
|
|
$sizeName = $request->input("size", "medium");
|
2021-01-17 02:39:33 +00:00
|
|
|
if(!array_key_exists($sizeName, self::$size)) {
|
2021-01-17 02:04:16 +00:00
|
|
|
abort(400, "Size not exists");
|
|
|
|
}
|
2021-01-17 02:40:00 +00:00
|
|
|
$size = (int)self::$size[$sizeName];
|
2021-01-12 14:49:45 +00:00
|
|
|
|
2021-01-17 02:07:52 +00:00
|
|
|
$image = Image::query()->where("id", "=", $image_id)->firstOrFail();
|
|
|
|
$gallery = Gallery::query()->where("url", "=", $gallery_url)->firstOrFail();
|
|
|
|
$tenant = Tenant::query()->where("url", "=", $tenant_url)->firstOrFail();
|
2021-01-17 02:08:41 +00:00
|
|
|
if($image->gallery != $gallery->id) {
|
2021-01-17 02:04:16 +00:00
|
|
|
abort(404, "Gallery not match");
|
2021-01-12 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 02:08:41 +00:00
|
|
|
if($gallery->tenant != $tenant->id) {
|
2021-01-17 02:04:16 +00:00
|
|
|
abort(404, "Tenant not match");
|
2021-01-12 14:49:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:50 +00:00
|
|
|
$cacheName = "cache/".$tenant->url."_".$gallery->url."_".$image->id;
|
|
|
|
|
2021-01-17 02:04:16 +00:00
|
|
|
//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;
|
2021-01-12 20:11:09 +00:00
|
|
|
}
|
2021-01-12 14:49:45 +00:00
|
|
|
|
|
|
|
|
2021-01-17 02:04:16 +00:00
|
|
|
//Return from s3, takes a loooong time
|
2021-01-17 02:13:59 +00:00
|
|
|
ResizeImage::dispatchSync($image->id, $size);
|
2021-01-17 02:04:16 +00:00
|
|
|
|
|
|
|
$r = $this->tryReturnFromCache($cacheName."_".$size, $tenant->id, $gallery->id, $image->id);
|
|
|
|
if($r != null) {
|
|
|
|
return $r;
|
2021-01-17 01:10:33 +00:00
|
|
|
}
|
2021-01-12 14:49:45 +00:00
|
|
|
|
2021-01-17 02:04:16 +00:00
|
|
|
return abort(500, "Something go wrong");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-01-12 14:49:45 +00:00
|
|
|
}
|
2021-01-17 02:04:16 +00:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|