-
Notifications
You must be signed in to change notification settings - Fork 528
handle folders in uploadFiles #1398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72322aa
handle folders in uploadFiles
coyotte508 7c0ca44
add tests
coyotte508 52f9c02
fix
coyotte508 0a3be81
fix sub-paths
coyotte508 590c930
use tmpDir everywhre for tests
coyotte508 568375d
fixup! use tmpDir everywhre for tests
coyotte508 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,4 +100,4 @@ describe("createRepo", () => { | |
credentials: { accessToken: TEST_ACCESS_TOKEN }, | ||
}); | ||
}); | ||
}, 10_000); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,4 +78,4 @@ describe("deleteFiles", () => { | |
}); | ||
} | ||
}); | ||
}, 10_000); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { assert, it, describe } from "vitest"; | ||
|
||
import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../test/consts"; | ||
import type { RepoId } from "../types/public"; | ||
import { insecureRandomString } from "../utils/insecureRandomString"; | ||
import { createRepo } from "./create-repo"; | ||
import { deleteRepo } from "./delete-repo"; | ||
import { downloadFile } from "./download-file"; | ||
import { uploadFiles } from "./upload-files"; | ||
import { mkdir } from "fs/promises"; | ||
import { writeFile } from "fs/promises"; | ||
import { pathToFileURL } from "url"; | ||
import { tmpdir } from "os"; | ||
|
||
describe("uploadFiles", () => { | ||
it("should upload local folder", async () => { | ||
const tmpDir = tmpdir(); | ||
|
||
await mkdir(`${tmpDir}/test-folder/sub`, { recursive: true }); | ||
|
||
await writeFile(`${tmpDir}/test-folder/sub/file1.txt`, "file1"); | ||
await writeFile(`${tmpDir}/test-folder/sub/file2.txt`, "file2"); | ||
|
||
await writeFile(`${tmpDir}/test-folder/file3.txt`, "file3"); | ||
await writeFile(`${tmpDir}/test-folder/file4.txt`, "file4"); | ||
|
||
const repoName = `${TEST_USER}/TEST-${insecureRandomString()}`; | ||
const repo = { type: "model", name: repoName } satisfies RepoId; | ||
|
||
try { | ||
const result = await createRepo({ | ||
accessToken: TEST_ACCESS_TOKEN, | ||
repo, | ||
hubUrl: TEST_HUB_URL, | ||
}); | ||
|
||
assert.deepStrictEqual(result, { | ||
repoUrl: `${TEST_HUB_URL}/${repoName}`, | ||
}); | ||
|
||
await uploadFiles({ | ||
accessToken: TEST_ACCESS_TOKEN, | ||
repo, | ||
files: [pathToFileURL(`${tmpDir}/test-folder`)], | ||
hubUrl: TEST_HUB_URL, | ||
}); | ||
|
||
let content = await downloadFile({ | ||
repo, | ||
path: "test-folder/sub/file1.txt", | ||
hubUrl: TEST_HUB_URL, | ||
}); | ||
|
||
assert.strictEqual(await content?.text(), "file1"); | ||
|
||
content = await downloadFile({ | ||
repo, | ||
path: "test-folder/file3.txt", | ||
hubUrl: TEST_HUB_URL, | ||
}); | ||
|
||
assert.strictEqual(await content?.text(), `file3`); | ||
} finally { | ||
await deleteRepo({ | ||
repo, | ||
accessToken: TEST_ACCESS_TOKEN, | ||
hubUrl: TEST_HUB_URL, | ||
}); | ||
} | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,4 +92,4 @@ describe("uploadFiles", () => { | |
}); | ||
} | ||
}); | ||
}, 10_000); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { WebBlob } from "./WebBlob"; | ||
import { isFrontend } from "./isFrontend"; | ||
|
||
/** | ||
* This function allow to retrieve either a FileBlob or a WebBlob from a URL. | ||
* | ||
* From the backend: | ||
* - support local files | ||
* - support local folders | ||
* - support http resources with absolute URLs | ||
* | ||
* From the frontend: | ||
* - support http resources with absolute or relative URLs | ||
*/ | ||
export async function createBlobs( | ||
url: URL, | ||
destPath: string, | ||
opts?: { fetch?: typeof fetch; maxFolderDepth?: number } | ||
): Promise<Array<{ path: string; blob: Blob }>> { | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
const blob = await WebBlob.create(url, { fetch: opts?.fetch }); | ||
return [{ path: destPath, blob }]; | ||
} | ||
|
||
if (isFrontend) { | ||
throw new TypeError(`Unsupported URL protocol "${url.protocol}"`); | ||
} | ||
|
||
if (url.protocol === "file:") { | ||
const { FileBlob } = await import("./FileBlob"); | ||
const { subPaths } = await import("./sub-paths"); | ||
const paths = await subPaths(url, opts?.maxFolderDepth); | ||
|
||
if (paths.length === 1 && paths[0].relativePath === ".") { | ||
const blob = await FileBlob.create(url); | ||
return [{ path: destPath, blob }]; | ||
} | ||
|
||
return Promise.all( | ||
paths.map(async (path) => ({ | ||
path: `${destPath}/${path.relativePath}`.replace(/\/[.]$/, "").replaceAll("//", "/"), | ||
blob: await FileBlob.create(new URL(path.path)), | ||
})) | ||
); | ||
} | ||
|
||
throw new TypeError(`Unsupported URL protocol "${url.protocol}"`); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { mkdir, writeFile } from "fs/promises"; | ||
import { tmpdir } from "os"; | ||
import { describe, expect, it } from "vitest"; | ||
import { subPaths } from "./sub-paths"; | ||
import { pathToFileURL } from "url"; | ||
|
||
describe("sub-paths", () => { | ||
it("should retrieve all sub-paths of a directory", async () => { | ||
const tmpDir = tmpdir(); | ||
|
||
await mkdir(`${tmpDir}/test-dir/sub`, { recursive: true }); | ||
|
||
await writeFile(`${tmpDir}/test-dir/sub/file1.txt`, "file1"); | ||
await writeFile(`${tmpDir}/test-dir/sub/file2.txt`, "file2"); | ||
await writeFile(`${tmpDir}/test-dir/file3.txt`, "file3"); | ||
await writeFile(`${tmpDir}/test-dir/file4.txt`, "file4"); | ||
const result = await subPaths(pathToFileURL(`${tmpDir}/test-dir`)); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
path: pathToFileURL(`${tmpDir}/test-dir/file3.txt`), | ||
relativePath: "file3.txt", | ||
}, | ||
{ | ||
path: pathToFileURL(`${tmpDir}/test-dir/file4.txt`), | ||
relativePath: "file4.txt", | ||
}, | ||
|
||
{ | ||
path: pathToFileURL(`${tmpDir}/test-dir/sub/file1.txt`), | ||
relativePath: "sub/file1.txt", | ||
}, | ||
{ | ||
path: pathToFileURL(`${tmpDir}/test-dir/sub/file2.txt`), | ||
relativePath: "sub/file2.txt", | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { readdir, stat } from "node:fs/promises"; | ||
import { fileURLToPath, pathToFileURL } from "node:url"; | ||
|
||
/** | ||
* Recursively retrieves all sub-paths of a given directory up to a specified depth. | ||
*/ | ||
export async function subPaths( | ||
path: URL, | ||
maxDepth = 10 | ||
): Promise< | ||
Array<{ | ||
path: URL; | ||
relativePath: string; | ||
}> | ||
> { | ||
const state = await stat(path); | ||
if (!state.isDirectory()) { | ||
return [{ path, relativePath: "." }]; | ||
} | ||
|
||
const files = await readdir(path, { withFileTypes: true }); | ||
const ret: Array<{ path: URL; relativePath: string }> = []; | ||
for (const file of files) { | ||
const filePath = pathToFileURL(fileURLToPath(path) + "/" + file.name); | ||
if (file.isDirectory()) { | ||
ret.push( | ||
...(await subPaths(filePath, maxDepth - 1)).map((subPath) => ({ | ||
...subPath, | ||
relativePath: `${file.name}/${subPath.relativePath}`, | ||
})) | ||
); | ||
} else { | ||
ret.push({ path: filePath, relativePath: file.name }); | ||
} | ||
} | ||
|
||
return ret; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huggingface_hub
usesglob
cc @Wauplin @hanouticelinaUnfortunately support is only experimental for nodejs: https://nodejs.org/api/fs.html#fspromisesglobpattern-options
So using a
maxFolderDepth
(to avoid potential symlink recursions)