1
1
/* eslint-disable @typescript-eslint/no-unsafe-argument */
2
2
3
- import { existsSync , createReadStream } from 'node:fs'
3
+ import { open } from 'node:fs/promises '
4
4
import path from 'node:path'
5
5
6
6
import { S3Client , ObjectCannedACL } from '@aws-sdk/client-s3'
@@ -13,10 +13,11 @@ import { getDriver } from '@db/neo4j'
13
13
export const description =
14
14
'Upload all image files to a S3 compatible object storage in order to reduce load on our backend.'
15
15
16
- export async function up ( ) {
16
+ export async function up ( _next ) {
17
17
if ( CONFIG . NODE_ENV === 'test' ) {
18
18
// Let's skip this migration for simplicity.
19
19
// There is nothing to migrate in test environment and setting up the S3 services seems overkill.
20
+ return
20
21
}
21
22
const { AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY , AWS_ENDPOINT , AWS_BUCKET } = CONFIG
22
23
if ( ! ( AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY && AWS_ENDPOINT && AWS_BUCKET ) ) {
@@ -39,44 +40,39 @@ export async function up() {
39
40
const { records } = await transaction . run ( 'MATCH (image:Image) RETURN image.url as url' )
40
41
let urls : string [ ] = records . map ( ( r ) => r . get ( 'url' ) as string )
41
42
urls = urls . filter ( ( url ) => url . startsWith ( '/uploads' ) )
42
- const locations = await Promise . all (
43
+ await Promise . all (
43
44
urls
44
- . map ( ( url ) => {
45
- return async ( ) => {
46
- const { pathname } = new URL ( url , 'http://example.org' )
47
- const fileLocation = path . join ( __dirname , `../../../public/${ pathname } ` )
48
- const s3Location = `original${ pathname } `
49
- // eslint-disable-next-line n/no-sync, security/detect-non-literal-fs-filename
50
- if ( existsSync ( fileLocation ) ) {
51
- const mimeType = lookup ( fileLocation )
52
- const params = {
53
- Bucket : AWS_BUCKET ,
54
- Key : s3Location ,
55
- ACL : ObjectCannedACL . public_read ,
56
- ContentType : mimeType || 'image/jpeg' ,
57
- // eslint-disable-next-line security/detect-non-literal-fs-filename
58
- Body : createReadStream ( fileLocation ) ,
59
- }
45
+ . map ( ( url ) => async ( ) => {
46
+ const { pathname } = new URL ( url , 'http://example.org' )
47
+ const fileLocation = path . join ( __dirname , `../../../public/${ pathname } ` )
48
+ const s3Location = `original${ pathname } `
49
+ const mimeType = lookup ( fileLocation )
50
+ console . log ( 'fileLocation' , fileLocation )
51
+ // eslint-disable-next-line security/detect-non-literal-fs-filename
52
+ const fileHandle = await open ( fileLocation )
53
+ const params = {
54
+ Bucket : AWS_BUCKET ,
55
+ Key : s3Location ,
56
+ ACL : ObjectCannedACL . public_read ,
57
+ ContentType : mimeType || 'image/jpeg' ,
58
+ Body : fileHandle . createReadStream ( ) ,
59
+ }
60
60
61
- const command = new Upload ( { client : s3 , params } )
62
- const data = await command . done ( )
63
- const { Location : spacesUrl } = data
61
+ const command = new Upload ( { client : s3 , params } )
62
+ const data = await command . done ( )
63
+ const { Location : spacesUrl } = data
64
64
65
- const updatedRecord = await transaction . run (
66
- 'MATCH (image:Image {url: $url}) SET image.url = $spacesUrl RETURN image.url as url' ,
67
- { url, spacesUrl } ,
68
- )
69
- const [ updatedUrl ] = updatedRecord . records . map (
70
- ( record ) => record . get ( 'url' ) as string ,
71
- )
72
- return updatedUrl
73
- }
74
- }
65
+ const updatedRecord = await transaction . run (
66
+ 'MATCH (image:Image {url: $url}) SET image.url = $spacesUrl RETURN image.url as url' ,
67
+ { url, spacesUrl } ,
68
+ )
69
+ const [ updatedUrl ] = updatedRecord . records . map ( ( record ) => record . get ( 'url' ) as string )
70
+ // eslint-disable-next-line no-console
71
+ console . log ( 'Migrated:' , updatedUrl )
72
+ return updatedUrl
75
73
} )
76
74
. map ( ( p ) => p ( ) ) ,
77
75
)
78
- // eslint-disable-next-line no-console
79
- console . log ( 'these are the locations' , locations )
80
76
await transaction . commit ( )
81
77
} catch ( error ) {
82
78
// eslint-disable-next-line no-console
@@ -90,7 +86,7 @@ export async function up() {
90
86
}
91
87
}
92
88
93
- export async function down ( ) {
89
+ export async function down ( _next ) {
94
90
const driver = getDriver ( )
95
91
const session = driver . session ( )
96
92
const transaction = session . beginTransaction ( )
0 commit comments