diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index a6aadb08c6..ddb5f054ed 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -28,7 +28,9 @@ use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Modules\Component; use App\Entity\Actor; +use App\Entity\ActorToAttachment; use App\Entity\Attachment; +use App\Entity\AttachmentToNote; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\ClientException; @@ -109,6 +111,21 @@ class Posting extends Component return Event::next; } + /** + * Store the given note with $content and $attachments, created by + * $actor_id, possibly as a reply to note $reply_to and with flag + * $is_local. Sanitizes $content and $attachments + * + * @param Actor $actor + * @param string $content + * @param string $content_type + * @param array $attachments + * @param null|Note $reply_to + * @param null|Note $repeat_of + * + * @throws ClientException + * @throws ServerException + */ public static function storeLocalNote(Actor $actor, string $content, string $content_type, array $attachments, ?Note $reply_to = null, ?Note $repeat_of = null) { $rendered = null; @@ -118,11 +135,36 @@ class Posting extends Component 'content' => $content, 'content_type' => $content_type, 'rendered' => $rendered, - 'attachments' => $attachments, // Not a regular field 'is_local' => true, ]); + + $processed_attachments = []; + /** @var \Symfony\Component\HttpFoundation\File\UploadedFile[] $attachments */ + foreach ($attachments as $f) { + $filesize = $f->getSize(); + $max_file_size = Common::config('attachments', 'file_quota'); + if ($max_file_size < $filesize) { + throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' . + 'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize])); + } + Event::handle('EnforceUserFileQuota', [$filesize, $actor->getId()]); + $processed_attachments[] = [GSFile::sanitizeAndStoreFileAsAttachment($f), $f->getClientOriginalName()]; + } + + DB::persist($note); + + // Need file and note ids for the next step Event::handle('ProcessNoteContent', [$note->getId(), $content, $content_type]); DB::flush(); + + if ($processed_attachments != []) { + foreach ($processed_attachments as [$a, $fname]) { + if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $args['actor_id']]) === 0) { + DB::persist(ActorToAttachment::create($args)); + } + DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname])); + } + } } public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, ?Note $reply_to = null) diff --git a/plugins/FileQuota/FileQuota.php b/plugins/FileQuota/FileQuota.php index 32d5b58b45..93115ce9e2 100644 --- a/plugins/FileQuota/FileQuota.php +++ b/plugins/FileQuota/FileQuota.php @@ -69,12 +69,6 @@ select sum(at.size) as total where ua.actor_id = :actor_id and at.size is not null END; - $max_file_size = Common::config('attachments', 'file_quota'); - if ($max_file_size < $filesize) { - throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ', - ['quota' => $max_file_size, 'size' => $filesize])); - } - $max_user_quota = Common::config('attachments', 'user_quota'); if ($max_user_quota !== false) { // If not disabled $cache_key_user_total = "FileQuota-total-user-{$user_id}"; diff --git a/src/Entity/Note.php b/src/Entity/Note.php index d0da8bbb6e..50dc4f2c88 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -25,7 +25,6 @@ use App\Core\Cache; use App\Core\DB\DB; use App\Core\Entity; use App\Core\Event; -use App\Core\GSFile; use App\Core\VisibilityScope; use DateTimeInterface; @@ -299,40 +298,6 @@ class Note extends Entity ['note_id' => $this->id, 'actor_id' => $a->getId()])); } - /** - * Create an instance of NoteToLink or fill in the - * properties of $obj with the associative array $args. Does - * persist the result - */ - public static function create(array $args, mixed $obj = null): self - { - /** @var \Symfony\Component\HttpFoundation\File\UploadedFile[] $attachments */ - $attachments = $args['attachments']; - unset($args['attachments']); - - $note = parent::create($args, new self()); - - $processed_attachments = []; - foreach ($attachments as $f) { - Event::handle('EnforceUserFileQuota', [$f->getSize(), $args['actor_id']]); - $processed_attachments[] = [GSFile::sanitizeAndStoreFileAsAttachment($f), $f->getClientOriginalName()]; - } - - // Need file and note ids for the next step - DB::persist($note); - - if ($processed_attachments != []) { - foreach ($processed_attachments as [$a, $fname]) { - if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $args['actor_id']]) === 0) { - DB::persist(ActorToAttachment::create($args)); - } - DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname])); - } - } - - return $note; - } - /** * @return Actor[] */