@holochain-open-dev/file-storage 0.300.0-rc → 0.400.0-dev.1
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.
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import type { AppClient } from "@holochain/client";
|
|
1
|
+
import type { AppClient, CellId } from "@holochain/client";
|
|
2
2
|
import { EntryHash } from "@holochain/client";
|
|
3
3
|
import { FileMetadata } from "./types";
|
|
4
4
|
export declare class FileStorageClient {
|
|
5
5
|
client: AppClient;
|
|
6
6
|
roleName: string;
|
|
7
7
|
zomeName: string;
|
|
8
|
+
cellId: CellId | undefined;
|
|
8
9
|
/**
|
|
9
10
|
* @param client connection to the holochain backend
|
|
10
11
|
* @param roleName
|
|
11
12
|
* @param zomeName the zome name of the file_storage zome in the given cell
|
|
13
|
+
* @param cellId optional cellId parameter for use when client is associated with a clone-cell
|
|
12
14
|
*/
|
|
13
|
-
constructor(client: AppClient, roleName: string, zomeName?: string);
|
|
15
|
+
constructor(client: AppClient, roleName: string, zomeName?: string, cellId?: CellId | undefined);
|
|
14
16
|
/**
|
|
15
17
|
* Upload a file to the file_storage zome, splitting it into chunks
|
|
16
18
|
*
|
|
@@ -3,11 +3,13 @@ export class FileStorageClient {
|
|
|
3
3
|
* @param client connection to the holochain backend
|
|
4
4
|
* @param roleName
|
|
5
5
|
* @param zomeName the zome name of the file_storage zome in the given cell
|
|
6
|
+
* @param cellId optional cellId parameter for use when client is associated with a clone-cell
|
|
6
7
|
*/
|
|
7
|
-
constructor(client, roleName, zomeName = "file_storage") {
|
|
8
|
+
constructor(client, roleName, zomeName = "file_storage", cellId = undefined) {
|
|
8
9
|
this.client = client;
|
|
9
10
|
this.roleName = roleName;
|
|
10
11
|
this.zomeName = zomeName;
|
|
12
|
+
this.cellId = cellId;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
15
|
* Upload a file to the file_storage zome, splitting it into chunks
|
|
@@ -88,12 +90,18 @@ export class FileStorageClient {
|
|
|
88
90
|
return this._callZome("create_file_chunk", new Uint8Array(bytes));
|
|
89
91
|
}
|
|
90
92
|
_callZome(fn_name, payload) {
|
|
91
|
-
const req = {
|
|
92
|
-
|
|
93
|
+
const req = this.cellId ? {
|
|
94
|
+
cell_id: this.cellId,
|
|
93
95
|
zome_name: this.zomeName,
|
|
94
96
|
fn_name,
|
|
95
97
|
payload,
|
|
96
|
-
}
|
|
98
|
+
} :
|
|
99
|
+
{
|
|
100
|
+
role_name: this.roleName,
|
|
101
|
+
zome_name: this.zomeName,
|
|
102
|
+
fn_name,
|
|
103
|
+
payload,
|
|
104
|
+
};
|
|
97
105
|
return this.client.callZome(req);
|
|
98
106
|
}
|
|
99
107
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-storage-client.js","sourceRoot":"","sources":["../src/file-storage-client.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,iBAAiB;IAC5B
|
|
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,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C7C,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 { AppCallZomeRequest, 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: AppCallZomeRequest =\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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holochain-open-dev/file-storage",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.400.0-dev.1",
|
|
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.
|
|
24
|
-
"@holochain-open-dev/utils": "^0.
|
|
25
|
-
"@holochain/client": "^0.
|
|
23
|
+
"@holochain-open-dev/elements": "^0.400.0-dev.0",
|
|
24
|
+
"@holochain-open-dev/utils": "^0.400.0-dev.0",
|
|
25
|
+
"@holochain/client": "^0.18.0-dev.1",
|
|
26
26
|
"@lit-labs/task": "^2.0.0",
|
|
27
27
|
"@lit/context": "^1.0.0",
|
|
28
28
|
"@lit/localize": "^0.12.0",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AppCallZomeRequest, AppClient } from "@holochain/client";
|
|
1
|
+
import type { AppCallZomeRequest, AppClient, CellId } from "@holochain/client";
|
|
2
2
|
import { EntryHash } from "@holochain/client";
|
|
3
3
|
import { FileMetadata } from "./types";
|
|
4
4
|
|
|
@@ -7,11 +7,13 @@ export class FileStorageClient {
|
|
|
7
7
|
* @param client connection to the holochain backend
|
|
8
8
|
* @param roleName
|
|
9
9
|
* @param zomeName the zome name of the file_storage zome in the given cell
|
|
10
|
+
* @param cellId optional cellId parameter for use when client is associated with a clone-cell
|
|
10
11
|
*/
|
|
11
12
|
constructor(
|
|
12
13
|
public client: AppClient,
|
|
13
14
|
public roleName: string,
|
|
14
|
-
public zomeName: string = "file_storage"
|
|
15
|
+
public zomeName: string = "file_storage",
|
|
16
|
+
public cellId: CellId|undefined = undefined,
|
|
15
17
|
) {}
|
|
16
18
|
|
|
17
19
|
/**
|
|
@@ -127,12 +129,19 @@ export class FileStorageClient {
|
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
private _callZome(fn_name: string, payload: any) {
|
|
130
|
-
const req: AppCallZomeRequest =
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
132
|
+
const req: AppCallZomeRequest =
|
|
133
|
+
this.cellId ? {
|
|
134
|
+
cell_id: this.cellId,
|
|
135
|
+
zome_name: this.zomeName,
|
|
136
|
+
fn_name,
|
|
137
|
+
payload,
|
|
138
|
+
} :
|
|
139
|
+
{
|
|
140
|
+
role_name: this.roleName,
|
|
141
|
+
zome_name: this.zomeName,
|
|
142
|
+
fn_name,
|
|
143
|
+
payload,
|
|
144
|
+
}
|
|
136
145
|
return this.client.callZome(req);
|
|
137
146
|
}
|
|
138
147
|
}
|