From 4d99bfb9fdd2f8b0114fa22735378ecdfb85bd82 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 7 Aug 2020 02:03:55 +0000 Subject: [PATCH] [DB][FILE][AVATAR] Handle deleting files, change file and avatar tables --- src/Entity/Avatar.php | 88 +++++++++++++++++++++++++----------- src/Entity/File.php | 80 +++++++++++++++++++------------- src/Entity/FileThumbnail.php | 9 ++++ 3 files changed, 120 insertions(+), 57 deletions(-) diff --git a/src/Entity/Avatar.php b/src/Entity/Avatar.php index 52ad83099b..68293a94d6 100644 --- a/src/Entity/Avatar.php +++ b/src/Entity/Avatar.php @@ -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'], ], ]; } diff --git a/src/Entity/File.php b/src/Entity/File.php index 6f2af51355..b0c69c3401 100644 --- a/src/Entity/File.php +++ b/src/Entity/File.php @@ -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'], diff --git a/src/Entity/FileThumbnail.php b/src/Entity/FileThumbnail.php index 0d389a5a44..58fe8e4947 100644 --- a/src/Entity/FileThumbnail.php +++ b/src/Entity/FileThumbnail.php @@ -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 [