Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add asBlob parameter to baseIpfsStorage to retrieve ipfs item a… #787

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 66 additions & 13 deletions packages/ipfs-storage/src/ipfs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
return cid;
}

public async get<T>(uriOrHash: string, asJson = true): Promise<T | string> {
public async get<T>(
uriOrHash: string,
asJson = true,
asBlob = false
): Promise<T | string | Blob | Uint8Array> {
let cid: CID = null;
try {
cid = CID.parse(
Expand All @@ -34,30 +38,79 @@
}

const value = await (cid
? this.getByCID<T>(cid.toString(), asJson)
: this.getByURL<T>(uriOrHash, asJson));
? this.getByCID<T>(cid.toString(), asJson, asBlob)
: this.getByURL<T>(uriOrHash, asJson, asBlob));
return value;
}

public async getByCID<T>(cid: string, asJson = true): Promise<T | string> {
public async getByCID<T>(
cid: string,
asJson: true,
asBlob: false
): Promise<T>;
public async getByCID(
cid: string,
asJson: false,
asBlob: true
): Promise<Blob>;
public async getByCID(
cid: string,
asJson: false,
asBlob: false
): Promise<Uint8Array>;
public async getByCID<T>(
cid: string,
asJson: boolean,
asBlob: boolean
): Promise<Uint8Array | Blob | T>;
public async getByCID<T>(
cid: string,
asJson = true,
asBlob = false
): Promise<Uint8Array | Blob | T> {
const chunks = [];
for await (const chunk of this.ipfsClient.cat(cid)) {
chunks.push(chunk);
}
const data = concat(chunks);
if (!asJson) {
return data as unknown as T;
if (!asJson && asBlob) {
return new Blob([data]);

Check warning on line 76 in packages/ipfs-storage/src/ipfs/base.ts

View check run for this annotation

Codecov / codecov/patch

packages/ipfs-storage/src/ipfs/base.ts#L76

Added line #L76 was not covered by tests
} else if (!asJson) {
return data;

Check warning on line 78 in packages/ipfs-storage/src/ipfs/base.ts

View check run for this annotation

Codecov / codecov/patch

packages/ipfs-storage/src/ipfs/base.ts#L78

Added line #L78 was not covered by tests
}
const dataStr = toString(data);
return JSON.parse(dataStr);
return JSON.parse(dataStr) as T;
}

public async getByURL<T>(url: string, asJson = true): Promise<T | string> {
public async getByURL<T>(
url: string,
asJson: true,
asBlob: false
): Promise<T>;
public async getByURL(
url: string,
asJson: false,
asBlob: true
): Promise<Blob>;
public async getByURL(
url: string,
asJson: false,
asBlob: false
): Promise<string>;
public async getByURL<T>(
url: string,
asJson: boolean,
asBlob: boolean
): Promise<string | Blob | T>;
public async getByURL<T>(
url: string,
asJson = true,
asBlob = false
): Promise<string | Blob | T> {
const response = await fetch(url);

if (!asJson) {
if (!asJson && asBlob) {
return response.blob();

Check warning on line 110 in packages/ipfs-storage/src/ipfs/base.ts

View check run for this annotation

Codecov / codecov/patch

packages/ipfs-storage/src/ipfs/base.ts#L110

Added line #L110 was not covered by tests
Comment on lines +109 to +110
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change are these 2 lines

} else if (!asJson) {
return response.text();
}
return response.json();
return response.json() as T;
}
}
4 changes: 2 additions & 2 deletions packages/ipfs-storage/src/ipfs/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class IpfsMetadataStorage
* @returns Offer metadata.
*/
public async getMetadata(metadataUriOrHash: string): Promise<AnyMetadata> {
const metadata = (await this.get<ERC721Metadata>(
const metadata = (await this.get(
metadataUriOrHash
)) as ERC721Metadata;
)) as unknown as ERC721Metadata;
if (metadata.type) {
this.validateMetadata(metadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export interface WithUploadToIpfsProps {
saveToIpfs: (
files: File[] | null
) => Promise<false | FileProps[] | undefined>;
loadMedia: (src: string) => string;
removeFile: (src: string) => void;
loadMedia: (src: string) => Promise<string | undefined>;
removeFile: (src: string) => Promise<void>;
}
export function WithUploadToIpfs<P extends WithUploadToIpfsProps>(
WrappedComponent: React.ComponentType<P>
Expand Down
5 changes: 3 additions & 2 deletions packages/react-kit/src/hooks/useRenderTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export function useRenderTemplate(
setRenderStatus(ProgressStatus.LOADING);
if (ipfsMetadataStorage && coreSDK) {
try {
const rawTemplate = await ipfsMetadataStorage.get<Uint8Array>(
const rawTemplate = (await ipfsMetadataStorage.get<Uint8Array>(
templateUrl,
false,
false
);
)) as Uint8Array;
const template = Buffer.from(rawTemplate).toString("utf-8");
let theOfferData = offerData;
// If the offerData is not specified, retrieve the data from offerId
Expand Down
6 changes: 2 additions & 4 deletions packages/react-kit/src/lib/base64/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ export const fetchIpfsBase64Media = async (
return [];
}
const fetchPromises = ipfsLinks.map(async (src) => {
const imgData = await ipfsMetadataStorage.get(src, false);
const base64str = await blobToBase64(
new Blob([imgData as unknown as BlobPart])
);
const imgData = await ipfsMetadataStorage.get<Blob>(src, false, true);
const base64str = await blobToBase64(new Blob([imgData]));
if (!String(base64str).includes("base64")) {
throw new Error("Decoded image is not in base64");
}
Expand Down
Loading