[DB][FILE][AVATAR] Handle deleting files, change file and avatar tables
This commit is contained in:
parent
5a68fd287b
commit
4d99bfb9fd
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use App\Core\DB\DB;
|
||||||
|
use App\Core\Entity;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,15 +37,15 @@ use DateTimeInterface;
|
||||||
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
class Avatar
|
class Avatar extends Entity
|
||||||
{
|
{
|
||||||
// {{{ Autocode
|
// {{{ Autocode
|
||||||
|
|
||||||
private int $profile_id;
|
private int $profile_id;
|
||||||
private ?bool $is_original;
|
|
||||||
private int $width;
|
private int $width;
|
||||||
private int $height;
|
private int $height;
|
||||||
private string $mediatype;
|
private ?bool $is_original;
|
||||||
|
private int $file_id;
|
||||||
private \DateTimeInterface $created;
|
private \DateTimeInterface $created;
|
||||||
private \DateTimeInterface $modified;
|
private \DateTimeInterface $modified;
|
||||||
|
|
||||||
|
@ -57,16 +59,6 @@ class Avatar
|
||||||
return $this->profile_id;
|
return $this->profile_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setIsOriginal(?bool $is_original): self
|
|
||||||
{
|
|
||||||
$this->is_original = $is_original;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getIsOriginal(): ?bool
|
|
||||||
{
|
|
||||||
return $this->is_original;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setWidth(int $width): self
|
public function setWidth(int $width): self
|
||||||
{
|
{
|
||||||
$this->width = $width;
|
$this->width = $width;
|
||||||
|
@ -87,14 +79,24 @@ class Avatar
|
||||||
return $this->height;
|
return $this->height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setMediatype(string $mediatype): self
|
public function setIsOriginal(?bool $is_original): self
|
||||||
{
|
{
|
||||||
$this->mediatype = $mediatype;
|
$this->is_original = $is_original;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function getMediatype(): string
|
public function getIsOriginal(): ?bool
|
||||||
{
|
{
|
||||||
return $this->mediatype;
|
return $this->is_original;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFileId(int $file_id): self
|
||||||
|
{
|
||||||
|
$this->file_id = $file_id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function getFileId(): int
|
||||||
|
{
|
||||||
|
return $this->file_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCreated(DateTimeInterface $created): self
|
public function setCreated(DateTimeInterface $created): self
|
||||||
|
@ -119,25 +121,59 @@ class Avatar
|
||||||
|
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
|
private ?File $file = null;
|
||||||
|
|
||||||
|
public function getFile(): File
|
||||||
|
{
|
||||||
|
$this->file = $this->file ?: DB::find('file', ['id' => $this->file_id]);
|
||||||
|
return $this->file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilePath(): string
|
||||||
|
{
|
||||||
|
$file_name = $this->getFile()->getFileName();
|
||||||
|
if ($this->is_original) {
|
||||||
|
return Common::config('avatar', 'dir') . '/' . $file_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete this avatar and the corresponding file and thumbnails, which this owns
|
||||||
|
*/
|
||||||
|
public function delete(bool $flush = false, bool $delete_files_now = false, bool $cascading = false): array
|
||||||
|
{
|
||||||
|
// Don't go into a loop if we're deleting from File
|
||||||
|
if (!$cascading) {
|
||||||
|
$files = $this->getFile()->delete($cascade = true, $file_flush = false, $delete_files_now);
|
||||||
|
} else {
|
||||||
|
DB::remove(DB::getReference('avatar', ['profile_id' => $this->profile_id, 'width' => $this->width, 'height' => $this->height]));
|
||||||
|
$file_path = $this->getFilePath();
|
||||||
|
$files[] = $file_path;
|
||||||
|
if ($flush) {
|
||||||
|
DB::flush();
|
||||||
|
}
|
||||||
|
return $delete_files_now ? [] : $files;
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
public static function schemaDef(): array
|
public static function schemaDef(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'avatar',
|
'name' => 'avatar',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'profile_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'],
|
'profile_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'],
|
||||||
'is_original' => ['type' => 'bool', 'default' => false, 'description' => 'uploaded by user or generated?'],
|
'file_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to file table'],
|
||||||
'width' => ['type' => 'int', 'not null' => true, 'description' => 'image width'],
|
'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
|
||||||
'height' => ['type' => 'int', 'not null' => true, 'description' => 'image height'],
|
'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
|
||||||
'mediatype' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'],
|
|
||||||
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
|
|
||||||
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
|
||||||
],
|
],
|
||||||
'primary key' => ['profile_id', 'width', 'height'],
|
'primary key' => ['profile_id'],
|
||||||
'foreign keys' => [
|
'foreign keys' => [
|
||||||
'avatar_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
|
'avatar_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
|
||||||
|
'avatar_file_id_fkey' => ['file', ['file_id' => 'id']],
|
||||||
],
|
],
|
||||||
'indexes' => [
|
'indexes' => [
|
||||||
'avatar_profile_id_idx' => ['profile_id'],
|
'avatar_file_id_idx' => ['file_id'],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use App\Core\Entity;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,22 +36,20 @@ use DateTimeInterface;
|
||||||
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
class File
|
class File extends Entity
|
||||||
{
|
{
|
||||||
// {{{ Autocode
|
// {{{ Autocode
|
||||||
|
|
||||||
private int $id;
|
private int $id;
|
||||||
private ?string $url;
|
private ?string $url;
|
||||||
private ?bool $is_url_protected;
|
private ?bool $is_url_protected;
|
||||||
private string $url_hash;
|
private ?string $url_hash;
|
||||||
private ?string $file_hash;
|
private ?string $file_hash;
|
||||||
private ?string $mimetype;
|
private ?string $mimetype;
|
||||||
private ?int $size;
|
private ?int $size;
|
||||||
private ?string $title;
|
private ?string $title;
|
||||||
private ?int $timestamp;
|
private ?int $timestamp;
|
||||||
private ?bool $is_local;
|
private ?bool $is_local;
|
||||||
private ?int $width;
|
|
||||||
private ?int $height;
|
|
||||||
private \DateTimeInterface $modified;
|
private \DateTimeInterface $modified;
|
||||||
|
|
||||||
public function setId(int $id): self
|
public function setId(int $id): self
|
||||||
|
@ -83,12 +82,12 @@ class File
|
||||||
return $this->is_url_protected;
|
return $this->is_url_protected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUrlHash(string $url_hash): self
|
public function setUrlHash(?string $url_hash): self
|
||||||
{
|
{
|
||||||
$this->url_hash = $url_hash;
|
$this->url_hash = $url_hash;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function getUrlHash(): string
|
public function getUrlHash(): ?string
|
||||||
{
|
{
|
||||||
return $this->url_hash;
|
return $this->url_hash;
|
||||||
}
|
}
|
||||||
|
@ -153,26 +152,6 @@ class File
|
||||||
return $this->is_local;
|
return $this->is_local;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setWidth(?int $width): self
|
|
||||||
{
|
|
||||||
$this->width = $width;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getWidth(): ?int
|
|
||||||
{
|
|
||||||
return $this->width;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setHeight(?int $height): self
|
|
||||||
{
|
|
||||||
$this->height = $height;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getHeight(): ?int
|
|
||||||
{
|
|
||||||
return $this->height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setModified(DateTimeInterface $modified): self
|
public function setModified(DateTimeInterface $modified): self
|
||||||
{
|
{
|
||||||
$this->modified = $modified;
|
$this->modified = $modified;
|
||||||
|
@ -185,6 +164,47 @@ class File
|
||||||
|
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
|
const URLHASH_ALGO = 'sha256';
|
||||||
|
const FILEHASH_ALGO = 'sha256';
|
||||||
|
|
||||||
|
public function getFileName(): string
|
||||||
|
{
|
||||||
|
return $this->file_hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete this file and by default all the associated entities (avatar and/or thumbnails, which this owns)
|
||||||
|
*/
|
||||||
|
public function delete(bool $cascade = true, bool $flush = false, bool $delete_files_now = false): array
|
||||||
|
{
|
||||||
|
$files = [];
|
||||||
|
if ($cascade) {
|
||||||
|
// An avatar can own a file, and it becomes invalid if the file is deleted
|
||||||
|
$avatar = DB::find('avatar', ['file_id' => $this->id]);
|
||||||
|
$files[] = $avatar->getFilePath();
|
||||||
|
$avatar->delete($flush, $delete_files_now, $cascading = true);
|
||||||
|
foreach (DB::findBy('file_thumbnail', ['file_id' => $this->id]) as $ft) {
|
||||||
|
$files[] = $ft->delete($flush, $delete_files_now, $cascading);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DB::remove($this);
|
||||||
|
if ($flush) {
|
||||||
|
DB::flush();
|
||||||
|
}
|
||||||
|
if ($delete_files_now) {
|
||||||
|
self::deleteFiles($files);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return $files;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function deleteFiles(array $files)
|
||||||
|
{
|
||||||
|
foreach ($files as $f) {
|
||||||
|
@unlink($f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function schemaDef(): array
|
public static function schemaDef(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -192,21 +212,19 @@ class File
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'id' => ['type' => 'serial', 'not null' => true],
|
'id' => ['type' => 'serial', 'not null' => true],
|
||||||
'url' => ['type' => 'text', 'description' => 'URL after following possible redirections'],
|
'url' => ['type' => 'text', 'description' => 'URL after following possible redirections'],
|
||||||
'is_url_protected' => ['type' => 'bool', 'description' => 'true when URL is private (needs login)'],
|
'is_url_protected' => ['type' => 'bool', 'default' => false, 'description' => 'true when URL is private (needs login)'],
|
||||||
'url_hash' => ['type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'sha256 of destination URL (url field)'],
|
'url_hash' => ['type' => 'varchar', 'length' => 64, 'description' => 'sha256 of destination URL (url field)'],
|
||||||
'file_hash' => ['type' => 'varchar', 'length' => 64, 'description' => 'sha256 of the file contents, if the file is stored locally'],
|
'file_hash' => ['type' => 'varchar', 'length' => 64, 'description' => 'sha256 of the file contents, if the file is stored locally'],
|
||||||
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
|
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
|
||||||
'size' => ['type' => 'int', 'description' => 'size of resource when available'],
|
'size' => ['type' => 'int', 'description' => 'size of resource when available'],
|
||||||
'title' => ['type' => 'text', 'description' => 'title of resource when available'],
|
'title' => ['type' => 'text', 'description' => 'title of resource when available'],
|
||||||
'timestamp' => ['type' => 'int', 'description' => 'unix timestamp according to http query'],
|
'timestamp' => ['type' => 'int', 'description' => 'unix timestamp according to http query'],
|
||||||
'is_local' => ['type' => 'bool', 'description' => 'whether the file is stored locally'],
|
'is_local' => ['type' => 'bool', 'description' => 'whether the file is stored locally'],
|
||||||
'width' => ['type' => 'int', 'description' => 'width in pixels, if it can be described as such and data is available'],
|
|
||||||
'height' => ['type' => 'int', 'description' => 'height in pixels, if it can be described as such and data is available'],
|
|
||||||
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
||||||
],
|
],
|
||||||
'primary key' => ['id'],
|
'primary key' => ['id'],
|
||||||
'unique keys' => [
|
'unique keys' => [
|
||||||
'file_urlhash_key' => ['url_hash'],
|
'file_file_key' => ['file_hash'],
|
||||||
],
|
],
|
||||||
'indexes' => [
|
'indexes' => [
|
||||||
'file_filehash_idx' => ['file_hash'],
|
'file_filehash_idx' => ['file_hash'],
|
||||||
|
|
|
@ -86,6 +86,15 @@ class FileThumbnail
|
||||||
|
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a file thumbnail. This table doesn't own all the files, only itself
|
||||||
|
*/
|
||||||
|
public function delete(bool $flush = false, bool $delete_files_now = false, bool $cascading = false): string
|
||||||
|
{
|
||||||
|
// TODO Implement deleting file thumbnails
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
public static function schemaDef(): array
|
public static function schemaDef(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|
Loading…
Reference in New Issue
Block a user