From fafa8d545cfd8cb680d9772b1eca72b737ab240e Mon Sep 17 00:00:00 2001 From: shomix <100691266+shomix@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:19:08 +0200 Subject: [PATCH] storage: add method for pinning multiple cids (#168) --- package-lock.json | 8 ++--- packages/core/package.json | 2 +- packages/core/src/upload-manager.ts | 45 +++++++++++++++++++++++++++++ packages/storage/README.md | 2 +- packages/storage/package.json | 4 +-- packages/storage/src/index.ts | 15 ++++++++++ 6 files changed, 68 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3cf9357f..1b5b4e3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6432,7 +6432,7 @@ }, "packages/core": { "name": "@spheron/core", - "version": "2.0.4", + "version": "2.0.5", "license": "Apache-2.0", "dependencies": { "async-lock": "^1.4.0", @@ -6567,10 +6567,10 @@ }, "packages/storage": { "name": "@spheron/storage", - "version": "2.0.3", + "version": "2.0.4", "license": "Apache-2.0", "dependencies": { - "@spheron/core": "2.0.4", + "@spheron/core": "2.0.5", "@spheron/encryption": "1.0.0", "form-data": "^4.0.0", "multiformats": "^9.9.0" @@ -7973,7 +7973,7 @@ "@spheron/storage": { "version": "file:packages/storage", "requires": { - "@spheron/core": "2.0.4", + "@spheron/core": "2.0.5", "@spheron/encryption": "1.0.0", "@types/node": "^18.13.0", "@typescript-eslint/eslint-plugin": "^5.51.0", diff --git a/packages/core/package.json b/packages/core/package.json index 659d1a50..84f4c50f 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@spheron/core", - "version": "2.0.4", + "version": "2.0.5", "description": "Shared core package for all sdk packages", "keywords": [ "Storage", diff --git a/packages/core/src/upload-manager.ts b/packages/core/src/upload-manager.ts index edcfd6ca..519ad7c9 100644 --- a/packages/core/src/upload-manager.ts +++ b/packages/core/src/upload-manager.ts @@ -79,6 +79,7 @@ class UploadManager { throw new Error(errorMessage); } } + public async pinCID(configuration: { name: string; organizationId?: string; @@ -126,6 +127,50 @@ class UploadManager { } } + public async pinCIDs(configuration: { + name: string; + organizationId?: string; + token: string; + cids: string[]; + }): Promise< + { + uploadId: string; + cid: string; + }[] + > { + try { + if (!configuration.name) { + throw new Error("Bucket name is not provided."); + } + + let url = `${this.spheronApiUrl}/v2/ipfs/pins?bucket=${configuration.name}`; + if (configuration.organizationId) { + url += `&organization=${configuration.organizationId}`; + } + + const response = await axios.post< + { + uploadId: string; + cid: string; + }[] + >( + url, + { + cids: configuration.cids, + }, + { + headers: { + Authorization: `Bearer ${configuration.token}`, + }, + } + ); + return response.data; + } catch (error) { + const errorMessage = error?.response?.data?.message || error?.message; + throw new Error(errorMessage); + } + } + public async getCIDStatus(CID: string): Promise<{ pinStatus: PinStatus }> { try { if (!CID) { diff --git a/packages/storage/README.md b/packages/storage/README.md index c961a1c6..72c28ca2 100644 --- a/packages/storage/README.md +++ b/packages/storage/README.md @@ -14,7 +14,7 @@
- + diff --git a/packages/storage/package.json b/packages/storage/package.json index dd04a13f..2cabf06d 100644 --- a/packages/storage/package.json +++ b/packages/storage/package.json @@ -1,6 +1,6 @@ { "name": "@spheron/storage", - "version": "2.0.3", + "version": "2.0.4", "description": "Typescript library for uploading files or directory to IPFS, Filecoin or Arweave via Spheron", "keywords": [ "Storage", @@ -35,7 +35,7 @@ "dist/" ], "dependencies": { - "@spheron/core": "2.0.4", + "@spheron/core": "2.0.5", "@spheron/encryption": "1.0.0", "form-data": "^4.0.0", "multiformats": "^9.9.0" diff --git a/packages/storage/src/index.ts b/packages/storage/src/index.ts index 2880bb33..b1ec1b4f 100644 --- a/packages/storage/src/index.ts +++ b/packages/storage/src/index.ts @@ -337,6 +337,21 @@ export class SpheronClient extends ScopeExtractor { }); } + async pinCIDs(configuration: { name: string; cids: string[] }): Promise< + { + uploadId: string; + cid: string; + }[] + > { + await this.validateStorageOrganizationType(); + + return await this.uploadManager.pinCIDs({ + name: configuration.name, + cids: configuration.cids, + token: this.configuration.token, + }); + } + async getOrganizationBuckets( organizationId: string, options: {