@holochain-open-dev/file-storage 0.500.1 → 0.601.0

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.
@@ -23,20 +23,23 @@ export declare class FileStorageClient {
23
23
  /**
24
24
  * Downloads the whole file with the given hash
25
25
  * @param fileHash
26
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
26
27
  */
27
- downloadFile(fileHash: EntryHash): Promise<File>;
28
+ downloadFile(fileHash: EntryHash, local?: boolean): Promise<File>;
28
29
  /**
29
30
  * Gets only the metadata of the file with the given hash
30
31
  * This is specially useful if you want to fetch the chunks one by one
31
32
  * @param fileHash the hash of the file
33
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
32
34
  */
33
- getFileMetadata(fileHash: EntryHash): Promise<FileMetadata>;
35
+ getFileMetadata(fileHash: EntryHash, local?: boolean): Promise<FileMetadata>;
34
36
  /**
35
37
  * Fetch the chunk identified with the given hash
36
38
  * This is useful if used with the chunk hashes received with `getFileMetadata`
37
39
  * @param fileChunkHash
40
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
38
41
  */
39
- fetchChunk(fileChunkHash: EntryHash): Promise<Blob>;
42
+ fetchChunk(fileChunkHash: EntryHash, local?: boolean): Promise<Blob>;
40
43
  /** Private helpers */
41
44
  private _splitFile;
42
45
  private _createChunk;
@@ -46,10 +46,11 @@ export class FileStorageClient {
46
46
  /**
47
47
  * Downloads the whole file with the given hash
48
48
  * @param fileHash
49
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
49
50
  */
50
- async downloadFile(fileHash) {
51
- const metadata = await this.getFileMetadata(fileHash);
52
- const fetchChunksPromises = metadata.chunks_hashes.map((hash) => this.fetchChunk(hash));
51
+ async downloadFile(fileHash, local = true) {
52
+ const metadata = await this.getFileMetadata(fileHash, local);
53
+ const fetchChunksPromises = metadata.chunks_hashes.map((hash) => this.fetchChunk(hash, local));
53
54
  const chunks = await Promise.all(fetchChunksPromises);
54
55
  const file = new File(chunks, metadata.name, {
55
56
  lastModified: Math.floor(metadata.last_modifed / 1000),
@@ -61,17 +62,19 @@ export class FileStorageClient {
61
62
  * Gets only the metadata of the file with the given hash
62
63
  * This is specially useful if you want to fetch the chunks one by one
63
64
  * @param fileHash the hash of the file
65
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
64
66
  */
65
- async getFileMetadata(fileHash) {
66
- return await this._callZome("get_file_metadata", fileHash);
67
+ async getFileMetadata(fileHash, local = true) {
68
+ return await this._callZome("get_file_metadata", { input: fileHash, local });
67
69
  }
68
70
  /**
69
71
  * Fetch the chunk identified with the given hash
70
72
  * This is useful if used with the chunk hashes received with `getFileMetadata`
71
73
  * @param fileChunkHash
74
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
72
75
  */
73
- async fetchChunk(fileChunkHash) {
74
- const bytes = await this._callZome("get_file_chunk", fileChunkHash);
76
+ async fetchChunk(fileChunkHash, local = true) {
77
+ const bytes = await this._callZome("get_file_chunk", { input: fileChunkHash, local });
75
78
  return new Blob([new Uint8Array(bytes)]);
76
79
  }
77
80
  /** Private helpers */
@@ -1 +1 @@
1
- {"version":3,"file":"file-storage-client.js","sourceRoot":"","sources":["../src/file-storage-client.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,iBAAiB;IAC5B;;;;;OAKG;IACH,YACS,MAAiB,EACjB,QAAgB,EAChB,WAAmB,cAAc,EACjC,SAA2B,SAAS;QAHpC,WAAM,GAAN,MAAM,CAAW;QACjB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,aAAQ,GAAR,QAAQ,CAAyB;QACjC,WAAM,GAAN,MAAM,CAA8B;IAC1C,CAAC;IAEJ;;;;;OAKG;IACH,KAAK,CAAC,UAAU,CACd,IAAU,EACV,aAEiE,SAAS,EAC1E,YAAoB,GAAG,GAAG,IAAI;QAE9B,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC/C,MAAM,cAAc,GA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O,IAAI,CAAC,SAAS,CAAC,mBAAmB,EAAE,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IACpE,CAAC;IAEO,SAAS,CAAC,OAAe,EAAE,OAAY;QAC7C,MAAM,GAAG,GACP,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;YACZ,OAAO,EAAE,IAAI,CAAC,MAAM;YACpB,SAAS,EAAE,IAAI,CAAC,QAAQ;YACxB,OAAO;YACP,OAAO;SACR,CAAC,CAAC;YACH;gBACE,SAAS,EAAE,IAAI,CAAC,QAAQ;gBACxB,SAAS,EAAE,IAAI,CAAC,QAAQ;gBACxB,OAAO;gBACP,OAAO;aACR,CAAA;QACH,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;CACF","sourcesContent":["import type { RoleNameCallZomeRequest, CallZomeRequest, AppClient, CellId } from \"@holochain/client\";\nimport { EntryHash } from \"@holochain/client\";\nimport { FileMetadata } from \"./types\";\n\nexport class FileStorageClient {\n /**\n * @param client connection to the holochain backend\n * @param roleName\n * @param zomeName the zome name of the file_storage zome in the given cell\n * @param cellId optional cellId parameter for use when client is associated with a clone-cell\n */\n constructor(\n public client: AppClient,\n public roleName: string,\n public zomeName: string = \"file_storage\",\n public cellId: CellId|undefined = undefined,\n ) {}\n\n /**\n * Upload a file to the file_storage zome, splitting it into chunks\n *\n * @param file file to split and upload\n * @param chunkSize chunk size to split the file, default 256 KB\n */\n async uploadFile(\n file: File,\n onProgress:\n | undefined\n | ((percentatgeProgress: number, bytesSent: number) => void) = undefined,\n chunkSize: number = 256 * 1024\n ): Promise<EntryHash> {\n const blobs = this._splitFile(file, chunkSize);\n const numberOfChunks = blobs.length;\n const bytesPerChunk = blobs[0].size;\n\n let uploadedChunks = 0;\n\n const onChunkUploaded = () => {\n if (onProgress) {\n uploadedChunks++;\n onProgress(\n ((uploadedChunks + 1) * 1.0) / numberOfChunks,\n bytesPerChunk * (uploadedChunks + 1)\n );\n }\n };\n\n const chunksHashes: Array<EntryHash> = await Promise.all(\n blobs.map(async (blob) => {\n const chunkHash = await this._createChunk(blob);\n\n onChunkUploaded();\n return chunkHash;\n })\n );\n\n const fileToCreate = {\n name: file.name,\n size: file.size,\n file_type: file.type,\n last_modified: file.lastModified * 1000,\n chunks_hashes: chunksHashes,\n };\n const hash = await this._callZome(\"create_file_metadata\", fileToCreate);\n\n return hash;\n }\n\n /**\n * Downloads the whole file with the given hash\n * @param fileHash\n */\n async downloadFile(fileHash: EntryHash): Promise<File> {\n const metadata = await this.getFileMetadata(fileHash);\n\n const fetchChunksPromises = metadata.chunks_hashes.map((hash) =>\n this.fetchChunk(hash)\n );\n\n const chunks = await Promise.all(fetchChunksPromises);\n\n const file = new File(chunks, metadata.name, {\n lastModified: Math.floor(metadata.last_modifed / 1000),\n type: metadata.file_type,\n });\n\n return file;\n }\n\n /**\n * Gets only the metadata of the file with the given hash\n * This is specially useful if you want to fetch the chunks one by one\n * @param fileHash the hash of the file\n */\n async getFileMetadata(fileHash: EntryHash): Promise<FileMetadata> {\n return await this._callZome(\"get_file_metadata\", fileHash);\n }\n\n /**\n * Fetch the chunk identified with the given hash\n * This is useful if used with the chunk hashes received with `getFileMetadata`\n * @param fileChunkHash\n */\n async fetchChunk(fileChunkHash: EntryHash): Promise<Blob> {\n const bytes = await this._callZome(\"get_file_chunk\", fileChunkHash);\n\n return new Blob([new Uint8Array(bytes)]);\n }\n\n /** Private helpers */\n\n private _splitFile(file: File, chunkSize: number): Blob[] {\n let offset = 0;\n const chunks: Blob[] = [];\n\n while (file.size > offset) {\n const chunk = file.slice(offset, offset + chunkSize);\n offset += chunkSize;\n chunks.push(chunk);\n }\n\n return chunks;\n }\n\n private async _createChunk(chunk: Blob): Promise<EntryHash> {\n const bytes = await chunk.arrayBuffer();\n\n return this._callZome(\"create_file_chunk\", new Uint8Array(bytes));\n }\n\n private _callZome(fn_name: string, payload: any) {\n const req: RoleNameCallZomeRequest | CallZomeRequest =\n this.cellId ? {\n cell_id: this.cellId,\n zome_name: this.zomeName,\n fn_name,\n payload,\n } :\n {\n role_name: this.roleName,\n zome_name: this.zomeName,\n fn_name,\n payload,\n }\n return this.client.callZome(req);\n }\n}\n"]}
1
+ {"version":3,"file":"file-storage-client.js","sourceRoot":"","sources":["../src/file-storage-client.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,iBAAiB;IAC5B;;;;;OAKG;IACH,YACS,MAAiB,EACjB,QAAgB,EAChB,WAAmB,cAAc,EACjC,SAA2B,SAAS;QAHpC,WAAM,GAAN,MAAM,CAAW;QACjB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,aAAQ,GAAR,QAAQ,CAAyB;QACjC,WAAM,GAAN,MAAM,CAA8B;IAC1C,CAAC;IAEJ;;;;;OAKG;IACH,KAAK,CAAC,UAAU,CACd,IAAU,EACV,aAEiE,SAAS,EAC1E,YAAoB,GAAG,GAAG,IAAI;QAE9B,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC/C,MAAM,cAAc,GAAG,KAAK,CAAC,MAAM,CAAC;QACpC,MAAM,aAAa,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAEpC,IAAI,cAAc,GAAG,CAAC,CAAC;QAEvB,MAAM,eAAe,GAAG,GAAG,EAAE;YAC3B,IAAI,UAAU,EAAE,CAAC;gBACf,cAAc,EAAE,CAAC;gBACjB,UAAU,CACR,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,cAAc,EAC7C,aAAa,GAAG,CAAC,cAAc,GAAG,CAAC,CAAC,CACrC,CAAC;YACJ,CAAC;QACH,CAAC,CAAC;QAEF,MAAM,YAAY,GAAqB,MAAM,OAAO,CAAC,GAAG,CACtD,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YACvB,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,YAAY,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AAO;gBACP,OAAO;aACR,CAAA;QACH,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;CACF","sourcesContent":["import type { RoleNameCallZomeRequest, CallZomeRequest, AppClient, CellId } from \"@holochain/client\";\nimport { EntryHash } from \"@holochain/client\";\nimport { FileMetadata } from \"./types\";\n\nexport class FileStorageClient {\n /**\n * @param client connection to the holochain backend\n * @param roleName\n * @param zomeName the zome name of the file_storage zome in the given cell\n * @param cellId optional cellId parameter for use when client is associated with a clone-cell\n */\n constructor(\n public client: AppClient,\n public roleName: string,\n public zomeName: string = \"file_storage\",\n public cellId: CellId|undefined = undefined,\n ) {}\n\n /**\n * Upload a file to the file_storage zome, splitting it into chunks\n *\n * @param file file to split and upload\n * @param chunkSize chunk size to split the file, default 256 KB\n */\n async uploadFile(\n file: File,\n onProgress:\n | undefined\n | ((percentatgeProgress: number, bytesSent: number) => void) = undefined,\n chunkSize: number = 256 * 1024\n ): Promise<EntryHash> {\n const blobs = this._splitFile(file, chunkSize);\n const numberOfChunks = blobs.length;\n const bytesPerChunk = blobs[0].size;\n\n let uploadedChunks = 0;\n\n const onChunkUploaded = () => {\n if (onProgress) {\n uploadedChunks++;\n onProgress(\n ((uploadedChunks + 1) * 1.0) / numberOfChunks,\n bytesPerChunk * (uploadedChunks + 1)\n );\n }\n };\n\n const chunksHashes: Array<EntryHash> = await Promise.all(\n blobs.map(async (blob) => {\n const chunkHash = await this._createChunk(blob);\n\n onChunkUploaded();\n return chunkHash;\n })\n );\n\n const fileToCreate = {\n name: file.name,\n size: file.size,\n file_type: file.type,\n last_modified: file.lastModified * 1000,\n chunks_hashes: chunksHashes,\n };\n const hash = await this._callZome(\"create_file_metadata\", fileToCreate);\n\n return hash;\n }\n\n /**\n * Downloads the whole file with the given hash\n * @param fileHash\n * @param local whether to only check locally (default: true). Set to false to fetch from network.\n */\n async downloadFile(fileHash: EntryHash, local: boolean = true): Promise<File> {\n const metadata = await this.getFileMetadata(fileHash, local);\n\n const fetchChunksPromises = metadata.chunks_hashes.map((hash) =>\n this.fetchChunk(hash, local)\n );\n\n const chunks = await Promise.all(fetchChunksPromises);\n\n const file = new File(chunks, metadata.name, {\n lastModified: Math.floor(metadata.last_modifed / 1000),\n type: metadata.file_type,\n });\n\n return file;\n }\n\n /**\n * Gets only the metadata of the file with the given hash\n * This is specially useful if you want to fetch the chunks one by one\n * @param fileHash the hash of the file\n * @param local whether to only check locally (default: true). Set to false to fetch from network.\n */\n async getFileMetadata(fileHash: EntryHash, local: boolean = true): Promise<FileMetadata> {\n return await this._callZome(\"get_file_metadata\", { input: fileHash, local });\n }\n\n /**\n * Fetch the chunk identified with the given hash\n * This is useful if used with the chunk hashes received with `getFileMetadata`\n * @param fileChunkHash\n * @param local whether to only check locally (default: true). Set to false to fetch from network.\n */\n async fetchChunk(fileChunkHash: EntryHash, local: boolean = true): Promise<Blob> {\n const bytes = await this._callZome(\"get_file_chunk\", { input: fileChunkHash, local });\n\n return new Blob([new Uint8Array(bytes)]);\n }\n\n /** Private helpers */\n\n private _splitFile(file: File, chunkSize: number): Blob[] {\n let offset = 0;\n const chunks: Blob[] = [];\n\n while (file.size > offset) {\n const chunk = file.slice(offset, offset + chunkSize);\n offset += chunkSize;\n chunks.push(chunk);\n }\n\n return chunks;\n }\n\n private async _createChunk(chunk: Blob): Promise<EntryHash> {\n const bytes = await chunk.arrayBuffer();\n\n return this._callZome(\"create_file_chunk\", new Uint8Array(bytes));\n }\n\n private _callZome(fn_name: string, payload: any) {\n const req: RoleNameCallZomeRequest | CallZomeRequest =\n this.cellId ? {\n cell_id: this.cellId,\n zome_name: this.zomeName,\n fn_name,\n payload,\n } :\n {\n role_name: this.roleName,\n zome_name: this.zomeName,\n fn_name,\n payload,\n }\n return this.client.callZome(req);\n }\n}\n"]}
package/dist/mocks.d.ts CHANGED
@@ -1,11 +1,17 @@
1
- import { EntryHashMap, ZomeMock } from "@holochain-open-dev/utils";
2
- import { HoloHash } from "@holochain/client";
1
+ import { ZomeMock } from "@holochain-open-dev/utils";
2
+ import { HoloHash, EntryHashMap } from "@holochain/client";
3
3
  import { FileMetadata } from "./types";
4
4
  export declare class FileStorageZomeMock extends ZomeMock {
5
- metadata: EntryHashMap<unknown>;
6
- chunks: EntryHashMap<unknown>;
5
+ metadata: EntryHashMap<FileMetadata>;
6
+ chunks: EntryHashMap<Uint8Array<ArrayBufferLike>>;
7
7
  create_file_metadata(fileMetadata: FileMetadata): HoloHash;
8
- get_file_metadata(fileHash: HoloHash): unknown;
8
+ get_file_metadata(input: {
9
+ input: HoloHash;
10
+ local?: boolean;
11
+ }): FileMetadata | undefined;
9
12
  create_file_chunk(fileChunk: Uint8Array): HoloHash;
10
- get_file_chunk(fileChunkHash: HoloHash): unknown;
13
+ get_file_chunk(input: {
14
+ input: HoloHash;
15
+ local?: boolean;
16
+ }): Uint8Array<ArrayBufferLike> | undefined;
11
17
  }
package/dist/mocks.js CHANGED
@@ -1,4 +1,5 @@
1
- import { EntryHashMap, ZomeMock, hash, HashType, } from "@holochain-open-dev/utils";
1
+ import { ZomeMock, hash, HashType, } from "@holochain-open-dev/utils";
2
+ import { EntryHashMap } from "@holochain/client";
2
3
  export class FileStorageZomeMock extends ZomeMock {
3
4
  constructor() {
4
5
  super(...arguments);
@@ -10,16 +11,16 @@ export class FileStorageZomeMock extends ZomeMock {
10
11
  this.metadata.set(newId, fileMetadata);
11
12
  return newId;
12
13
  }
13
- get_file_metadata(fileHash) {
14
- return this.metadata.get(fileHash);
14
+ get_file_metadata(input) {
15
+ return this.metadata.get(input.input);
15
16
  }
16
17
  create_file_chunk(fileChunk) {
17
18
  const newId = hash(fileChunk, HashType.ENTRY);
18
19
  this.chunks.set(newId, fileChunk);
19
20
  return newId;
20
21
  }
21
- get_file_chunk(fileChunkHash) {
22
- return this.chunks.get(fileChunkHash);
22
+ get_file_chunk(input) {
23
+ return this.chunks.get(input.input);
23
24
  }
24
25
  }
25
26
  //# sourceMappingURL=mocks.js.map
package/dist/mocks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mocks.js","sourceRoot":"","sources":["../src/mocks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,IAAI,EACJ,QAAQ,GACT,MAAM,2BAA2B,CAAC;AAInC,MAAM,OAAO,mBAAoB,SAAQ,QAAQ;IAAjD;;QACE,aAAQ,GAAG,IAAI,YAAY,EAAE,CAAC;QAC9B,WAAM,GAAG,IAAI,YAAY,EAAE,CAAC;IAyB9B,CAAC;IAvBC,oBAAoB,CAAC,YAA0B;QAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEjD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAEvC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,iBAAiB,CAAC,QAAkB;QAClC,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACrC,CAAC;IAED,iBAAiB,CAAC,SAAqB;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;QAE9C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAElC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,cAAc,CAAC,aAAuB;QACpC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACxC,CAAC;CACF","sourcesContent":["import {\n EntryHashMap,\n ZomeMock,\n hash,\n HashType,\n} from \"@holochain-open-dev/utils\";\nimport { HoloHash } from \"@holochain/client\";\nimport { FileMetadata } from \"./types\";\n\nexport class FileStorageZomeMock extends ZomeMock {\n metadata = new EntryHashMap();\n chunks = new EntryHashMap();\n\n create_file_metadata(fileMetadata: FileMetadata) {\n const newId = hash(fileMetadata, HashType.ENTRY);\n\n this.metadata.set(newId, fileMetadata);\n\n return newId;\n }\n\n get_file_metadata(fileHash: HoloHash) {\n return this.metadata.get(fileHash);\n }\n\n create_file_chunk(fileChunk: Uint8Array) {\n const newId = hash(fileChunk, HashType.ENTRY);\n\n this.chunks.set(newId, fileChunk);\n\n return newId;\n }\n\n get_file_chunk(fileChunkHash: HoloHash) {\n return this.chunks.get(fileChunkHash);\n }\n}\n"]}
1
+ {"version":3,"file":"mocks.js","sourceRoot":"","sources":["../src/mocks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,IAAI,EACJ,QAAQ,GACT,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAY,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAG3D,MAAM,OAAO,mBAAoB,SAAQ,QAAQ;IAAjD;;QACE,aAAQ,GAAG,IAAI,YAAY,EAAgB,CAAC;QAC5C,WAAM,GAAG,IAAI,YAAY,EAAc,CAAC;IAyB1C,CAAC;IAvBC,oBAAoB,CAAC,YAA0B;QAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEjD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAEvC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,iBAAiB,CAAC,KAA2C;QAC3D,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;IAED,iBAAiB,CAAC,SAAqB;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;QAE9C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAElC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,cAAc,CAAC,KAA2C;QACxD,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACtC,CAAC;CACF","sourcesContent":["import {\n ZomeMock,\n hash,\n HashType,\n} from \"@holochain-open-dev/utils\";\nimport { HoloHash, EntryHashMap } from \"@holochain/client\";\nimport { FileMetadata } from \"./types\";\n\nexport class FileStorageZomeMock extends ZomeMock {\n metadata = new EntryHashMap<FileMetadata>();\n chunks = new EntryHashMap<Uint8Array>();\n\n create_file_metadata(fileMetadata: FileMetadata) {\n const newId = hash(fileMetadata, HashType.ENTRY);\n\n this.metadata.set(newId, fileMetadata);\n\n return newId;\n }\n\n get_file_metadata(input: { input: HoloHash; local?: boolean }) {\n return this.metadata.get(input.input);\n }\n\n create_file_chunk(fileChunk: Uint8Array) {\n const newId = hash(fileChunk, HashType.ENTRY);\n\n this.chunks.set(newId, fileChunk);\n\n return newId;\n }\n\n get_file_chunk(input: { input: HoloHash; local?: boolean }) {\n return this.chunks.get(input.input);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holochain-open-dev/file-storage",
3
- "version": "0.500.1",
3
+ "version": "0.601.0",
4
4
  "description": "File storage utilities to store and retrieve files in a holochain DHT",
5
5
  "author": "guillem.cordoba@gmail.com",
6
6
  "license": "MIT",
@@ -20,9 +20,9 @@
20
20
  "prepare": "tsc"
21
21
  },
22
22
  "dependencies": {
23
- "@holochain-open-dev/elements": "^0.500.1",
24
- "@holochain-open-dev/utils": "^0.500.2",
25
- "@holochain/client": "^0.19.1",
23
+ "@holochain-open-dev/elements": "^0.601.0",
24
+ "@holochain-open-dev/utils": "^0.601.0",
25
+ "@holochain/client": "^0.20.4-rc.0",
26
26
  "@lit-labs/task": "^2.0.0",
27
27
  "@lit/context": "^1.0.0",
28
28
  "@lit/localize": "^0.12.0",
@@ -69,12 +69,13 @@ export class FileStorageClient {
69
69
  /**
70
70
  * Downloads the whole file with the given hash
71
71
  * @param fileHash
72
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
72
73
  */
73
- async downloadFile(fileHash: EntryHash): Promise<File> {
74
- const metadata = await this.getFileMetadata(fileHash);
74
+ async downloadFile(fileHash: EntryHash, local: boolean = true): Promise<File> {
75
+ const metadata = await this.getFileMetadata(fileHash, local);
75
76
 
76
77
  const fetchChunksPromises = metadata.chunks_hashes.map((hash) =>
77
- this.fetchChunk(hash)
78
+ this.fetchChunk(hash, local)
78
79
  );
79
80
 
80
81
  const chunks = await Promise.all(fetchChunksPromises);
@@ -91,18 +92,20 @@ export class FileStorageClient {
91
92
  * Gets only the metadata of the file with the given hash
92
93
  * This is specially useful if you want to fetch the chunks one by one
93
94
  * @param fileHash the hash of the file
95
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
94
96
  */
95
- async getFileMetadata(fileHash: EntryHash): Promise<FileMetadata> {
96
- return await this._callZome("get_file_metadata", fileHash);
97
+ async getFileMetadata(fileHash: EntryHash, local: boolean = true): Promise<FileMetadata> {
98
+ return await this._callZome("get_file_metadata", { input: fileHash, local });
97
99
  }
98
100
 
99
101
  /**
100
102
  * Fetch the chunk identified with the given hash
101
103
  * This is useful if used with the chunk hashes received with `getFileMetadata`
102
104
  * @param fileChunkHash
105
+ * @param local whether to only check locally (default: true). Set to false to fetch from network.
103
106
  */
104
- async fetchChunk(fileChunkHash: EntryHash): Promise<Blob> {
105
- const bytes = await this._callZome("get_file_chunk", fileChunkHash);
107
+ async fetchChunk(fileChunkHash: EntryHash, local: boolean = true): Promise<Blob> {
108
+ const bytes = await this._callZome("get_file_chunk", { input: fileChunkHash, local });
106
109
 
107
110
  return new Blob([new Uint8Array(bytes)]);
108
111
  }
package/src/mocks.ts CHANGED
@@ -1,15 +1,14 @@
1
1
  import {
2
- EntryHashMap,
3
2
  ZomeMock,
4
3
  hash,
5
4
  HashType,
6
5
  } from "@holochain-open-dev/utils";
7
- import { HoloHash } from "@holochain/client";
6
+ import { HoloHash, EntryHashMap } from "@holochain/client";
8
7
  import { FileMetadata } from "./types";
9
8
 
10
9
  export class FileStorageZomeMock extends ZomeMock {
11
- metadata = new EntryHashMap();
12
- chunks = new EntryHashMap();
10
+ metadata = new EntryHashMap<FileMetadata>();
11
+ chunks = new EntryHashMap<Uint8Array>();
13
12
 
14
13
  create_file_metadata(fileMetadata: FileMetadata) {
15
14
  const newId = hash(fileMetadata, HashType.ENTRY);
@@ -19,8 +18,8 @@ export class FileStorageZomeMock extends ZomeMock {
19
18
  return newId;
20
19
  }
21
20
 
22
- get_file_metadata(fileHash: HoloHash) {
23
- return this.metadata.get(fileHash);
21
+ get_file_metadata(input: { input: HoloHash; local?: boolean }) {
22
+ return this.metadata.get(input.input);
24
23
  }
25
24
 
26
25
  create_file_chunk(fileChunk: Uint8Array) {
@@ -31,7 +30,7 @@ export class FileStorageZomeMock extends ZomeMock {
31
30
  return newId;
32
31
  }
33
32
 
34
- get_file_chunk(fileChunkHash: HoloHash) {
35
- return this.chunks.get(fileChunkHash);
33
+ get_file_chunk(input: { input: HoloHash; local?: boolean }) {
34
+ return this.chunks.get(input.input);
36
35
  }
37
36
  }