-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfile.ts
More file actions
202 lines (185 loc) · 6.75 KB
/
file.ts
File metadata and controls
202 lines (185 loc) · 6.75 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { FileMetadata } from '../pod/types'
import { assertAccount } from '../account/utils'
import { assertPodName, getExtendedPodsListByAccountData, META_VERSION } from '../pod/utils'
import { getUnixTimestamp } from '../utils/time'
import { stringToBytes } from '../utils/bytes'
import { AccountData } from '../account/account-data'
import {
assertFullPathWithName,
createFileShareInfo,
extractPathInfo,
getSharedFileInfo,
updateFileMetadata,
uploadBytes,
} from './utils'
import { writeFeedData } from '../feed/api'
import { downloadData } from './handler'
import { blocksToManifest, getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter'
import { Blocks, DataUploadOptions, FileReceiveOptions, FileShareInfo } from './types'
import { addEntryToDirectory, removeEntryFromDirectory } from '../content-items/handler'
import { Data, Reference } from '@ethersphere/bee-js'
import { getRawMetadata } from '../content-items/utils'
import { assertRawFileMetadata, combine } from '../directory/utils'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
/**
* Files management class
*/
export class File {
public readonly defaultUploadOptions: DataUploadOptions = {
blockSize: 1000000,
contentType: '',
}
constructor(private accountData: AccountData) {}
/**
* Downloads file content
*
* @param podName pod where file is stored
* @param fullPath full path of the file
*/
async downloadData(podName: string, fullPath: string): Promise<Data> {
assertAccount(this.accountData)
assertPodName(podName)
assertFullPathWithName(fullPath)
assertPodName(podName)
const { podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
return downloadData(
this.accountData.connection.bee,
fullPath,
podAddress,
pod.password,
this.accountData.connection.options?.downloadOptions,
)
}
/**
* Uploads file content
*
* @param podName pod where file is stored
* @param fullPath full path of the file
* @param data file content
* @param options upload options
*/
async uploadData(
podName: string,
fullPath: string,
data: Uint8Array | string,
options?: DataUploadOptions,
): Promise<FileMetadata> {
options = { ...this.defaultUploadOptions, ...options }
assertAccount(this.accountData)
assertPodName(podName)
assertFullPathWithName(fullPath)
assertPodName(podName)
data = typeof data === 'string' ? stringToBytes(data) : data
const connection = this.accountData.connection
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const pathInfo = extractPathInfo(fullPath)
const now = getUnixTimestamp()
const blocksCount = Math.ceil(data.length / options.blockSize)
const blocks: Blocks = { blocks: [] }
for (let i = 0; i < blocksCount; i++) {
const currentBlock = data.slice(i * options.blockSize, (i + 1) * options.blockSize)
const result = await uploadBytes(connection, currentBlock)
blocks.blocks.push({
size: currentBlock.length,
compressedSize: currentBlock.length,
reference: result.reference,
})
}
const manifestBytes = stringToBytes(blocksToManifest(blocks))
const blocksReference = (await uploadBytes(connection, manifestBytes)).reference
const meta: FileMetadata = {
version: META_VERSION,
filePath: pathInfo.path,
fileName: pathInfo.filename,
fileSize: data.length,
blockSize: options.blockSize,
contentType: options.contentType,
compression: '',
creationTime: now,
accessTime: now,
modificationTime: now,
blocksReference,
}
await addEntryToDirectory(connection, podWallet, pod.password, pathInfo.path, pathInfo.filename, true)
await writeFeedData(connection, fullPath, getFileMetadataRawBytes(meta), podWallet.privateKey, pod.password)
return meta
}
/**
* Deletes a file
*
* @param podName pod where file is located
* @param fullPath full path of the file
*/
async delete(podName: string, fullPath: string): Promise<void> {
assertAccount(this.accountData)
assertFullPathWithName(fullPath)
assertPodName(podName)
const pathInfo = extractPathInfo(fullPath)
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
await removeEntryFromDirectory(
this.accountData.connection,
podWallet,
pod.password,
pathInfo.path,
pathInfo.filename,
true,
)
}
/**
* Shares file information
*
* @param podName pod where file is stored
* @param fullPath full path of the file
*/
async share(podName: string, fullPath: string): Promise<Reference> {
assertAccount(this.accountData)
assertFullPathWithName(fullPath)
assertPodName(podName)
const connection = this.accountData.connection
const { podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const meta = (await getRawMetadata(connection.bee, fullPath, podAddress, pod.password)).metadata
assertRawFileMetadata(meta)
const data = JSON.stringify(createFileShareInfo(meta))
return (await uploadBytes(connection, stringToBytes(data))).reference
}
/**
* Gets shared file information
*
* @param reference swarm reference with shared file information
*
* @returns shared file information
*/
async getSharedInfo(reference: string | EncryptedReference): Promise<FileShareInfo> {
assertAccount(this.accountData)
assertEncryptedReference(reference)
return getSharedFileInfo(this.accountData.connection.bee, reference)
}
/**
* Saves shared file to a personal account
*
* @param podName pod where file is stored
* @param parentPath the path to the file to save
* @param reference swarm reference with shared file information
* @param options save options
*
* @returns saved file metadata
*/
async saveShared(
podName: string,
parentPath: string,
reference: string | EncryptedReference,
options?: FileReceiveOptions,
): Promise<FileMetadata> {
assertPodName(podName)
const sharedInfo = await this.getSharedInfo(reference)
const connection = this.accountData.connection
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
let meta = rawFileMetadataToFileMetadata(sharedInfo.meta)
const fileName = options?.name ?? sharedInfo.meta.fileName
meta = updateFileMetadata(meta, parentPath, fileName)
const fullPath = combine(parentPath, fileName)
await addEntryToDirectory(connection, podWallet, pod.password, parentPath, fileName, true)
await writeFeedData(connection, fullPath, getFileMetadataRawBytes(meta), podWallet.privateKey, pod.password)
return meta
}
}