[ENTITY] Split Attachment in various new entities

Remove Attachment Scope
Fixed some minor bugs

Scope will be implemented later in v3. It doesn't make sense to have
the scope handling being per attachment. Different actors can post
the same attachment with different scopes. The attachment controller
will assume the highest level of scope applied to the attachment and
the rest will be handled at the note level.

Motivation:
* Remove title from attachment, as it's part of the relation between attachment and note.
* Remove actor from attachment, many actors may publish the same attachment.
* Remove is_local from attachment,  as it's part of the relation between attachment and note.
* Remove remote_url from attachment, different urls can return the same attachment.

Addition:
* Attachment now has a lives attribute,  it's a reference counter with a nicer name
* GSActorToAttachment
* GSActorToRemoteURL
* RemoteURL
* RemoteURLToNote
* RemoteURLToAttachment
* AttachmentToNote now has a title attribute
This commit is contained in:
2021-08-14 16:47:45 +01:00
committed by Hugo Sales
parent a7c8da0534
commit 3f61537140
20 changed files with 731 additions and 274 deletions

View File

@@ -75,12 +75,6 @@ class Avatar extends Component
return Event::next;
}
public function onAttachmentRefCount(int $attachment_id, int &$dependencies): bool
{
$dependencies += DB::count('avatar', ['attachment_id' => $attachment_id]);
return Event::next;
}
// UTILS ----------------------------------
/**
@@ -126,7 +120,7 @@ class Avatar extends Component
{
$res = Cache::get("avatar-file-info-{$gsactor_id}",
function () use ($gsactor_id) {
return DB::dql('select f.id, f.filename, f.mimetype, f.title ' .
return DB::dql('select f.id, f.filename, f.mimetype ' .
'from App\Entity\Attachment f ' .
'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' .
'where a.gsactor_id = :gsactor_id',

View File

@@ -29,7 +29,6 @@ use App\Core\GSFile;
use App\Core\GSFile as M;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Security;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\NotFoundException;
@@ -106,12 +105,9 @@ class Avatar extends Controller
} else {
throw new ClientException('Invalid form');
}
$attachment = GSFile::validateAndStoreFileAsAttachment(
$attachment = GSFile::sanitizeAndStoreFileAsAttachment(
$file,
dest_dir: Common::config('attachments', 'dir'),
actor_id: $gsactor_id,
title: Security::sanitize($file->getClientOriginalName()),
is_local: true
dest_dir: Common::config('attachments', 'dir')
);
// Delete current avatar if there's one
$avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]);

View File

@@ -122,24 +122,17 @@ class Avatar extends Entity
}
/**
* Delete this avatar and, if safe, the corresponding file and thumbnails, which this owns
* Delete this avatar and kill corresponding attachment
*
* @param bool $cascade
* @param bool $flush
* @return bool
*/
public function delete(bool $cascade = true, bool $flush = true): void
public function delete(): bool
{
DB::remove($this);
if ($cascade) {
$attachment = $this->getAttachment();
// We can't use $attachment->isSafeDelete() because underlying findBy doesn't respect remove persistence
if ($attachment->refCount() - 1 === 0) {
$attachment->delete(cascade: true, flush: false);
}
}
if ($flush) {
DB::flush();
}
$attachment = $this->getAttachment();
$attachment->kill();
DB::flush();
return true;
}
public static function schemaDef(): array

View File

@@ -28,13 +28,17 @@ use App\Core\Form;
use App\Core\GSFile;
use function App\Core\I18n\_m;
use App\Core\Modules\Component;
use App\Core\Security;
use App\Entity\Attachment;
use App\Entity\AttachmentToNote;
use App\Entity\GSActorToAttachment;
use App\Entity\GSActorToRemoteURL;
use App\Entity\Note;
use App\Entity\RemoteURL;
use App\Entity\RemoteURLToNote;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\RedirectException;
use InvalidArgumentException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@@ -115,31 +119,42 @@ END;
$processed_attachments = [];
foreach ($attachments as $f) { // where $f is a Symfony\Component\HttpFoundation\File\UploadedFile
$processed_attachments[] = GSFile::validateAndStoreFileAsAttachment(
$filesize = $f->getSize();
Event::handle('EnforceQuota', [$actor_id, $filesize]);
$processed_attachments[] = GSFile::sanitizeAndStoreFileAsAttachment(
$f,
dest_dir: Common::config('attachments', 'dir'),
actor_id: $actor_id,
title: Security::sanitize($f->getClientOriginalName()),
is_local: true
dest_dir: Common::config('attachments', 'dir')
);
}
$matched_urls = [];
preg_match_all(self::URL_REGEX, $content, $matched_urls, PREG_SET_ORDER);
foreach ($matched_urls as $match) {
$processed_attachments[] = GSFile::validateAndStoreURLAsAttachment($match[0]);
}
DB::persist($note);
// Need file and note ids for the next step
DB::flush();
if ($processed_attachments != []) {
foreach ($processed_attachments as $a) {
DB::persist(GSActorToAttachment::create(['attachment_id' => $a->getId(), 'gsactor_id' => $actor_id]));
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId()]));
}
DB::flush();
}
$matched_urls = [];
$processed_urls = false;
preg_match_all(self::URL_REGEX, $content, $matched_urls, PREG_SET_ORDER);
foreach ($matched_urls as $match) {
try {
$remoteurl_id = RemoteURL::getOrCreate($match[0])->getId();
DB::persist(GSActorToRemoteURL::create(['remoteurl_id' => $remoteurl_id, 'gsactor_id' => $actor_id]));
DB::persist(RemoteURLToNote::create(['remoteurl_id' => $a->getId(), 'note_id' => $note->getId()]));
$processed_urls = true;
} catch (InvalidArgumentException) {
continue;
}
}
if ($processed_urls) {
DB::flush();
}
}
/**