1
1
import { ModuleRef } from '@nestjs/core' ;
2
2
import { node , relation } from 'cypher-query-builder' ;
3
- import { IdOf } from '~/common' ;
4
3
import { BaseMigration , Migration } from '~/core/database' ;
5
4
import { ACTIVE } from '~/core/database/query' ;
6
5
import { FileVersion } from '../dto' ;
7
- import { FileService } from '../file.service ' ;
6
+ import { FileRepository } from '../file.repository ' ;
8
7
import { MediaService } from './media.service' ;
9
8
10
- @Migration ( '2023-08-24T15 :00:00' )
9
+ @Migration ( '2023-09-01T18 :00:00' )
11
10
export class DetectExistingMediaMigration extends BaseMigration {
12
11
constructor (
13
12
private readonly mediaService : MediaService ,
@@ -17,12 +16,10 @@ export class DetectExistingMediaMigration extends BaseMigration {
17
16
}
18
17
19
18
async up ( ) {
20
- const fileService = this . moduleRef . get ( FileService , { strict : false } ) ;
21
- const detect = async ( f : Row ) => {
19
+ const detect = async ( f : FileVersion ) => {
22
20
this . logger . info ( 'Detecting' , f ) ;
23
21
try {
24
- const d = fileService . asDownloadable ( f , f . file ) ;
25
- const result = await this . mediaService . detectAndSave ( d ) ;
22
+ const result = await this . mediaService . detectAndSave ( f ) ;
26
23
this . logger . info ( 'Detected and saved media' , {
27
24
...f ,
28
25
...( result ?? { } ) ,
@@ -39,26 +36,40 @@ export class DetectExistingMediaMigration extends BaseMigration {
39
36
}
40
37
41
38
private async * grabFileVersionsToDetect ( type : string ) {
39
+ const fileRepo = this . moduleRef . get ( FileRepository , { strict : false } ) ;
40
+
42
41
let page = 0 ;
43
42
const size = 100 ;
44
43
do {
45
44
this . logger . info ( `Grabbing page of files to detect ${ page } ` ) ;
46
45
47
46
const currentPage = await this . db
48
47
. query ( )
49
- . match ( [
50
- node ( 'fv' , 'FileVersion' ) ,
51
- relation ( 'out' , '' , 'mimeType' , ACTIVE ) ,
52
- node ( 'mt' , 'Property' ) ,
53
- ] )
54
- . raw (
55
- `where not (fv)-[:media]->(:Media)
56
- and mt.value starts with '${ type } /'` ,
48
+ // eslint-disable-next-line no-loop-func
49
+ . subQuery ( ( sub ) =>
50
+ sub
51
+ . match ( [
52
+ node ( 'fv' , 'FileVersion' ) ,
53
+ relation ( 'out' , '' , 'mimeType' , ACTIVE ) ,
54
+ node ( 'mt' , 'Property' ) ,
55
+ ] )
56
+ . optionalMatch ( [
57
+ node ( 'fv' ) ,
58
+ relation ( 'out' , '' , 'media' ) ,
59
+ node ( 'media' , 'Media' ) ,
60
+ ] )
61
+ . with ( 'fv, mt, media' )
62
+ . raw (
63
+ `where mt.value starts with '${ type } /' and (media is null or media.duration = 0)` ,
64
+ )
65
+ . return ( 'fv' )
66
+ . orderBy ( 'fv.createdAt' )
67
+ . skip ( page * size )
68
+ . limit ( size ) ,
57
69
)
58
- . return < Row > ( 'fv.id as file, mt.value as mimeType' )
59
- . orderBy ( 'fv.createdAt' )
60
- . skip ( page * size )
61
- . limit ( size )
70
+ . with ( 'fv as node' )
71
+ . apply ( fileRepo . hydrate ( ) )
72
+ . map ( ( row ) => row . dto as FileVersion )
62
73
. run ( ) ;
63
74
64
75
if ( currentPage . length === 0 ) {
@@ -70,8 +81,3 @@ export class DetectExistingMediaMigration extends BaseMigration {
70
81
} while ( true ) ;
71
82
}
72
83
}
73
-
74
- interface Row {
75
- file : IdOf < FileVersion > ;
76
- mimeType : string ;
77
- }
0 commit comments