diff options
author | Joas Schilling <coding@schilljs.com> | 2022-04-20 10:38:26 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-04-20 10:38:26 +0300 |
commit | e5229fa6e6699e76bd96c1af49c63744e9570289 (patch) | |
tree | f3e6b679eae6c8d277c89b504a1530f60df98041 /lib | |
parent | d2c77b45e6bcb7c1e975f123da28771e4694b213 (diff) |
Fix migration of attachment types for media
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Migration/Version14000Date20220330141646.php | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/lib/Migration/Version14000Date20220330141646.php b/lib/Migration/Version14000Date20220330141646.php index 4277b878d..2f0e82654 100644 --- a/lib/Migration/Version14000Date20220330141646.php +++ b/lib/Migration/Version14000Date20220330141646.php @@ -97,8 +97,6 @@ class Version14000Date20220330141646 extends SimpleMigrationStep { * @param array $options */ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { - // FIXME need to loop over all chat messages :( - $insert = $this->connection->getQueryBuilder(); $insert->insert('talk_attachments') ->setValue('room_id', $insert->createParameter('room_id')) @@ -129,7 +127,7 @@ class Version14000Date20220330141646 extends SimpleMigrationStep { protected function chunkedWriting(IQueryBuilder $insert, IQueryBuilder $select, int $offset): int { $select->setParameter('offset', $offset); - $attachments = []; + $attachments = $sharesWithoutMimetype = []; $result = $select->executeQuery(); while ($row = $result->fetch()) { $attachment = [ @@ -166,11 +164,14 @@ class Version14000Date20220330141646 extends SimpleMigrationStep { } elseif (str_starts_with($mimetype, 'image/') || str_starts_with($mimetype, 'video/')) { $attachment['object_type'] = Attachment::TYPE_MEDIA; } else { + if ($mimetype === '' && isset($parameters['share'])) { + $sharesWithoutMimetype[(int) $parameters['share']] = (int) $row['id']; + } $attachment['object_type'] = Attachment::TYPE_FILE; } } - $attachments[] = $attachment; + $attachments[(int) $row['id']] = $attachment; } $result->closeCursor(); @@ -178,6 +179,19 @@ class Version14000Date20220330141646 extends SimpleMigrationStep { return 0; } + $mimetypes = $this->getMimetypeFromFileCache(array_keys($sharesWithoutMimetype)); + foreach ($mimetypes as $shareId => $mimetype) { + if (!isset($attachments[$sharesWithoutMimetype[$shareId]])) { + continue; + } + + if (str_starts_with($mimetype, 'audio/')) { + $attachments[$sharesWithoutMimetype[$shareId]]['object_type'] = Attachment::TYPE_AUDIO; + } elseif (str_starts_with($mimetype, 'image/') || str_starts_with($mimetype, 'video/')) { + $attachments[$sharesWithoutMimetype[$shareId]]['object_type'] = Attachment::TYPE_MEDIA; + } + } + $this->connection->beginTransaction(); foreach ($attachments as $attachment) { $insert @@ -195,4 +209,22 @@ class Version14000Date20220330141646 extends SimpleMigrationStep { return end($attachments)['message_id']; } + + protected function getMimetypeFromFileCache(array $shareIds): array { + $mimetype = []; + + $query = $this->connection->getQueryBuilder(); + $query->select('s.id', 'm.mimetype') + ->from('share', 's') + ->leftJoin('s', 'filecache', 'f', $query->expr()->eq('s.item_source', 'f.fileid')) + ->leftJoin('f', 'mimetypes', 'm', $query->expr()->eq('f.mimetype', 'm.id')) + ->where($query->expr()->in('s.id', $query->createNamedParameter($shareIds, IQueryBuilder::PARAM_INT_ARRAY))); + $result = $query->executeQuery(); + while ($row = $result->fetch()) { + $mimetype[$row['id']] = $row['mimetype']; + } + $result->closeCursor(); + + return $mimetype; + } } |