/
collection.node.ts
74 lines (62 loc) · 2.05 KB
/
collection.node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import fs from 'fs'
import path from 'path'
import { Collection } from '../types'
/**
* Creates array in the format of Collection with data loaded from directory on filesystem.
* The function loads all the data into memory!
*
* @param dir path to the directory
*/
export async function makeCollectionFromFS(dir: string): Promise<Collection<Uint8Array>> {
if (typeof dir !== 'string') {
throw new TypeError('dir has to be string!')
}
if (dir === '') {
throw new TypeError('dir must not be empty string!')
}
return buildCollectionRelative(dir, '')
}
async function buildCollectionRelative(dir: string, relativePath: string): Promise<Collection<Uint8Array>> {
// Handles case when the dir is not existing or it is a file ==> throws an error
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, relativePath, entry.name)
const entryPath = path.join(relativePath, entry.name)
if (entry.isFile()) {
collection.push({
path: entryPath,
data: new Uint8Array(await fs.promises.readFile(fullPath)),
})
} else if (entry.isDirectory()) {
collection = [...(await buildCollectionRelative(dir, entryPath)), ...collection]
}
}
return collection
}
/**
* Calculate folder size recursively
*
* @param dir the path to the folder to check
* @returns size in bytes
*/
export async function getFolderSize(dir: string): Promise<number> {
if (typeof dir !== 'string') {
throw new TypeError('dir has to be string!')
}
if (dir === '') {
throw new TypeError('dir must not be empty string!')
}
const entries = await fs.promises.opendir(dir)
let size = 0
for await (const entry of entries) {
if (entry.isFile()) {
const stats = await fs.promises.stat(path.join(dir, entry.name))
size += stats.size
} else if (entry.isDirectory()) {
size += await getFolderSize(path.join(dir, entry.name))
}
}
return size
}