-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhandler.ts
More file actions
143 lines (130 loc) · 5 KB
/
handler.ts
File metadata and controls
143 lines (130 loc) · 5 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { writeFeedData } from '../feed/api'
import { EthAddress } from '@ethersphere/bee-js/dist/types/utils/eth'
import { Bee, PrivateKeyBytes, Reference, RequestOptions } from '@ethersphere/bee-js'
import {
assertDirectoryName,
assertPartsLength,
assertRawDirectoryMetadata,
assertRawFileMetadata,
combine,
getPathFromParts,
getPathParts,
} from './utils'
import { DIRECTORY_TOKEN, FILE_TOKEN } from '../file/handler'
import { getUnixTimestamp } from '../utils/time'
import { createRawDirectoryMetadata, META_VERSION } from '../pod/utils'
import { Connection } from '../connection/connection'
import { utils } from 'ethers'
import { addEntryToDirectory } from '../content-items/handler'
import { DirectoryItem } from '../content-items/directory-item'
import { FileItem } from '../content-items/file-item'
import { getRawMetadata } from '../content-items/utils'
import { PodPasswordBytes } from '../utils/encryption'
import { preparePrivateKey } from '../utils/wallet'
export const MAX_DIRECTORY_NAME_LENGTH = 100
/**
* Get files and directories under path with recursion or not
*
* @param bee Bee instance
* @param path path to start searching from
* @param address Ethereum address of the pod which owns the path
* @param podPassword bytes for data encryption from pod metadata
* @param isRecursive search with recursion or not
* @param downloadOptions options for downloading
*/
export async function readDirectory(
bee: Bee,
path: string,
address: EthAddress,
podPassword: PodPasswordBytes,
isRecursive?: boolean,
downloadOptions?: RequestOptions,
): Promise<DirectoryItem> {
const parentRawDirectoryMetadata = (await getRawMetadata(bee, path, address, podPassword, downloadOptions)).metadata
assertRawDirectoryMetadata(parentRawDirectoryMetadata)
const resultDirectoryItem = DirectoryItem.fromRawDirectoryMetadata(parentRawDirectoryMetadata)
if (!parentRawDirectoryMetadata.fileOrDirNames) {
return resultDirectoryItem
}
for (let item of parentRawDirectoryMetadata.fileOrDirNames) {
const isFile = item.startsWith(FILE_TOKEN)
const isDirectory = item.startsWith(DIRECTORY_TOKEN)
if (isFile) {
item = combine(path, item.substring(FILE_TOKEN.length))
const data = (await getRawMetadata(bee, item, address, podPassword, downloadOptions)).metadata
assertRawFileMetadata(data)
resultDirectoryItem.content.push(FileItem.fromRawFileMetadata(data))
} else if (isDirectory) {
item = combine(path, item.substring(DIRECTORY_TOKEN.length))
const data = (await getRawMetadata(bee, item, address, podPassword, downloadOptions)).metadata
assertRawDirectoryMetadata(data)
const currentMetadata = DirectoryItem.fromRawDirectoryMetadata(data)
if (isRecursive) {
currentMetadata.content = (
await readDirectory(bee, item, address, podPassword, isRecursive, downloadOptions)
).content
}
resultDirectoryItem.content.push(currentMetadata)
}
}
return resultDirectoryItem
}
/**
* Creates directory metadata for a given directory path and upload it to the network
*
* @param connection Bee connection
* @param path parent path
* @param name name of the directory
* @param podPassword bytes for data encryption from pod metadata
* @param privateKey private key for uploading data to the network
*/
async function createDirectoryInfo(
connection: Connection,
path: string,
name: string,
podPassword: PodPasswordBytes,
privateKey: PrivateKeyBytes,
): Promise<Reference> {
const now = getUnixTimestamp()
const metadata = createRawDirectoryMetadata(META_VERSION, path, name, now, now, now)
return writeFeedData(connection, combine(path, name), metadata, privateKey, podPassword)
}
/**
* Creates root directory for the pod that tied to the private key
*
* @param connection Bee connection
* @param podPassword bytes for data encryption
* @param privateKey private key for uploading data to the network
*/
export async function createRootDirectory(
connection: Connection,
podPassword: PodPasswordBytes,
privateKey: PrivateKeyBytes,
): Promise<Reference> {
return createDirectoryInfo(connection, '', '/', podPassword, privateKey)
}
/**
* Creates directory under the pod
*
* @param connection Bee connection
* @param fullPath path to the directory
* @param podWallet pod wallet
* @param podPassword bytes for decrypting pod content
* @param downloadOptions options for downloading
*/
export async function createDirectory(
connection: Connection,
fullPath: string,
podWallet: utils.HDNode,
podPassword: PodPasswordBytes,
downloadOptions?: RequestOptions,
): Promise<void> {
const parts = getPathParts(fullPath)
assertPartsLength(parts)
const name = parts[parts.length - 1]
assertDirectoryName(name)
const privateKey = preparePrivateKey(podWallet.privateKey)
const parentPath = getPathFromParts(parts, 1)
await addEntryToDirectory(connection, podWallet, podPassword, parentPath, name, false, downloadOptions)
await createDirectoryInfo(connection, parentPath, name, podPassword, privateKey)
}