openvk/Web/Models/Entities/Media.php

151 lines
4.6 KiB
PHP
Raw Normal View History

2020-06-07 19:04:43 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use Nette\InvalidStateException as ISE;
abstract class Media extends Postable
{
protected $fileExtension = "oct"; #octet stream xddd
protected $upperNodeReferenceColumnName = "owner";
2022-04-12 22:01:12 +03:00
protected $processingPlaceholder = NULL;
protected $processingTime = 30;
2020-06-07 19:04:43 +03:00
function __destruct()
{
#Remove data, if model wasn't presisted
if(isset($this->changes["hash"]))
unlink($this->pathFromHash($this->changes["hash"]));
}
2020-06-23 23:04:17 +03:00
protected function getBaseDir(): string
2020-06-07 19:04:43 +03:00
{
$uploadSettings = OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"];
if($uploadSettings["mode"] === "server" && $uploadSettings["server"]["kind"] === "cdn")
return $uploadSettings["server"]["directory"];
else
return OPENVK_ROOT . "/storage/";
}
2022-04-12 22:01:12 +03:00
protected function checkIfFileIsProcessed(): bool
{
throw new \LogicException("checkIfFileIsProcessed is not implemented");
}
2020-06-07 19:04:43 +03:00
abstract protected function saveFile(string $filename, string $hash): bool;
protected function pathFromHash(string $hash): string
{
$dir = $this->getBaseDir() . substr($hash, 0, 2);
if(!is_dir($dir))
mkdir($dir);
return "$dir/$hash." . $this->fileExtension;
}
function getFileName(): string
{
return $this->pathFromHash($this->getRecord()->hash);
}
function getURL(): string
{
2022-04-12 22:01:12 +03:00
if(!is_null($this->processingPlaceholder))
if(!$this->isProcessed())
return "/assets/packages/static/openvk/$this->processingPlaceholder.$this->fileExtension";
2020-06-07 19:04:43 +03:00
$hash = $this->getRecord()->hash;
switch(OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["mode"]) {
default:
2020-06-07 19:04:43 +03:00
case "default":
case "basic":
2020-06-07 19:04:43 +03:00
return "http://" . $_SERVER['HTTP_HOST'] . "/blob_" . substr($hash, 0, 2) . "/$hash.$this->fileExtension";
break;
case "accelerated":
return "http://" . $_SERVER['HTTP_HOST'] . "/openvk-datastore/$hash.$this->fileExtension";
break;
case "server":
$settings = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["server"];
return (
$settings->protocol ?? ovk_scheme() .
2020-06-07 19:04:43 +03:00
"://" . $settings->host .
$settings->path .
substr($hash, 0, 2) . "/$hash.$this->fileExtension"
);
break;
}
}
function getDescription(): ?string
{
return $this->getRecord()->description;
}
2022-04-12 22:01:12 +03:00
protected function isProcessed(): bool
{
if(is_null($this->processingPlaceholder))
return true;
if($this->getRecord()->processed)
return true;
$timeDiff = time() - $this->getRecord()->last_checked;
if($timeDiff < $this->processingTime)
return false;
$res = $this->checkIfFileIsProcessed();
$this->stateChanges("last_checked", time());
$this->stateChanges("processed", $res);
$this->save();
return $res;
}
2020-06-07 19:04:43 +03:00
function isDeleted(): bool
{
return (bool) $this->getRecord()->deleted;
}
function setHash(string $hash): void
{
throw new ISE("Setting file hash manually is forbidden");
}
function setFile(array $file): void
{
if($file["error"] !== UPLOAD_ERR_OK)
throw new ISE("File uploaded is corrupted");
$hash = hash_file("whirlpool", $file["tmp_name"]);
$this->saveFile($file["tmp_name"], $hash);
$this->stateChanges("hash", $hash);
}
2022-04-12 22:01:12 +03:00
[WIP] Textarea: Upload multiple pictures (#800) * VKAPI: Fix bug when DELETED user appear if there is no user_ids * Textarea: Make multiple attachments * постмодернистское искусство * Use only attachPic for grabbing pic attachments TODO throw flashFail on bruh moment with pic attachments * draft masonry picture layout in posts xddd где мои опиаты??? * fix funny typos in computeMasonryLayout * Fix video bruh moment in textarea * Posts: add multiple kakahi for microblog * Photo: Add minimal implementation of миниатюра открывашка Co-authored-by: Daniel <60743585+myslivets@users.noreply.github.com> * Photo: Add ability to slide trough photos in one post This also gives ability to easily implement comments and actions * Photo: The Fxck Is This implementation of comments under photo in viewer * FloatingPhotoViewer: Better CSS - Fix that details background issue - Make slide buttons slightly shorter by height * FloatingPhotoViewer: Refactor, and make it better - Now you can actually check the comments under EVERY photo - Fix for textarea. Now you can publish comments * Fix funny typos xddd * Kinda fix poll display in non-microblog posts * Posts: Fix poll display in microblog posts * Add photos picker (#986) * early implementation of photos pickir Добавлен пикер фоточек и быстрая загрузка фото. Так же пофикшен просмотрщик фото в группах. Но, правда, я сломал копипейст, но это ладн. * Fiks fotos viver four coments. * Add picking photos from clubs albums Копипейст и граффити так и не пофикшены * Fix graffiti and copypaste Какого-то хуя копипаста у постов срабатывает два раза. * some fixesx * dragon drop * Fix PHP 8 compatibility * 5 (#988) --------- Co-authored-by: celestora <kitsuruko@gmail.com> Co-authored-by: Daniel <60743585+myslivets@users.noreply.github.com> Co-authored-by: lalka2016 <99399973+lalka2016@users.noreply.github.com> Co-authored-by: Alexander Minkin <weryskok@gmail.com>
2023-10-03 19:40:13 +03:00
function save(?bool $log = false): void
2022-04-12 22:01:12 +03:00
{
if(!is_null($this->processingPlaceholder) && is_null($this->getRecord())) {
2022-04-12 22:01:12 +03:00
$this->stateChanges("processed", 0);
$this->stateChanges("last_checked", time());
}
[WIP] Textarea: Upload multiple pictures (#800) * VKAPI: Fix bug when DELETED user appear if there is no user_ids * Textarea: Make multiple attachments * постмодернистское искусство * Use only attachPic for grabbing pic attachments TODO throw flashFail on bruh moment with pic attachments * draft masonry picture layout in posts xddd где мои опиаты??? * fix funny typos in computeMasonryLayout * Fix video bruh moment in textarea * Posts: add multiple kakahi for microblog * Photo: Add minimal implementation of миниатюра открывашка Co-authored-by: Daniel <60743585+myslivets@users.noreply.github.com> * Photo: Add ability to slide trough photos in one post This also gives ability to easily implement comments and actions * Photo: The Fxck Is This implementation of comments under photo in viewer * FloatingPhotoViewer: Better CSS - Fix that details background issue - Make slide buttons slightly shorter by height * FloatingPhotoViewer: Refactor, and make it better - Now you can actually check the comments under EVERY photo - Fix for textarea. Now you can publish comments * Fix funny typos xddd * Kinda fix poll display in non-microblog posts * Posts: Fix poll display in microblog posts * Add photos picker (#986) * early implementation of photos pickir Добавлен пикер фоточек и быстрая загрузка фото. Так же пофикшен просмотрщик фото в группах. Но, правда, я сломал копипейст, но это ладн. * Fiks fotos viver four coments. * Add picking photos from clubs albums Копипейст и граффити так и не пофикшены * Fix graffiti and copypaste Какого-то хуя копипаста у постов срабатывает два раза. * some fixesx * dragon drop * Fix PHP 8 compatibility * 5 (#988) --------- Co-authored-by: celestora <kitsuruko@gmail.com> Co-authored-by: Daniel <60743585+myslivets@users.noreply.github.com> Co-authored-by: lalka2016 <99399973+lalka2016@users.noreply.github.com> Co-authored-by: Alexander Minkin <weryskok@gmail.com>
2023-10-03 19:40:13 +03:00
parent::save($log);
2022-04-12 22:01:12 +03:00
}
function delete(bool $softly = true): void
{
$deleteQuirk = ovkGetQuirk("blobs.erase-upon-deletion");
if($deleteQuirk === 2 || ($deleteQuirk === 1 && !$softly))
@unlink($this->getFileName());
parent::delete($softly);
}
function undelete(): void
{
if(ovkGetQuirk("blobs.erase-upon-deletion") === 2)
throw new \LogicException("Can't undelete model which is tied to blob, because of config constraint (quriks.yml:blobs.erase-upon-deletion)");
parent::undelete();
}
2020-06-07 19:04:43 +03:00
}