88
99import path from 'path' ;
1010import fs from 'fs-extra' ;
11- import { fileURLToPath } from 'url' ;
1211import log from '../../lib/logger.js' ;
13- import config from '../../../config .js' ;
12+ import { tmpDir } from '../../lib/tempManager .js' ;
1413import { fetch , parseArgsToFetchOptions , getHeader } from '../../addon/bridge.js' ;
1514
16- const __filename = fileURLToPath ( import . meta. url ) ;
17- const __dirname = path . dirname ( __filename ) ;
18-
1915export default async function ( { argsArray } ) {
2016 const { url, options } = parseArgsToFetchOptions ( argsArray ) ;
2117 try {
@@ -37,10 +33,6 @@ export default async function ({ argsArray }) {
3733 }
3834 const isDirectDownload = contentType . startsWith ( 'image/' ) || contentType . startsWith ( 'audio/' ) || contentType . startsWith ( 'video/' ) || contentType . startsWith ( 'application/pdf' ) || contentType . startsWith ( 'application/zip' ) || contentType . startsWith ( 'application/octet-stream' ) ;
3935 if ( isDirectDownload ) {
40- const tempDir = config . paths . tempDir ;
41- if ( ! fs . existsSync ( tempDir ) ) {
42- fs . mkdirSync ( tempDir , { recursive : true } ) ;
43- }
4436 let extension = '' ;
4537 try {
4638 const urlPath = new URL ( url ) . pathname ;
@@ -54,9 +46,8 @@ export default async function ({ argsArray }) {
5446 extension = ctParts [ 1 ] . split ( ';' ) [ 0 ] ;
5547 }
5648 }
57- const tempFileName = `${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 9 ) } .${ extension || 'tmp' } ` ;
58- const tempFilePath = path . join ( tempDir , tempFileName ) ;
59- fs . writeFileSync ( tempFilePath , bodyBuffer ) ;
49+ const tempFilePath = tmpDir . createTempFile ( extension , 'fetch-' ) ;
50+ await fs . writeFile ( tempFilePath , bodyBuffer ) ;
6051 return {
6152 type : 'local_file' ,
6253 path : tempFilePath
0 commit comments