Skip to content

Commit 7027578

Browse files
committed
remove fs usage
1 parent a95ffa0 commit 7027578

File tree

2 files changed

+6
-54
lines changed

2 files changed

+6
-54
lines changed

libs/langchain-community/src/document_loaders/tests/dropbox.int.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe("DropboxLoader Integration Tests", () => {
184184
});
185185

186186
it("should recursively load all documents from a Dropbox folder", async () => {
187-
const dropboxFilenames = allTestFiles.map((path) => path.toLowerCase());
187+
const dropboxFilenames = allTestFiles;
188188

189189
const clientOptions: DropboxOptions = {};
190190
const unstructuredOptions: UnstructuredLoaderOptions = {

libs/langchain-community/src/document_loaders/web/dropbox.ts

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
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";
51
import { files as DropboxFiles } from "dropbox/types/dropbox_types.js";
62
import { Dropbox, DropboxOptions, DropboxAuth } from "dropbox";
73

@@ -46,10 +42,6 @@ export interface DropboxLoaderConfig {
4642
* Defaults to `"file"`.
4743
*/
4844
mode?: "file" | "directory";
49-
/**
50-
* The file system module to use. Defaults to Node's `fs` module.
51-
*/
52-
fs?: typeof fsDefault;
5345
/**
5446
* The UnstructuredLoader class to use for processing files.
5547
* Defaults to the UnstructuredLoader provided by `langchain`.
@@ -111,11 +103,6 @@ export class DropboxLoader extends BaseDocumentLoader {
111103
*/
112104
protected mode: "file" | "directory";
113105

114-
/**
115-
* The file system module to use.
116-
*/
117-
protected fs: typeof fsDefault;
118-
119106
/**
120107
* The UnstructuredLoader class to use for processing files.
121108
*/
@@ -133,7 +120,6 @@ export class DropboxLoader extends BaseDocumentLoader {
133120
filePaths,
134121
recursive = false,
135122
mode = "file",
136-
fs = fsDefault,
137123
UnstructuredLoader = UnstructuredLoaderDefault,
138124
}: DropboxLoaderConfig) {
139125
super();
@@ -147,9 +133,7 @@ export class DropboxLoader extends BaseDocumentLoader {
147133
this.filePaths = filePaths || [];
148134
this.recursive = recursive;
149135
this.mode = mode;
150-
this.fs = fs;
151136
this._UnstructuredLoader = UnstructuredLoader;
152-
153137
this.dropboxClient = DropboxLoader._getDropboxClient(clientOptions);
154138
}
155139

@@ -236,15 +220,13 @@ export class DropboxLoader extends BaseDocumentLoader {
236220
/**
237221
* Downloads a file from Dropbox, processes it into `Document` instances using the `UnstructuredLoader`,
238222
* 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.
240224
*
241225
* @param filePath - The path to the file in Dropbox.
242226
* @returns A promise that resolves to an array of `Document` instances generated from a dropbox file.
243227
*/
244228
private async _loadFile(filePath: string): Promise<Document[]> {
245229
const client: Dropbox = this.dropboxClient;
246-
let tempDir: string | undefined;
247-
let localFilePath: string | undefined;
248230
try {
249231
const fetchRes = await client.filesDownload({ path: filePath });
250232
const fileMetadata =
@@ -258,26 +240,12 @@ export class DropboxLoader extends BaseDocumentLoader {
258240

259241
const fileBinary = fileMetadata.fileBinary;
260242

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-
278243
// Create an unstructured loader and load the file.
279244
const unstructuredLoader = new this._UnstructuredLoader(
280-
localFilePath,
245+
{
246+
fileName: fileMetadata.name,
247+
buffer: fileBinary,
248+
},
281249
this.unstructuredOptions
282250
);
283251
const docs = await unstructuredLoader.load();
@@ -293,22 +261,6 @@ export class DropboxLoader extends BaseDocumentLoader {
293261
console.error(`Error processing file ${filePath}:`, error);
294262
console.error(`File ${filePath} was skipped.`);
295263
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-
}
312264
}
313265
}
314266

0 commit comments

Comments
 (0)