[DB][FILE][AVATAR] Handle deleting files, change file and avatar tables

This commit is contained in:
Hugo Sales 2020-08-07 02:03:55 +00:00 committed by Hugo Sales
parent 5a68fd287b
commit 4d99bfb9fd
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 120 additions and 57 deletions

View File

@ -19,6 +19,8 @@
namespace App\Entity;
use App\Core\DB\DB;
use App\Core\Entity;
use DateTimeInterface;
/**
@ -35,15 +37,15 @@ use DateTimeInterface;
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Avatar
class Avatar extends Entity
{
// {{{ Autocode
private int $profile_id;
private ?bool $is_original;
private int $width;
private int $height;
private string $mediatype;
private ?bool $is_original;
private int $file_id;
private \DateTimeInterface $created;
private \DateTimeInterface $modified;
@ -57,16 +59,6 @@ class Avatar
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
{
$this->width = $width;
@ -87,14 +79,24 @@ class Avatar
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;
}
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
@ -119,25 +121,59 @@ class Avatar
// }}} 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
{
return [
'name' => 'avatar',
'fields' => [
'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?'],
'width' => ['type' => 'int', 'not null' => true, 'description' => 'image width'],
'height' => ['type' => 'int', 'not null' => true, 'description' => 'image height'],
'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'],
'profile_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'],
'file_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to file table'],
'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
],
'primary key' => ['profile_id', 'width', 'height'],
'primary key' => ['profile_id'],
'foreign keys' => [
'avatar_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
'avatar_file_id_fkey' => ['file', ['file_id' => 'id']],
],
'indexes' => [
'avatar_profile_id_idx' => ['profile_id'],
'avatar_file_id_idx' => ['file_id'],
],
];
}

View File

@ -19,6 +19,7 @@
namespace App\Entity;
use App\Core\Entity;
use DateTimeInterface;
/**
@ -35,22 +36,20 @@ use DateTimeInterface;
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class File
class File extends Entity
{
// {{{ Autocode
private int $id;
private ?string $url;
private ?bool $is_url_protected;
private string $url_hash;
private ?string $url_hash;
private ?string $file_hash;
private ?string $mimetype;
private ?int $size;
private ?string $title;
private ?int $timestamp;
private ?bool $is_local;
private ?int $width;
private ?int $height;
private \DateTimeInterface $modified;
public function setId(int $id): self
@ -83,12 +82,12 @@ class File
return $this->is_url_protected;
}
public function setUrlHash(string $url_hash): self
public function setUrlHash(?string $url_hash): self
{
$this->url_hash = $url_hash;
return $this;
}
public function getUrlHash(): string
public function getUrlHash(): ?string
{
return $this->url_hash;
}
@ -153,26 +152,6 @@ class File
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
{
$this->modified = $modified;
@ -185,6 +164,47 @@ class File
// }}} 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
{
return [
@ -192,21 +212,19 @@ class File
'fields' => [
'id' => ['type' => 'serial', 'not null' => true],
'url' => ['type' => 'text', 'description' => 'URL after following possible redirections'],
'is_url_protected' => ['type' => 'bool', 'description' => 'true when URL is private (needs login)'],
'url_hash' => ['type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'sha256 of destination URL (url field)'],
'is_url_protected' => ['type' => 'bool', 'default' => false, 'description' => 'true when URL is private (needs login)'],
'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'],
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
'size' => ['type' => 'int', 'description' => 'size of resource when available'],
'title' => ['type' => 'text', 'description' => 'title of resource when available'],
'timestamp' => ['type' => 'int', 'description' => 'unix timestamp according to http query'],
'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'],
],
'primary key' => ['id'],
'unique keys' => [
'file_urlhash_key' => ['url_hash'],
'file_file_key' => ['file_hash'],
],
'indexes' => [
'file_filehash_idx' => ['file_hash'],

View File

@ -86,6 +86,15 @@ class FileThumbnail
// }}} 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
{
return [