From 5579f4fa5da778fc90102e07a00bec4e57b0aef9 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 15 Apr 2021 22:28:28 +0000 Subject: [PATCH] [MEDIA] Rename File to Attachment --- components/Media/Utils.php | 27 +++++++------- src/Controller/UserPanel.php | 3 +- src/Entity/Attachment.php | 16 +++++++-- src/Entity/AttachmentThumbnail.php | 35 ++++++++++--------- .../{FileToNote.php => AttachmentToNote.php} | 28 +++++++-------- src/Entity/Note.php | 12 +++---- 6 files changed, 67 insertions(+), 54 deletions(-) rename src/Entity/{FileToNote.php => AttachmentToNote.php} (63%) diff --git a/components/Media/Utils.php b/components/Media/Utils.php index d8ab47c9f2..20fcf87dc0 100644 --- a/components/Media/Utils.php +++ b/components/Media/Utils.php @@ -25,8 +25,8 @@ use App\Core\Cache; use App\Core\DB\DB; use function App\Core\I18n\_m; use App\Core\Log; +use App\Entity\Attachment; use App\Entity\Avatar; -use App\Entity\File; use App\Util\Common; use App\Util\Exception\ClientException; use Component\Media\Exception\NoAvatarException; @@ -43,20 +43,21 @@ abstract class Utils /** * Perform file validation (checks and normalization) and store the given file */ - public static function validateAndStoreFile(SymfonyFile $sfile, - string $dest_dir, - ?string $title = null, - bool $is_local = true, - ?int $actor_id = null): File + public static function validateAndStoreAttachment(SymfonyFile $sfile, + string $dest_dir, + ?string $title = null, + bool $is_local = true, + ?int $actor_id = null): Attachment { // The following properly gets the mimetype with `file` or other // available methods, so should be safe - $hash = hash_file(File::FILEHASH_ALGO, $sfile->getPathname()); - $file = File::create([ + $hash = hash_file(Attachment::FILEHASH_ALGO, $sfile->getPathname()); + $file = Attachment::create([ 'file_hash' => $hash, 'actor_id' => $actor_id, 'mimetype' => $sfile->getMimeType(), 'title' => $title ?: _m('Untitled attachment'), + 'filename' => $hash, 'is_local' => $is_local, ]); $sfile->move($dest_dir, $hash); @@ -77,7 +78,7 @@ abstract class Utils [ 'Content-Description' => 'File Transfer', 'Content-Type' => $mimetype, - 'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('untitled')), + 'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment')), 'Cache-Control' => 'public', ], $public = true, @@ -122,9 +123,9 @@ abstract class Utils $id, Cache::get("file-info-{$id}", function () use ($id) { - return DB::dql('select f.file_hash, f.mimetype, f.title ' . - 'from App\\Entity\\File f ' . - 'where f.id = :id', + return DB::dql('select at.file_hash, at.mimetype, at.title ' . + 'from App\\Entity\\Attachment at ' . + 'where at.id = :id', ['id' => $id]); })); } @@ -192,7 +193,7 @@ abstract class Utils Cache::get("avatar-file-info-{$nickname}", function () use ($nickname) { return DB::dql('select f.file_hash, f.mimetype, f.title ' . - 'from App\\Entity\\File f ' . + 'from App\\Entity\\Attachment f ' . 'join App\\Entity\\Avatar a with f.id = a.file_id ' . 'join App\\Entity\\GSActor g with g.id = a.gsactor_id ' . 'where g.nickname = :nickname', diff --git a/src/Controller/UserPanel.php b/src/Controller/UserPanel.php index 714618a4fa..fdfc9669ca 100644 --- a/src/Controller/UserPanel.php +++ b/src/Controller/UserPanel.php @@ -41,7 +41,6 @@ use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Log; use App\Entity\Avatar; -use App\Entity\File; use App\Util\ClientException; use App\Util\Common; use App\Util\Form\ArrayTransformer; @@ -147,7 +146,7 @@ class UserPanel extends AbstractController } $user = Common::user(); $actor_id = $user->getId(); - $file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id); + $file = Media::validateAndStoreAttachment($sfile, Common::config('avatar', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id); $old_file = null; $avatar = DB::find('avatar', ['gsactor_id' => $actor_id]); // Must get old id before inserting another one diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index befc19d483..e9c18172e6 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -52,6 +52,7 @@ class Attachment extends Entity private ?string $filename; private ?bool $is_local; private ?int $source; + private ?int $scope; private DateTimeInterface $modified; public function setId(int $id): self @@ -164,6 +165,17 @@ class Attachment extends Entity return $this->source; } + public function setScope(?int $scope): self + { + $this->scope = $scope; + return $this; + } + + public function getScope(): ?int + { + return $this->scope; + } + public function setModified(DateTimeInterface $modified): self { $this->modified = $modified; @@ -217,9 +229,8 @@ class Attachment extends Entity public static function schemaDef(): array { - // TODO add scope return [ - 'name' => 'file', + 'name' => 'attachment', 'fields' => [ 'id' => ['type' => 'serial', 'not null' => true], 'remote_url' => ['type' => 'text', 'description' => 'URL after following possible redirections'], @@ -231,6 +242,7 @@ class Attachment extends Entity 'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'title of resource when available'], 'is_local' => ['type' => 'bool', 'description' => 'whether the file is stored locally'], 'source' => ['type' => 'int', 'default' => null, 'description' => 'Source of the Attachment (upload, TFN, embed)'], + 'scope' => ['type' => 'int', 'default' => null, 'description' => 'visibility scope for this attachment'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], ], 'primary key' => ['id'], diff --git a/src/Entity/AttachmentThumbnail.php b/src/Entity/AttachmentThumbnail.php index 216e12131e..942bee2a0b 100644 --- a/src/Entity/AttachmentThumbnail.php +++ b/src/Entity/AttachmentThumbnail.php @@ -1,6 +1,7 @@ . + // }}} namespace App\Entity; @@ -23,7 +25,7 @@ use App\Core\Entity; use DateTimeInterface; /** - * Entity for File thumbnails + * Entity for Attachment thumbnails * * @category DB * @package GNUsocial @@ -39,20 +41,20 @@ use DateTimeInterface; class AttachmentThumbnail extends Entity { // {{{ Autocode - private int $file_id; + private int $attachment_id; private int $width; private int $height; private DateTimeInterface $modified; - public function setFileId(int $file_id): self + public function setAttachmentId(int $attachment_id): self { - $this->file_id = $file_id; + $this->attachment_id = $attachment_id; return $this; } - public function getFileId(): int + public function getAttachmentId(): int { - return $this->file_id; + return $this->attachment_id; } public function setWidth(int $width): self @@ -91,28 +93,27 @@ class AttachmentThumbnail extends Entity // }}} Autocode /** - * Delete a file thumbnail. This table doesn't own all the files, only itself + * Delete a attachment thumbnail. This table doesn't own all the attachments, only itself */ - public function delete(bool $flush = false, bool $delete_files_now = false, bool $cascading = false): string + public function delete(bool $flush = false, bool $delete_attachments_now = false, bool $cascading = false): string { - // TODO Implement deleting file thumbnails + // TODO Implement deleting attachment thumbnails return ''; } public static function schemaDef(): array { return [ - 'name' => 'file_thumbnail', + 'name' => 'attachment_thumbnail', 'fields' => [ - 'file_ - id' => ['type' => 'int', 'foreign key' => true, 'target' => 'File.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what file'], - 'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'], - 'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'], - 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], + 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what attachment'], + 'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'], + 'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'], + 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], ], - 'primary key' => ['file_id', 'width', 'height'], + 'primary key' => ['attachment_id', 'width', 'height'], 'indexes' => [ - 'file_thumbnail_file_id_idx' => ['file_id'], + 'attachment_thumbnail_attachment_id_idx' => ['attachment_id'], ], ]; } diff --git a/src/Entity/FileToNote.php b/src/Entity/AttachmentToNote.php similarity index 63% rename from src/Entity/FileToNote.php rename to src/Entity/AttachmentToNote.php index 0aafd04400..3e102eb4b5 100644 --- a/src/Entity/FileToNote.php +++ b/src/Entity/AttachmentToNote.php @@ -23,7 +23,7 @@ use App\Core\Entity; use DateTimeInterface; /** - * Entity for relating a file to a post + * Entity for relating a attachment to a post * * @category DB * @package GNUsocial @@ -36,22 +36,22 @@ use DateTimeInterface; * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -class FileToNote extends Entity +class AttachmentToNote extends Entity { // {{{ Autocode - private int $file_id; + private int $attachment_id; private int $note_id; private DateTimeInterface $modified; - public function setFileId(int $file_id): self + public function setAttachmentId(int $attachment_id): self { - $this->file_id = $file_id; + $this->attachment_id = $attachment_id; return $this; } - public function getFileId(): int + public function getAttachmentId(): int { - return $this->file_id; + return $this->attachment_id; } public function setNoteId(int $note_id): self @@ -81,16 +81,16 @@ class FileToNote extends Entity public static function schemaDef(): array { return [ - 'name' => 'file_to_note', + 'name' => 'attachment_to_note', 'fields' => [ - 'file_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'File.id', 'multiplicity' => 'one to one', 'name' => 'file_to_note_file_id_fkey', 'not null' => true, 'description' => 'id of file'], - 'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'name' => 'file_to_note_note_id_fkey', 'not null' => true, 'description' => 'id of the note it belongs to'], - 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], + 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'name' => 'attachment_to_note_attachment_id_fkey', 'not null' => true, 'description' => 'id of attachment'], + 'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'name' => 'attachment_to_note_note_id_fkey', 'not null' => true, 'description' => 'id of the note it belongs to'], + 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], ], - 'primary key' => ['file_id', 'note_id'], + 'primary key' => ['attachment_id', 'note_id'], 'indexes' => [ - 'file_id_idx' => ['file_id'], - 'note_id_idx' => ['note_id'], + 'attachment_id_idx' => ['attachment_id'], + 'note_id_idx' => ['note_id'], ], ]; } diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 1b8306f4ae..8bc4960389 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -25,7 +25,7 @@ use App\Core\Cache; use App\Core\DB\DB; use App\Core\Entity; use App\Core\Event; -use App\Core\NoteScope; +use App\Core\VisibilityScope; use DateTimeInterface; /** @@ -213,9 +213,9 @@ class Note extends Entity { return Cache::get('note-attachments-' . $this->id, function () { return DB::dql( - 'select f from App\Entity\File f ' . - 'join App\Entity\FileToNote ftn with ftn.file_id = f.id ' . - 'where ftn.note_id = :note_id', + 'select att from App\Entity\Attachment att ' . + 'join App\Entity\AttachmentToNote atn with atn.attachment_id = att.id ' . + 'where atn.note_id = :note_id', ['note_id' => $this->id] ); }); @@ -247,7 +247,7 @@ class Note extends Entity */ public function isVisibleTo(/* GSActor|LocalUser */ $a): bool { - $scope = NoteScope::create($this->scope); + $scope = VisibilityScope::create($this->scope); return $scope->public || ($scope->follower && null != DB::find('follow', ['follower' => $a->getId(), 'followed' => $this->gsactor_id])) @@ -274,7 +274,7 @@ class Note extends Entity 'source' => ['type' => 'varchar', 'foreign key' => true, 'length' => 32, 'target' => 'NoteSource.code', 'multiplicity' => 'many to one', 'description' => 'fkey to source of note, like "web", "im", or "clientname"'], 'conversation' => ['type' => 'int', 'foreign key' => true, 'target' => 'Conversation.id', 'multiplicity' => 'one to one', 'description' => 'the local conversation id'], 'repeat_of' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note this is a repeat of'], - 'scope' => ['type' => 'int', 'not null' => true, 'default' => NoteScope::PUBLIC, 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = groups; 8 = followers; 16 = messages; null = default'], + 'scope' => ['type' => 'int', 'not null' => true, 'default' => VisibilityScope::PUBLIC, 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = groups; 8 = followers; 16 = messages; null = default'], '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'], ],