bedrock-ts-sdk 0.0.4 → 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 +17 -4
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +73 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +73 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -871,8 +871,16 @@ var FileService = class {
|
|
|
871
871
|
const aleph = this.core.getAlephService();
|
|
872
872
|
const publicKey = this.core.getPublicKey();
|
|
873
873
|
const uploadedFiles = [];
|
|
874
|
+
const existingFiles = await this.listFiles();
|
|
875
|
+
const filesToUpload = files.filter((file) => {
|
|
876
|
+
const filterIn = !existingFiles.some(
|
|
877
|
+
(existingFile) => !existingFile.deleted_at && existingFile.path === (directoryPath ? `${directoryPath}${file.path}` : file.path)
|
|
878
|
+
);
|
|
879
|
+
console.log(`Keeping ${file} in ? ${filterIn ? "yes" : "no"}`);
|
|
880
|
+
return filterIn;
|
|
881
|
+
});
|
|
874
882
|
try {
|
|
875
|
-
for (const file of
|
|
883
|
+
for (const file of filesToUpload) {
|
|
876
884
|
const key = EncryptionService.generateKey();
|
|
877
885
|
const iv = EncryptionService.generateIv();
|
|
878
886
|
let fileBuffer;
|
|
@@ -913,6 +921,39 @@ var FileService = class {
|
|
|
913
921
|
throw new FileError(`Failed to upload files: ${error.message}`);
|
|
914
922
|
}
|
|
915
923
|
}
|
|
924
|
+
async editFileContent(fileInfo, newContent) {
|
|
925
|
+
const aleph = this.core.getAlephService();
|
|
926
|
+
const privateKey = this.core.getSubAccountPrivateKey();
|
|
927
|
+
try {
|
|
928
|
+
const postResult = await aleph.updatePost(
|
|
929
|
+
POST_TYPES.FILE,
|
|
930
|
+
fileInfo.post_hash,
|
|
931
|
+
[aleph.getAddress()],
|
|
932
|
+
FileMetaEncryptedSchema,
|
|
933
|
+
async (encryptedMeta) => {
|
|
934
|
+
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
935
|
+
const encryptedContent = await EncryptionService.encryptFile(
|
|
936
|
+
newContent,
|
|
937
|
+
Buffer.from(decryptedMeta.key, "hex"),
|
|
938
|
+
Buffer.from(decryptedMeta.iv, "hex")
|
|
939
|
+
);
|
|
940
|
+
const uploadResult = await aleph.uploadFile(encryptedContent);
|
|
941
|
+
decryptedMeta.store_hash = uploadResult.item_hash;
|
|
942
|
+
fileInfo.store_hash = decryptedMeta.store_hash;
|
|
943
|
+
return await this.encryptFileMeta(decryptedMeta);
|
|
944
|
+
}
|
|
945
|
+
);
|
|
946
|
+
fileInfo.post_hash = postResult.item_hash;
|
|
947
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
948
|
+
files: aggregate.files.map(
|
|
949
|
+
(entry) => fileInfo.path === EncryptionService.decryptEcies(entry.path, privateKey) ? { ...entry, post_hash: fileInfo.post_hash } : entry
|
|
950
|
+
)
|
|
951
|
+
}));
|
|
952
|
+
return fileInfo;
|
|
953
|
+
} catch (error) {
|
|
954
|
+
throw new FileError(`Failed to edit file's content: ${error.message}`);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
916
957
|
/**
|
|
917
958
|
* Download and decrypt a file
|
|
918
959
|
* @param fileInfo - File information
|
|
@@ -1015,10 +1056,10 @@ var FileService = class {
|
|
|
1015
1056
|
try {
|
|
1016
1057
|
for (const path of filePaths) {
|
|
1017
1058
|
const file = await this.getFile(path);
|
|
1018
|
-
await aleph.updatePost(
|
|
1059
|
+
const updatedPost = await aleph.updatePost(
|
|
1019
1060
|
POST_TYPES.FILE,
|
|
1020
1061
|
file.post_hash,
|
|
1021
|
-
[
|
|
1062
|
+
[aleph.getAddress()],
|
|
1022
1063
|
FileMetaEncryptedSchema,
|
|
1023
1064
|
async (encryptedMeta) => {
|
|
1024
1065
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1026,6 +1067,11 @@ var FileService = class {
|
|
|
1026
1067
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1027
1068
|
}
|
|
1028
1069
|
);
|
|
1070
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1071
|
+
files: aggregate.files.map(
|
|
1072
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, post_hash: updatedPost.item_hash } : entry
|
|
1073
|
+
)
|
|
1074
|
+
}));
|
|
1029
1075
|
}
|
|
1030
1076
|
} catch (error) {
|
|
1031
1077
|
throw new FileError(`Failed to soft delete files: ${error.message}`);
|
|
@@ -1040,10 +1086,10 @@ var FileService = class {
|
|
|
1040
1086
|
try {
|
|
1041
1087
|
for (const path of filePaths) {
|
|
1042
1088
|
const file = await this.getFile(path);
|
|
1043
|
-
await aleph.updatePost(
|
|
1089
|
+
const updatedPost = await aleph.updatePost(
|
|
1044
1090
|
POST_TYPES.FILE,
|
|
1045
1091
|
file.post_hash,
|
|
1046
|
-
[
|
|
1092
|
+
[aleph.getAddress()],
|
|
1047
1093
|
FileMetaEncryptedSchema,
|
|
1048
1094
|
async (encryptedMeta) => {
|
|
1049
1095
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1051,6 +1097,11 @@ var FileService = class {
|
|
|
1051
1097
|
return await this.encryptFileMeta(decryptedMeta);
|
|
1052
1098
|
}
|
|
1053
1099
|
);
|
|
1100
|
+
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1101
|
+
files: aggregate.files.map(
|
|
1102
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, post_hash: updatedPost.item_hash } : entry
|
|
1103
|
+
)
|
|
1104
|
+
}));
|
|
1054
1105
|
}
|
|
1055
1106
|
} catch (error) {
|
|
1056
1107
|
throw new FileError(`Failed to restore files: ${error.message}`);
|
|
@@ -1083,10 +1134,10 @@ var FileService = class {
|
|
|
1083
1134
|
try {
|
|
1084
1135
|
for (const { oldPath, newPath } of moves) {
|
|
1085
1136
|
const file = await this.getFile(oldPath);
|
|
1086
|
-
await aleph.updatePost(
|
|
1137
|
+
const updatedPost = await aleph.updatePost(
|
|
1087
1138
|
POST_TYPES.FILE,
|
|
1088
1139
|
file.post_hash,
|
|
1089
|
-
[
|
|
1140
|
+
[aleph.getAddress()],
|
|
1090
1141
|
FileMetaEncryptedSchema,
|
|
1091
1142
|
async (encryptedMeta) => {
|
|
1092
1143
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1098,7 +1149,7 @@ var FileService = class {
|
|
|
1098
1149
|
const newEncryptedPath = EncryptionService.encryptEcies(newPath, publicKey);
|
|
1099
1150
|
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1100
1151
|
files: aggregate.files.map(
|
|
1101
|
-
(entry) => entry.post_hash === file.post_hash ? { ...entry, path: newEncryptedPath } : entry
|
|
1152
|
+
(entry) => entry.post_hash === file.post_hash ? { ...entry, path: newEncryptedPath, post_hash: updatedPost.item_hash } : entry
|
|
1102
1153
|
)
|
|
1103
1154
|
}));
|
|
1104
1155
|
}
|
|
@@ -1138,10 +1189,10 @@ var FileService = class {
|
|
|
1138
1189
|
const file = await this.getFile(filePath);
|
|
1139
1190
|
const encryptedKey = EncryptionService.encryptEcies(file.key, contactPublicKey);
|
|
1140
1191
|
const encryptedIv = EncryptionService.encryptEcies(file.iv, contactPublicKey);
|
|
1141
|
-
await aleph.updatePost(
|
|
1192
|
+
const updatedPost = await aleph.updatePost(
|
|
1142
1193
|
POST_TYPES.FILE,
|
|
1143
1194
|
file.post_hash,
|
|
1144
|
-
[
|
|
1195
|
+
[aleph.getAddress()],
|
|
1145
1196
|
FileMetaEncryptedSchema,
|
|
1146
1197
|
async (encryptedMeta) => {
|
|
1147
1198
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1154,7 +1205,11 @@ var FileService = class {
|
|
|
1154
1205
|
);
|
|
1155
1206
|
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1156
1207
|
files: aggregate.files.map(
|
|
1157
|
-
(entry) => entry.post_hash === file.post_hash ? {
|
|
1208
|
+
(entry) => entry.post_hash === file.post_hash ? {
|
|
1209
|
+
...entry,
|
|
1210
|
+
post_hash: updatedPost.item_hash,
|
|
1211
|
+
shared_with: [.../* @__PURE__ */ new Set([...entry.shared_with, contactPublicKey])]
|
|
1212
|
+
} : entry
|
|
1158
1213
|
)
|
|
1159
1214
|
}));
|
|
1160
1215
|
} catch (error) {
|
|
@@ -1170,10 +1225,10 @@ var FileService = class {
|
|
|
1170
1225
|
const aleph = this.core.getAlephService();
|
|
1171
1226
|
try {
|
|
1172
1227
|
const file = await this.getFile(filePath);
|
|
1173
|
-
await aleph.updatePost(
|
|
1228
|
+
const updatedPost = await aleph.updatePost(
|
|
1174
1229
|
POST_TYPES.FILE,
|
|
1175
1230
|
file.post_hash,
|
|
1176
|
-
[
|
|
1231
|
+
[aleph.getAddress()],
|
|
1177
1232
|
FileMetaEncryptedSchema,
|
|
1178
1233
|
async (encryptedMeta) => {
|
|
1179
1234
|
const decryptedMeta = await this.decryptFileMeta(encryptedMeta);
|
|
@@ -1183,7 +1238,11 @@ var FileService = class {
|
|
|
1183
1238
|
);
|
|
1184
1239
|
await aleph.updateAggregate(AGGREGATE_KEYS.FILE_ENTRIES, FileEntriesAggregateSchema, async (aggregate) => ({
|
|
1185
1240
|
files: aggregate.files.map(
|
|
1186
|
-
(entry) => entry.post_hash === file.post_hash ? {
|
|
1241
|
+
(entry) => entry.post_hash === file.post_hash ? {
|
|
1242
|
+
...entry,
|
|
1243
|
+
post_hash: updatedPost.item_hash,
|
|
1244
|
+
shared_with: entry.shared_with.filter((pk) => pk !== contactPublicKey)
|
|
1245
|
+
} : entry
|
|
1187
1246
|
)
|
|
1188
1247
|
}));
|
|
1189
1248
|
} catch (error) {
|