Skip to content

Commit fda6184

Browse files
authored
Merge pull request #2882 from SeedCompany/bugfix/media
2 parents 7a8a633 + 3d9d07e commit fda6184

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/components/file/media/detect-existing-media.migration.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FileVersion } from '../dto';
66
import { FileRepository } from '../file.repository';
77
import { MediaService } from './media.service';
88

9-
@Migration('2023-09-05T19:00:00')
9+
@Migration('2023-09-06T13:00:00')
1010
export class DetectExistingMediaMigration extends BaseMigration {
1111
constructor(
1212
private readonly mediaService: MediaService,
@@ -16,6 +16,9 @@ export class DetectExistingMediaMigration extends BaseMigration {
1616
}
1717

1818
async up() {
19+
await this.fixVideoLabels();
20+
await this.fixVideoBuggedDuration();
21+
await this.fixFileVersionFromBuggedMediaMerge();
1922
await this.dropAllDuplicatedMedia();
2023

2124
const detect = async (f: FileVersion) => {
@@ -82,6 +85,33 @@ export class DetectExistingMediaMigration extends BaseMigration {
8285
} while (true);
8386
}
8487

88+
private async fixVideoLabels() {
89+
await this.db.query().raw`
90+
match (v:Video)
91+
set v:VisualMedia:TemporalMedia:Media
92+
`.executeAndLogStats();
93+
}
94+
95+
private async fixVideoBuggedDuration() {
96+
await this.db.query().raw`
97+
match (v:Video)
98+
where v.duration is null
99+
set v.duration = 0
100+
`.executeAndLogStats();
101+
}
102+
103+
private async fixFileVersionFromBuggedMediaMerge() {
104+
await this.db.query().raw`
105+
match (badFv:FileVersion)
106+
where not badFv:BaseNode
107+
with badFv
108+
match (realFv:FileVersion:BaseNode { id: badFv.id }),
109+
(badFv)-->(media:Media)
110+
merge (realFv)-[:media]->(media)
111+
detach delete badFv
112+
`.executeAndLogStats();
113+
}
114+
85115
private async dropAllDuplicatedMedia() {
86116
await this.db.query().raw`
87117
match (fv:FileVersion)-->(media:Media)

src/components/file/media/media.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export class Image extends VisualMedia {
120120
@ObjectType({
121121
implements: [VisualMedia, TemporalMedia, Media],
122122
})
123+
@DbLabel('Video', 'VisualMedia', 'TemporalMedia', 'Media') // IntersectionType blocks label inheritance
123124
export class Video extends IntersectionType(VisualMedia, TemporalMedia) {
124125
static readonly Props = keysOf<Video>();
125126
static readonly SecuredProps = keysOf<SecuredProps<Video>>();

src/components/file/media/media.repository.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,27 @@ export class MediaRepository extends CommonRepository {
4949
const res = input.__typename
5050
? EnhancedResource.of(resolveMedia(input as AnyMedia))
5151
: undefined;
52+
const tempId = await generateId();
5253
const query = this.db
5354
.query()
54-
.merge([
55-
node('fv', 'FileVersion', input.file ? { id: input.file } : {}),
56-
relation('out', '', 'media'),
57-
node('node', 'Media', input.id ? { id: input.id } : {}),
55+
.match([
56+
input.file ? [node('fv', 'FileVersion', { id: input.file })] : [],
57+
input.id ? [node('node', 'Media', { id: input.id })] : [],
5858
])
59-
.onCreate.set({
60-
values: { 'node.id': await generateId() },
61-
variables: { 'node.createdAt': 'datetime()' },
62-
})
59+
.apply((q) =>
60+
input.file
61+
? q
62+
.merge([
63+
node('fv'),
64+
relation('out', '', 'media'),
65+
node('node', input.id ? undefined : 'Media'),
66+
])
67+
.onCreate.set({
68+
values: { 'node.id': tempId },
69+
variables: { 'node.createdAt': 'datetime()' },
70+
})
71+
: q,
72+
)
6373
.setValues({ node: toDbShape(input) }, true)
6474
.with('node, fv')
6575
// Update the labels if typename is given, and maybe changed.
@@ -126,7 +136,7 @@ const toDbShape = (input: Partial<AnyMedia>) => ({
126136
? input.dimensions
127137
: // If not visual, ensure dimensions get cleared
128138
{ width: null, height: null }),
129-
...(input.__typename === 'Audio'
139+
...(input.__typename === 'Audio' || input.__typename === 'Video'
130140
? { duration: input.duration }
131141
: // If not temporal, ensure duration gets cleared
132142
{ duration: null }),

0 commit comments

Comments
 (0)