1+ import { bytes } from '@/lib/bytes' ;
2+ import { config , reloadSettings } from '@/lib/config' ;
13import { guess } from '@/lib/mimes' ;
24import { statSync } from 'fs' ;
3- import { readFile , readdir } from 'fs/promises' ;
5+ import { mkdir , readdir } from 'fs/promises' ;
46import { join , parse , resolve } from 'path' ;
5- import { reloadSettings } from '@/lib/config' ;
6- import { bytes } from '@/lib/bytes' ;
77
88export async function importDir (
99 directory : string ,
@@ -57,16 +57,18 @@ export async function importDir(
5757
5858 for ( let i = 0 ; i !== files . length ; ++ i ) {
5959 const info = parse ( files [ i ] ) ;
60+ if ( info . base . startsWith ( '.thumbnail' ) ) continue ;
61+
6062 const mime = await guess ( info . ext . replace ( '.' , '' ) ) ;
6163 const { size } = statSync ( join ( fullPath , files [ i ] ) ) ;
6264
63- data [ i ] = {
65+ data . push ( {
6466 name : info . base ,
6567 type : mime ,
6668 size,
6769 userId,
6870 ...( folder ? { folderId : folder } : { } ) ,
69- } ;
71+ } ) ;
7072 }
7173
7274 if ( ! skipDb ) {
@@ -78,16 +80,25 @@ export async function importDir(
7880
7981 const totalSize = data . reduce ( ( acc , file ) => acc + file . size , 0 ) ;
8082 let completed = 0 ;
83+ let imported = 0 ;
84+
85+ if ( config . datasource . type === 'local' )
86+ await mkdir ( config . datasource . local ! . directory , { recursive : true } ) ;
87+
88+ const { getDatasource } = await import ( '@/lib/datasource/index.js' ) ;
89+ const datasource = getDatasource ( config ) ;
90+ if ( ! datasource ) return console . error ( 'No datasource configured' ) ;
91+
92+ for ( let i = 0 ; i !== data . length ; ++ i ) {
93+ if ( ! data [ i ] ) continue ;
8194
82- const { datasource } = await import ( '@/lib/datasource/index.js' ) ;
83- for ( let i = 0 ; i !== files . length ; ++ i ) {
8495 console . log ( `Uploading ${ data [ i ] . name } (${ bytes ( data [ i ] . size ) } )...` ) ;
8596
8697 const start = process . hrtime ( ) ;
8798
88- const buff = await readFile ( join ( fullPath , files [ i ] ) ) ;
89- await datasource . put ( data [ i ] . name , buff , {
99+ await datasource . put ( data [ i ] . name , join ( fullPath , files [ i ] ) , {
90100 mimetype : data [ i ] . type ?? 'application/octet-stream' ,
101+ noDelete : true ,
91102 } ) ;
92103
93104 const diff = process . hrtime ( start ) ;
@@ -104,7 +115,11 @@ export async function importDir(
104115 console . log (
105116 `Uploaded ${ data [ i ] . name } in ${ timeStr } (${ bytes ( data [ i ] . size ) } ) ${ i + 1 } /${ files . length } ${ bytes ( completed ) } /${ bytes ( totalSize ) } ${ uploadSpeedStr } ` ,
106117 ) ;
118+
119+ ++ imported ;
107120 }
108121
109- console . log ( 'Done importing files.' ) ;
122+ console . log ( `Done importing ${ imported } files.` ) ;
123+
124+ process . exit ( 0 ) ;
110125}
0 commit comments