1
- import * as fsDefault from "node:fs" ;
2
- import { promises as fsPromises } from "node:fs" ;
3
- import * as path from "node:path" ;
4
- import * as os from "node:os" ;
5
1
import { files as DropboxFiles } from "dropbox/types/dropbox_types.js" ;
6
2
import { Dropbox , DropboxOptions , DropboxAuth } from "dropbox" ;
7
3
@@ -46,10 +42,6 @@ export interface DropboxLoaderConfig {
46
42
* Defaults to `"file"`.
47
43
*/
48
44
mode ?: "file" | "directory" ;
49
- /**
50
- * The file system module to use. Defaults to Node's `fs` module.
51
- */
52
- fs ?: typeof fsDefault ;
53
45
/**
54
46
* The UnstructuredLoader class to use for processing files.
55
47
* Defaults to the UnstructuredLoader provided by `langchain`.
@@ -111,11 +103,6 @@ export class DropboxLoader extends BaseDocumentLoader {
111
103
*/
112
104
protected mode : "file" | "directory" ;
113
105
114
- /**
115
- * The file system module to use.
116
- */
117
- protected fs : typeof fsDefault ;
118
-
119
106
/**
120
107
* The UnstructuredLoader class to use for processing files.
121
108
*/
@@ -133,7 +120,6 @@ export class DropboxLoader extends BaseDocumentLoader {
133
120
filePaths,
134
121
recursive = false ,
135
122
mode = "file" ,
136
- fs = fsDefault ,
137
123
UnstructuredLoader = UnstructuredLoaderDefault ,
138
124
} : DropboxLoaderConfig ) {
139
125
super ( ) ;
@@ -147,9 +133,7 @@ export class DropboxLoader extends BaseDocumentLoader {
147
133
this . filePaths = filePaths || [ ] ;
148
134
this . recursive = recursive ;
149
135
this . mode = mode ;
150
- this . fs = fs ;
151
136
this . _UnstructuredLoader = UnstructuredLoader ;
152
-
153
137
this . dropboxClient = DropboxLoader . _getDropboxClient ( clientOptions ) ;
154
138
}
155
139
@@ -236,15 +220,13 @@ export class DropboxLoader extends BaseDocumentLoader {
236
220
/**
237
221
* Downloads a file from Dropbox, processes it into `Document` instances using the `UnstructuredLoader`,
238
222
* and returns the resulting documents. This method handles the entire lifecycle of the file processing,
239
- * including downloading, temporary storage, processing, metadata augmentation, and cleanup .
223
+ * including downloading, processing, and metadata augmentation.
240
224
*
241
225
* @param filePath - The path to the file in Dropbox.
242
226
* @returns A promise that resolves to an array of `Document` instances generated from a dropbox file.
243
227
*/
244
228
private async _loadFile ( filePath : string ) : Promise < Document [ ] > {
245
229
const client : Dropbox = this . dropboxClient ;
246
- let tempDir : string | undefined ;
247
- let localFilePath : string | undefined ;
248
230
try {
249
231
const fetchRes = await client . filesDownload ( { path : filePath } ) ;
250
232
const fileMetadata =
@@ -258,26 +240,12 @@ export class DropboxLoader extends BaseDocumentLoader {
258
240
259
241
const fileBinary = fileMetadata . fileBinary ;
260
242
261
- // Create temporary directory
262
- tempDir = await fsPromises . mkdtemp (
263
- path . join ( os . tmpdir ( ) , "dropboxfileloader-" )
264
- ) ;
265
-
266
- // Normalize the file path
267
- const normalizedFilePath = filePath . startsWith ( "/" )
268
- ? filePath . slice ( 1 )
269
- : filePath ;
270
- localFilePath = path . join ( tempDir , normalizedFilePath ) ;
271
-
272
- await fsPromises . mkdir ( path . dirname ( localFilePath ) , { recursive : true } ) ;
273
-
274
- await fsPromises . writeFile ( localFilePath , fileBinary , {
275
- encoding : "binary" ,
276
- } ) ;
277
-
278
243
// Create an unstructured loader and load the file.
279
244
const unstructuredLoader = new this . _UnstructuredLoader (
280
- localFilePath ,
245
+ {
246
+ fileName : fileMetadata . name ,
247
+ buffer : fileBinary ,
248
+ } ,
281
249
this . unstructuredOptions
282
250
) ;
283
251
const docs = await unstructuredLoader . load ( ) ;
@@ -293,22 +261,6 @@ export class DropboxLoader extends BaseDocumentLoader {
293
261
console . error ( `Error processing file ${ filePath } :` , error ) ;
294
262
console . error ( `File ${ filePath } was skipped.` ) ;
295
263
return [ ] ; // Proceed to the next file
296
- } finally {
297
- // Cleanup temporary files
298
- if ( localFilePath ) {
299
- try {
300
- await fsPromises . unlink ( localFilePath ) ;
301
- } catch ( err ) {
302
- console . warn ( `Failed to delete file ${ localFilePath } :` , err ) ;
303
- }
304
- }
305
- if ( tempDir ) {
306
- try {
307
- await fsPromises . rm ( tempDir , { recursive : true , force : true } ) ;
308
- } catch ( err ) {
309
- console . warn ( `Failed to delete temp directory ${ tempDir } :` , err ) ;
310
- }
311
- }
312
264
}
313
265
}
314
266
0 commit comments