Skip to content

Commit

Permalink
fix: collection upload with relative path (#73)
Browse files Browse the repository at this point in the history
* fix: collection upload with relative path

* test: add test data files

* chore: fix linter issues
  • Loading branch information
agazso authored Jan 22, 2021
1 parent c81a359 commit b8f5c90
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/modules/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,31 @@ function isCollection(data: unknown): data is Collection<Uint8Array> {
* @param dir absolute path to the directory
* @param recursive flag that specifies if the directory should be recursively walked and get files in those directories.
*/
export async function buildCollection(dir: string, recursive = true): Promise<Collection<Uint8Array>> {
export function buildCollection(dir: string, recursive = true): Promise<Collection<Uint8Array>> {
return buildCollectionRelative(dir, '', recursive)
}

async function buildCollectionRelative(
dir: string,
relativePath: string,
recursive = true,
): Promise<Collection<Uint8Array>> {
// Handles case when the dir is not existing or it is a file ==> throws an error
const entries = await fs.promises.opendir(dir)
const dirname = path.join(dir, relativePath)
const entries = await fs.promises.opendir(dirname)
let collection: Collection<Uint8Array> = []

for await (const entry of entries) {
const fullPath = path.join(dir, entry.name)
const fullPath = path.join(dir, relativePath, entry.name)
const entryPath = path.join(relativePath, entry.name)

if (entry.isFile()) {
collection.push({
path: fullPath,
path: entryPath,
data: new Uint8Array(await fs.promises.readFile(fullPath)),
})
} else if (entry.isDirectory() && recursive) {
collection = [...(await buildCollection(fullPath, recursive)), ...collection]
collection = [...(await buildCollectionRelative(dir, entryPath, recursive)), ...collection]
}
}

Expand Down
1 change: 1 addition & 0 deletions test/data/sub/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
22 changes: 18 additions & 4 deletions test/modules/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,27 @@ describe('modules/collection', () => {
it('should store and retrieve actual directory', async () => {
const path = 'test/data/'
const dir = `./${path}`
const name = '1.txt'
const data = Uint8Array.from([49, 10])
const file3Name = '3.txt'
const subDir = 'sub/'
const data = Uint8Array.from([51, 10])
const directoryStructure = await collection.buildCollection(dir)
const hash = await collection.upload(BEE_URL, directoryStructure)

const file1 = await collection.download(BEE_URL, hash, `${path}${name}`)
expect(file1.name).toEqual(name)
const file3 = await collection.download(BEE_URL, hash, `${subDir}${file3Name}`)
expect(file3.name).toEqual(file3Name)
expect(file3.data).toEqual(data)
})

it('should store and retrieve actual directory with index document', async () => {
const path = 'test/data/'
const dir = `./${path}`
const fileName = '1.txt'
const data = Uint8Array.from([49, 10])
const directoryStructure = await collection.buildCollection(dir)
const hash = await collection.upload(BEE_URL, directoryStructure, { indexDocument: `${fileName}` })

const file1 = await collection.download(BEE_URL, hash)
expect(file1.name).toEqual(fileName)
expect(file1.data).toEqual(data)
})
})

0 comments on commit b8f5c90

Please sign in to comment.