UNPKG

js-isp

Version:

272 lines (271 loc) 7.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ICSP = void 0; const icsp_1 = require("./did/icsp"); const agent_1 = require("@dfinity/agent"); const nanoid_1 = require("nanoid"); const bucket_1 = require("../bucket"); const chunkSize = 2031616; class ICSP { constructor(icspCanisterId, agent) { this.agent = agent; this.icspCanisterId = icspCanisterId; this.ICSPActor = agent_1.Actor.createActor(icsp_1.idlFactory, { agent, canisterId: this.icspCanisterId }); } async get_cycle_balance() { try { return (await this.ICSPActor.getCycleBalance()); } catch (e) { throw e; } } async init() { try { return await this.ICSPActor.init(); } catch (e) { throw e; } } async allFiles() { try { const allFiles = await Promise.all([this.getAllArFileKey(), this.getAllIcFileKey(), this.getAllIpfsFileKey()]); return { ArFiles: allFiles[0], ICFiles: allFiles[1], IPFSFiles: allFiles[2] }; } catch (e) { throw e; } } async getFileInfo(key) { try { return await this.ICSPActor.getFileInfo(key); } catch (e) { throw e; } } async getAllArFileKey() { try { return await this.ICSPActor.getAllArFileKey(); } catch (e) { throw e; } } async getAllIcFileKey() { try { return await this.ICSPActor.getAllIcFileKey(); } catch (e) { throw e; } } async getAllIpfsFileKey() { try { return await this.ICSPActor.getAllIpfsFileKey(); } catch (e) { throw e; } } async recordFile(OtherFile) { try { return await this.ICSPActor.recordFile(OtherFile); } catch (e) { throw e; } } /* * @argument * file_key:the unique key of file * * @return * the bucket store this file * */ async get_bucket_of_file(file_key) { try { return (await this.ICSPActor.getBucketOfFile(file_key)); } catch (e) { throw e; } } /* * @return: All buckets under this icsp * */ async get_icsp_buckets() { try { return (await this.ICSPActor.getBuckets()); } catch (e) { throw e; } } async storeString(dataArr, is_http_open, key_arr) { try { const keyArr = []; const Actor = this.ICSPActor; const allPromise = []; for (let i = 0; i < dataArr.length; i++) { const file = dataArr[i]; const key = key_arr ? key_arr[i] : (0, nanoid_1.nanoid)(); keyArr.push(key); const data = new TextEncoder().encode(file); const arg = { key, value: data, file_type: "text/plain", index: BigInt(0), total_index: BigInt(1), total_size: BigInt(data.length), is_http_open }; allPromise.push(Actor.store(arg)); } await Promise.all(allPromise); return keyArr; } catch (e) { throw e; } } async FileRead(file) { try { return new Promise((resolve, reject) => { let start = 0; let currentChunk = 0; const total_index = Math.ceil(file.size / chunkSize); const allData = []; let blobSlice = //@ts-ignore File.prototype.slice || //@ts-ignore File.prototype.mozSlice || //@ts-ignore File.prototype.webkitSlice; let reader = new FileReader(); reader.onload = async function (e) { allData.push(new Uint8Array(e.target.result)); if (currentChunk === total_index) return resolve(allData); else loadChunk(); }; reader.onerror = (error) => { reject(error); }; const loadChunk = () => { const end = start + chunkSize; currentChunk++; reader.readAsArrayBuffer(blobSlice.call(file, start, end)); start = end; }; loadChunk(); }); } catch (e) { throw e; } } async storeBlob(files, is_http_open, key_arr) { try { const keyArr = []; const Actor = this.ICSPActor; const allPromise = []; for (let i = 0; i < files.length; i++) { const file = files[i]; const key = key_arr ? key_arr[i] : (0, nanoid_1.nanoid)(); keyArr.push(key); const total_size = file.size; const total_index = Math.ceil(total_size / chunkSize); const allData = await this.FileRead(file); for (let i = 0; i < allData.length; i++) { const arg = { key, value: allData[i], file_type: file.type, index: BigInt(i), total_index: BigInt(total_index), total_size: BigInt(total_size), is_http_open }; allPromise.push(Actor.store(arg)); } } await Promise.all(allPromise); return keyArr; } catch (e) { throw e; } } /* * @argument: * metadata:metadata to upload * is_http_open:Is it possible to access via http *@return :null * */ async store_file(metadata, is_http_open, key_arr) { try { if (key_arr && metadata.length !== key_arr.length) throw new Error("文件数组和key数组长度不一"); if (typeof (metadata[0]) === "string") { return await this.storeString(metadata, is_http_open, key_arr); } else { return await this.storeBlob(metadata, is_http_open, key_arr); } } catch (e) { throw e; } } async update_data(key, new_data, is_http_open) { try { await this.delete_file(key); const update_res = await this.store_file([new_data], is_http_open, [key]); return update_res[0] === key; } catch (e) { throw e; } } async delete_file(file_key) { try { const Actor = this.ICSPActor; return await Actor.delete(file_key); } catch (e) { throw e; } } async get_file(fileKey) { try { const bucket = (await this.get_bucket_of_file(fileKey))[0]; if (bucket) { const bucketApi = new bucket_1.Bucket(String(bucket), this.agent); return await bucketApi.get_file(fileKey); } else { throw new Error("There is no bucket to store this file"); } } catch (e) { throw e; } } async getVersion() { try { return (await this.ICSPActor.getVersion()); } catch (e) { throw e; } } } exports.ICSP = ICSP;