kuvia/app/Http/Controllers/PublicController.php

162 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2021-01-12 14:49:45 +00:00
<?php
namespace App\Http\Controllers;
2021-01-21 00:37:38 +00:00
2021-01-12 14:49:45 +00:00
use App\Models\Gallery;
use App\Models\Image;
use App\Models\Tenant;
2021-01-21 00:37:38 +00:00
2021-01-12 14:49:45 +00:00
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;
2021-01-19 22:32:50 +00:00
use Illuminate\Support\Facades\Auth;
2021-01-21 00:37:38 +00:00
2021-01-12 14:49:45 +00:00
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;
2021-02-08 00:15:18 +00:00
public function listGalleriesViewAPI($name, Request $request) {
$data = [];
$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);
}
2021-02-08 10:10:05 +00:00
$limit = $request->input("limit", 10);
2021-02-08 00:20:45 +00:00
$g = collect($galleries);
2021-02-08 00:27:32 +00:00
$page = $request->input("page", 1);
2021-02-08 10:10:05 +00:00
$r = $g->forPage($page, $limit);
2021-02-08 00:27:06 +00:00
2021-02-08 00:38:51 +00:00
$next = null;
$preview = null;
if($page > 1) {
$preview = url("/".$tenant->url.".json?page=".($page-1));
}
2021-02-08 10:10:05 +00:00
if(($g->count() / $limit) > $page) {
2021-02-08 00:39:07 +00:00
$next = url("/".$tenant->url.".json?page=".($page+1));
2021-02-08 00:38:51 +00:00
}
2021-02-08 00:27:06 +00:00
return response()->json(["pagination" => [
2021-02-08 00:28:06 +00:00
"total" => $g->count(),
2021-02-08 10:10:57 +00:00
"items_per_page" => (int)$limit,
2021-02-08 10:10:40 +00:00
"pages" => (int)($g->count() / $limit),
2021-02-08 00:38:51 +00:00
"preview" => $preview,
2021-02-08 00:39:07 +00:00
"next" => $next],
2021-02-08 10:00:42 +00:00
"data" => [
"galleries" => \App\Http\Resources\Gallery::collection($r)
]]);
2021-02-08 00:15:18 +00:00
}
2021-01-21 17:56:30 +00:00
public function listGalleriesView($name, Request $request) {
2021-01-12 14:49:45 +00:00
$tenant = Tenant::query()->where("url", "=", $name)->firstOrFail();
2021-01-19 22:30:21 +00:00
$galleries = Gallery::query()
->where("tenant", "=", $tenant->id)
->where("status", "=", "online")
->where("listed", "=", 1)
->orderByDesc("gallery_create_time")
->orderByDesc("created_at")
->get();
2021-01-17 16:28:53 +00:00
$parser = new \Parsedown();
foreach ($galleries as $gallery) {
$gallery->html = $parser->parse($gallery->description);
}
2021-01-21 17:56:30 +00:00
2021-01-27 00:38:17 +00:00
$theme = $tenant->template;
2021-01-21 17:56:30 +00:00
if($request->input("theme", false) && $tenant->owner == Auth::id()) {
$theme = $request->input("theme");
}
return view("themes.tenant.".$theme.".list", ["galleries" => $galleries, "tenant" => $tenant]);
2021-01-12 14:49:45 +00:00
}
2021-02-08 10:00:42 +00:00
public function listGalleryImagesViewAPI($tenant, $gallery, Request $request) {
$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 response()->json(["data" => ["gallery" => new \App\Http\Resources\Gallery($gallery), "images" => \App\Http\Resources\Image::collection(collect(($images)))]]);
return view("themes.gallery.".$theme.".list", ["gallery" => $gallery, "tenant" => $tenant, "images" => $images]);
}
2021-01-27 00:38:17 +00:00
public function listGalleryImagesView($tenant, $gallery, Request $request) {
2021-01-12 14:49:45 +00:00
$tenant = Tenant::query()->where("url", "=", $tenant)->firstOrFail();
$gallery = Gallery::getByTenantAndUrl($tenant->id, $gallery);
2021-01-19 22:33:41 +00:00
if($gallery->status == "online" || (Auth::check() && $gallery->status == "preview" && Auth::user()->id == $tenant->owner)) {
2021-01-19 23:12:50 +00:00
$images = Image::query()
->where("gallery", "=", $gallery->id)
->whereNull("deleted_at")
->orderBy("rating")
->orderBy("DateTimeOriginal")
->get();
2021-01-19 22:32:50 +00:00
} else {
abort( 404);
}
2021-01-20 00:17:57 +00:00
$parser = new \Parsedown();
2021-01-20 00:17:34 +00:00
$gallery->html = $parser->parse($gallery->description);
2021-01-27 00:38:17 +00:00
$theme = $tenant->gallery_default_theme;
if($request->input("theme", false) && $tenant->owner == Auth::id()) {
$theme = $request->input("theme");
}
return view("themes.gallery.".$theme.".list", ["gallery" => $gallery, "tenant" => $tenant, "images" => $images]);
2021-01-12 14:49:45 +00:00
}
2021-01-21 00:37:38 +00:00
public function returnWatermakeFile(\App\Helper\Image $imageHelper, \App\Helper\Access $accessHelper) {
2021-01-20 20:43:58 +00:00
$tenant = Tenant::query()->where("id", "=", session("current_tenant_id"))->firstOrFail();
2021-01-21 00:40:38 +00:00
$imageModel = Image::query()->where("id", "=", $tenant->watermark)->firstOrFail();
$img = $imageHelper->getRawImage($imageModel);
$accessHelper->add($imageModel, $img, \App\Helper\Access::TYPE_CACHE);
2021-01-21 00:37:38 +00:00
return $img->response();
2021-01-20 20:43:58 +00:00
}
2021-01-21 00:13:41 +00:00
public function returnImageFile($tenant_url, $gallery_url, $image_id, \App\Helper\Image $image, \App\Helper\Access $access, 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-21 00:13:41 +00:00
$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();
2021-01-12 14:49:45 +00:00
}
}