kuvia/app/Http/Controllers/PublicController.php
2021-01-21 01:37:38 +01:00

88 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Gallery;
use App\Models\Image;
use App\Models\Tenant;
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\Auth;
class PublicController extends BaseController
{
public static $size = ["pixel" => 100, "small" => 300, "medium" => 600, "big" => 1200];
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function listGalleriesView($name) {
$tenant = Tenant::query()->where("url", "=", $name)->firstOrFail();
$galleries = Gallery::query()
->where("tenant", "=", $tenant->id)
->where("status", "=", "online")
->where("listed", "=", 1)
->orderByDesc("gallery_create_time")
->orderByDesc("created_at")
->get();
$parser = new \Parsedown();
foreach ($galleries as $gallery) {
$gallery->html = $parser->parse($gallery->description);
}
return view("themes.tenant.kuvia-blog.list", ["galleries" => $galleries, "tenant" => $tenant]);
}
public function listGalleryImagesView($tenant, $gallery) {
$tenant = Tenant::query()->where("url", "=", $tenant)->firstOrFail();
$gallery = Gallery::getByTenantAndUrl($tenant->id, $gallery);
if($gallery->status == "online" || (Auth::check() && $gallery->status == "preview" && Auth::user()->id == $tenant->owner)) {
$images = Image::query()
->where("gallery", "=", $gallery->id)
->whereNull("deleted_at")
->orderBy("rating")
->orderBy("DateTimeOriginal")
->get();
} else {
abort( 404);
}
$parser = new \Parsedown();
$gallery->html = $parser->parse($gallery->description);
return view("themes.gallery.gallery-detail.list", ["gallery" => $gallery, "tenant" => $tenant, "images" => $images]);
}
public function returnWatermakeFile(\App\Helper\Image $imageHelper, \App\Helper\Access $accessHelper) {
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
$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) {
$sizeName = $request->input("size", "medium");
if(!array_key_exists($sizeName, self::$size)) {
abort(400, "Size not exists");
}
$size = (int)self::$size[$sizeName];
$imageModel = Image::query()->where("id", "=", $image_id)->firstOrFail();
if(!$image->validateGalleryAndTenant($imageModel, $gallery_url, $tenant_url)) {
abort(404);
}
$img = $image->getImage($imageModel, $size);
$typ = \App\Helper\Access::TYPE_S3;
if($img->getDrive() == "cache") {
$typ = \App\Helper\Access::TYPE_CACHE;
}
$access->addById($image_id, $img, $typ);
return $img->response();
}
}