Skip to content

Commit

Permalink
storage: add method for pinning multiple cids (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
shomix authored Oct 23, 2023
1 parent 1716644 commit fafa8d5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/upload-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class UploadManager {
throw new Error(errorMessage);
}
}

public async pinCID(configuration: {
name: string;
organizationId?: string;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<p align="center">
<a href="https://www.npmjs.com/package/@spheron/storage" target="_blank" rel="noreferrer">
<img src="https://img.shields.io/static/v1?label=npm&message=v2.0.3&color=green" />
<img src="https://img.shields.io/static/v1?label=npm&message=v2.0.4&color=green" />
</a>
<a href="https://github.com/spheronFdn/sdk/blob/main/LICENSE" target="_blank" rel="noreferrer">
<img src="https://img.shields.io/static/v1?label=license&message=Apache%202.0&color=red" />
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit fafa8d5

Please sign in to comment.