@holochain-open-dev/file-storage 0.5.0 → 0.6.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.
@@ -31,7 +31,7 @@ export class FileStorageClient {
31
31
  name: file.name,
32
32
  size: file.size,
33
33
  file_type: file.type,
34
- last_modified: file.lastModified,
34
+ last_modified: file.lastModified * 1000,
35
35
  chunks_hashes: chunksHashes,
36
36
  };
37
37
  const hash = await this._callZome("create_file_metadata", fileToCreate);
@@ -46,7 +46,7 @@ export class FileStorageClient {
46
46
  const fetchChunksPromises = metadata.chunks_hashes.map((hash) => this.fetchChunk(hash));
47
47
  const chunks = await Promise.all(fetchChunksPromises);
48
48
  const file = new File(chunks, metadata.name, {
49
- lastModified: metadata.last_modifed,
49
+ lastModified: Math.floor(metadata.last_modifed / 1000),
50
50
  type: metadata.file_type,
51
51
  });
52
52
  return file;
@@ -1 +1 @@
1
- {"version":3,"file":"file-storage-client.js","sourceRoot":"","sources":["../src/file-storage-client.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,iBAAiB;IAC5B;;;;OAIG;IACH,YACS,MAAsB,EACtB,QAAgB,EAChB,WAAmB,cAAc;QAFjC,WAAM,GAAN,MAAM,CAAgB;QACtB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,aAAQ,GAAR,QAAQ,CAAyB;IACvC,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,MAAM,YAAY,GAAqB,EAAE,CAAC;QAC1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,C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ent":["import type {\n AppAgentCallZomeRequest,\n AppAgentClient,\n} 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 */\n constructor(\n public client: AppAgentClient,\n public roleName: string,\n public zomeName: string = \"file_storage\"\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 const chunksHashes: Array<EntryHash> = [];\n for (let i = 0; i < blobs.length; i++) {\n const chunkHash = await this._createChunk(blobs[i]);\n chunksHashes.push(chunkHash);\n if (onProgress) {\n onProgress(((i + 1) * 1.0) / numberOfChunks, bytesPerChunk * (i + 1));\n }\n }\n\n const fileToCreate = {\n name: file.name,\n size: file.size,\n file_type: file.type,\n last_modified: file.lastModified,\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: metadata.last_modifed,\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: AppAgentCallZomeRequest = {\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":"AAOA,MAAM,OAAO,iBAAiB;IAC5B;;;;OAIG;IACH,YACS,MAAsB,EACtB,QAAgB,EAChB,WAAmB,cAAc;QAFjC,WAAM,GAAN,MAAM,CAAgB;QACtB,aAAQ,GAAR,QAAQ,CAAQ;QAChB,aAAQ,GAAR,QAAQ,CAAyB;IACvC,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,MAAM,YAAY,GAAqB,E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ACP,OAAO;SACR,CAAC;QACF,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;CACF","sourcesContent":["import type {\n AppAgentCallZomeRequest,\n AppAgentClient,\n} 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 */\n constructor(\n public client: AppAgentClient,\n public roleName: string,\n public zomeName: string = \"file_storage\"\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 const chunksHashes: Array<EntryHash> = [];\n for (let i = 0; i < blobs.length; i++) {\n const chunkHash = await this._createChunk(blobs[i]);\n chunksHashes.push(chunkHash);\n if (onProgress) {\n onProgress(((i + 1) * 1.0) / numberOfChunks, bytesPerChunk * (i + 1));\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: AppAgentCallZomeRequest = {\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.5.0",
3
+ "version": "0.6.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",
@@ -19,9 +19,9 @@
19
19
  "format": "eslint --ext .ts,.html src --fix --ignore-path .gitignore"
20
20
  },
21
21
  "dependencies": {
22
- "@holochain-open-dev/elements": "^0.6.0",
23
- "@holochain-open-dev/utils": "^0.14.0",
24
- "@holochain/client": "^0.15.0",
22
+ "@holochain-open-dev/elements": "^0.7.0",
23
+ "@holochain-open-dev/utils": "^0.15.0",
24
+ "@holochain/client": "^0.16.1",
25
25
  "@lit-labs/context": "^0.2.0",
26
26
  "@lit-labs/task": "^2.0.0",
27
27
  "@lit/localize": "^0.11.4",