Fast Log add tenant

This commit is contained in:
Kekskurse 2021-01-21 01:37:38 +01:00
parent 484bcc236e
commit 018ff08cec
1 changed files with 7 additions and 72 deletions

View File

@ -2,12 +2,11 @@
namespace App\Http\Controllers;
use App\Jobs\ResizeImage;
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;
@ -15,9 +14,7 @@ use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use phpDocumentor\Reflection\Types\Integer;
class PublicController extends BaseController
{
@ -62,11 +59,11 @@ class PublicController extends BaseController
return view("themes.gallery.gallery-detail.list", ["gallery" => $gallery, "tenant" => $tenant, "images" => $images]);
}
public function returnWatermakeFile() {
public function returnWatermakeFile(\App\Helper\Image $imageHelper, \App\Helper\Access $accessHelper) {
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
$image = Image::query()->where("id", "=", $tenant->watermark)->firstOrFail();
$this->addAccessLog($tenant->id, null, $image->id, "Access", $image->size);
return Storage::disk($image->drive)->response($image->path);
$img = $imageHelper->getRawImage($tenant->watermark);
$accessHelper->addById($tenant->watermark, $img, \App\Helper\Access::TYPE_CACHE);
return $img->response();
}
public function returnImageFile($tenant_url, $gallery_url, $image_id, \App\Helper\Image $image, \App\Helper\Access $access, Request $request) {
@ -87,67 +84,5 @@ class PublicController extends BaseController
}
$access->addById($image_id, $img, $typ);
return $img->response();
return
$image = Image::query()->where("id", "=", $image_id)->firstOrFail();
$gallery = Gallery::query()->where("url", "=", $gallery_url)->firstOrFail();
$tenant = Tenant::query()->where("url", "=", $tenant_url)->firstOrFail();
if($image->gallery != $gallery->id) {
abort(404, "Gallery not match");
}
if($gallery->tenant != $tenant->id) {
abort(404, "Tenant not match");
}
$cacheName = "cache/".$tenant->url."_".$gallery->url."_".$image->id;
//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;
}
//Return from s3, takes a loooong time
ResizeImage::dispatchSync($image->id, $size);
$r = $this->tryReturnFromCache($cacheName."_".$size, $tenant->id, $gallery->id, $image->id);
if($r != null) {
return $r;
}
Log::error("Get Image failed", ["image_id" => $image->id]);
return abort(500, "Something go wrong");
}
private function tryReturnFromCache($cacheName, $tenant_id, $gallery_id, $image_id) {
$image = Image::query()->where("id", "=", $image_id)->firstOrFail();
if($image->refreshCache) {
return null;
}
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, $gallery, int $image, string $typ, int $size, $type = "general") {
$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();
}
}