bedrock-ts-sdk 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +42 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +42 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -132,6 +132,19 @@ const files = await client.files.uploadFiles([
|
|
|
132
132
|
]);
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
+
#### Edit Files
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
const [file] = await client.files.uploadFiles([
|
|
139
|
+
{
|
|
140
|
+
name: 'document.txt',
|
|
141
|
+
path: '/documents/document.txt',
|
|
142
|
+
content: fileBuffer, // Buffer or File
|
|
143
|
+
},
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
const editedFile = await client.files.editFileContent(file, editedFileBuffer);
|
|
147
|
+
```
|
|
135
148
|
#### List Files
|
|
136
149
|
|
|
137
150
|
```typescript
|
package/dist/index.d.mts
CHANGED
|
@@ -674,6 +674,7 @@ declare class FileService {
|
|
|
674
674
|
* @returns Array of uploaded file info
|
|
675
675
|
*/
|
|
676
676
|
uploadFiles(files: FileInput[], directoryPath?: string): Promise<FileFullInfo[]>;
|
|
677
|
+
editFileContent(fileInfo: FileFullInfo, newContent: Buffer): Promise<FileFullInfo>;
|
|
677
678
|
/**
|
|
678
679
|
* Download and decrypt a file
|
|
679
680
|
* @param fileInfo - File information
|
package/dist/index.d.ts
CHANGED
|
@@ -674,6 +674,7 @@ declare class FileService {
|
|
|
674
674
|
* @returns Array of uploaded file info
|
|
675
675
|
*/
|
|
676
676
|
uploadFiles(files: FileInput[], directoryPath?: string): Promise<FileFullInfo[]>;
|
|
677
|
+
editFileContent(fileInfo: FileFullInfo, newContent: Buffer): Promise<FileFullInfo>;
|
|
677
678
|
/**
|
|
678
679
|
* Download and decrypt a file
|
|
679
680
|
* @param fileInfo - File information
|
package/dist/index.js
CHANGED
|
@@ -943,8 +943,16 @@ var FileService = class {
|
|
|
943
943
|
const aleph = this.core.getAlephService();
|
|
944
944
|
const publicKey = this.core.getPublicKey();
|
|
945
945
|
const uploadedFiles = [];
|
|
946
|
+
const existingFiles = await this.listFiles();
|
|
947
|
+
const filesToUpload = files.filter((file) => {
|
|
948
|
+
const filterIn = !existingFiles.some(
|
|
949
|
+
(existingFile) => !existingFile.deleted_at && existingFile.path === (directoryPath ? `${directoryPath}${file.path}` : file.path)
|
|
950
|
+
);
|
|
951
|
+
console.log(`Keeping ${file} in ? ${filterIn ? "yes" : "no"}`);
|
|
952
|
+
return filterIn;
|
|
953
|
+
});
|
|
946
954
|
try {
|
|
947
|
-
for (const file of
|
|
955
|
+
for (const file of filesToUpload) {
|
|
948
956
|
const key = EncryptionService.generateKey();
|
|
949
957
|
const iv = EncryptionService.generateIv();
|
|
950
958
|
let fileBuffer;
|
|
@@ -985,6 +993,39 @@ var FileService = class {
|
|
|
985
993
|
throw new FileError(`Failed to upload files: ${error.message}`);
|
|
986
994
|
}
|
|
987
995
|
}
|
|
996
|
+
async editFileContent(fileInfo, newContent) {
|
|
997
|
+
const aleph = this.core.getAlephService();
|
|
998
|
+
const privateKey = this.core.getSubAccountPrivateKey();
|
|
999
|
+
try {
|
|
1000
|
+
const postResult = await aleph.updatePost(
|
|
1001
|
+
POST_TYPES.FILE,
|
|
1002
|
+
fileInfo.post_hash,
|
|
1003
|
+
[aleph.getAddress()],
|
|
1004
|
+
FileMetaEncryptedSchema,
|
|
1005
|
+
async (encryptedMeta) => {
|
|
1006
|
+
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
1007
|
+
const encryptedContent = await EncryptionService.encryptFile(
|
|
1008
|
+
newContent,
|
|
1009
|
+
Buffer.from(decryptedMeta.key, "hex"),
|
|
1010
|
+
Buffer.from(decryptedMeta.iv, "hex")
|
|
1011
|
+
);
|
|
1012
|
+
const uploadResult = await aleph.uploadFile(encryptedContent);
|
|
1013
|
+
decryptedMeta.store_hash = uploadResult.item_hash;
|
|
1014
|
+
fileInfo.store_hash = decryptedMeta.store_hash;
|
|
1015
|
+
return await this.encryptFileMeta(decryptedMeta);
|
|
1016
|
+
}
|
|
1017
|
+
);
|
|
1018
|
+
fileInfo.post_hash = postResult.item_hash;
|
|
1019
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1020
|
+
files: aggregate.files.map(
|
|
1021
|
+
(entry) => fileInfo.path === EncryptionService.decryptEcies(entry.path, privateKey) ? { ...entry, post_hash: fileInfo.post_hash } : entry
|
|
1022
|
+
)
|
|
1023
|
+
}));
|
|
1024
|
+
return fileInfo;
|
|
1025
|
+
} catch (error) {
|
|
1026
|
+
throw new FileError(`Failed to edit file's content: ${error.message}`);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
988
1029
|
/**
|
|
989
1030
|
* Download and decrypt a file
|
|
990
1031
|
* @param fileInfo - File information
|