-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete-file.ts
More file actions
87 lines (74 loc) · 2.76 KB
/
delete-file.ts
File metadata and controls
87 lines (74 loc) · 2.76 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
import { stringToBytes, Args } from '@massalabs/as-types';
import { sha256, Storage, balance } from '@massalabs/massa-as-sdk';
import { FileDelete } from '../../../contracts/serializable/FileDelete';
import { deleteFiles, purge } from '../../../contracts/deweb-interface';
import {
fileMetadataKey,
fileLocationKey,
} from '../../../contracts/internals/storageKeys/metadataKeys';
import { fileChunkCountKey } from '../../../contracts/internals/storageKeys/chunksKeys';
import {
FILE_TAG,
CHUNK_TAG,
} from '../../../contracts/internals/storageKeys/tags';
import { _getFileLocations } from '../../../contracts/internals/location';
export function _purge(): void {
purge([]);
}
export function _assertPurged(): void {
assert(Storage.getKeys([]).length == 0, 'Storage should be empty');
}
export function _deleteFiles(files: string[]): void {
const filesToDelete: FileDelete[] = [];
for (let i = 0; i < files.length; i++) {
filesToDelete.push(new FileDelete(sha256(stringToBytes(files[i]))));
}
deleteFiles(
new Args()
.addSerializableObjectArray<FileDelete>(filesToDelete)
.serialize(),
);
}
export function hasNoFiles(): void {
const fileList = _getFileLocations();
assert(fileList.length === 0, 'FileList should be empty');
}
export function _assertFilesArePresent(files: string[]): void {
const fileList = _getFileLocations();
for (let i = 0; i < files.length; i++) {
assert(
fileList.includes(files[i]),
`File ${files[i]} should be in the file list`,
);
}
}
export function _assertFilesAreNotPresent(files: string[]): void {
for (let i = 0; i < files.length; i++) {
const locationHash = sha256(stringToBytes(files[i]));
_assertHasNoChunk(locationHash);
_assertHasNoMetadata(locationHash);
_assertHasNoChunkCount(locationHash);
_assertHasNoLocation(locationHash);
}
}
export function _assertHasNoMetadata(locationHash: StaticArray<u8>): void {
const keys = Storage.getKeys(fileMetadataKey(locationHash));
assert(keys.length === 0, 'Metadata should not be stored');
}
export function _assertHasNoChunkCount(locationHash: StaticArray<u8>): void {
const chunkCountKey = fileChunkCountKey(locationHash);
assert(!Storage.has(chunkCountKey), 'Chunk count should not be stored');
}
export function _assertHasNoLocation(locationHash: StaticArray<u8>): void {
const locationKey = fileLocationKey(locationHash);
assert(!Storage.has(locationKey), 'Location should not be stored');
}
export function _assertHasNoChunk(locationHash: StaticArray<u8>): void {
const chunkKeys = Storage.getKeys(
FILE_TAG.concat(locationHash).concat(CHUNK_TAG),
);
assert(chunkKeys.length === 0, 'Chunks should not be stored');
}
export function _assertHasNoCoins(): void {
assert(balance() === 0, 'Balance should be zero');
}