@@ -17,6 +17,8 @@ import {
17
17
import { stringToUuid } from "@ai16z/eliza" ;
18
18
import { settings } from "@ai16z/eliza" ;
19
19
import { createApiRouter } from "./api.ts" ;
20
+ import * as fs from "fs" ;
21
+ import * as path from "path" ;
20
22
const upload = multer ( { storage : multer . memoryStorage ( ) } ) ;
21
23
22
24
export const messageHandlerTemplate =
@@ -295,27 +297,63 @@ export class DirectClient {
295
297
}
296
298
) ;
297
299
this . app . get (
298
- "/fine-tune/:assetId" ,
300
+ "/fine-tune/:assetId" ,
299
301
async ( req : express . Request , res : express . Response ) => {
300
302
const assetId = req . params . assetId ;
303
+ const downloadDir = path . join ( process . cwd ( ) , 'downloads' , assetId ) ;
304
+
305
+ console . log ( 'Download directory:' , downloadDir ) ;
306
+
301
307
try {
302
- const response = await fetch ( `https://api.bageldb.ai/api/v1/asset/${ assetId } /download` , {
308
+ console . log ( 'Creating directory...' ) ;
309
+ await fs . promises . mkdir ( downloadDir , { recursive : true } ) ;
310
+
311
+ console . log ( 'Fetching file...' ) ;
312
+ const fileResponse = await fetch ( `https://api.bageldb.ai/api/v1/asset/${ assetId } /download` , {
303
313
headers : {
304
314
'X-API-KEY' : `${ process . env . BAGEL_API_KEY } `
305
315
}
306
316
} ) ;
317
+
318
+ if ( ! fileResponse . ok ) {
319
+ throw new Error ( `API responded with status ${ fileResponse . status } : ${ await fileResponse . text ( ) } ` ) ;
320
+ }
321
+
322
+ console . log ( 'Response headers:' , fileResponse . headers ) ;
323
+
324
+ const fileName = fileResponse . headers . get ( 'content-disposition' )
325
+ ?. split ( 'filename=' ) [ 1 ]
326
+ ?. replace ( / " / g, '' ) || 'default_name.txt' ;
307
327
308
- // Forward the content-type header
309
- res . set ( 'Content-Type' , response . headers . get ( 'content-type' ) ) ;
328
+ console . log ( 'Saving as:' , fileName ) ;
310
329
311
- // Convert ReadableStream to Buffer and send
312
- const arrayBuffer = await response . arrayBuffer ( ) ;
330
+ const arrayBuffer = await fileResponse . arrayBuffer ( ) ;
313
331
const buffer = Buffer . from ( arrayBuffer ) ;
314
- res . send ( buffer ) ;
332
+
333
+ const filePath = path . join ( downloadDir , fileName ) ;
334
+ console . log ( 'Full file path:' , filePath ) ;
335
+
336
+ await fs . promises . writeFile ( filePath , buffer ) ;
337
+
338
+ // Verify file was written
339
+ const stats = await fs . promises . stat ( filePath ) ;
340
+ console . log ( 'File written successfully. Size:' , stats . size , 'bytes' ) ;
341
+
342
+ res . json ( {
343
+ success : true ,
344
+ message : 'Single file downloaded successfully' ,
345
+ downloadPath : downloadDir ,
346
+ fileCount : 1 ,
347
+ fileName : fileName ,
348
+ fileSize : stats . size
349
+ } ) ;
350
+
315
351
} catch ( error ) {
352
+ console . error ( 'Detailed error:' , error ) ;
316
353
res . status ( 500 ) . json ( {
317
- error : 'Failed to forward request to BagelDB' ,
318
- details : error . message
354
+ error : 'Failed to download files from BagelDB' ,
355
+ details : error . message ,
356
+ stack : error . stack
319
357
} ) ;
320
358
}
321
359
}
0 commit comments