Skip to content

Commit

Permalink
Merge pull request #32 from PinataCloud/feat/integrate-image-optimiza…
Browse files Browse the repository at this point in the history
…tions

feat/integrate image optimizations
  • Loading branch information
stevedylandev authored Sep 11, 2024
2 parents 70a775a + f1384c2 commit 52f575c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 57 deletions.
52 changes: 25 additions & 27 deletions src/core/gateway/createSignedURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,38 @@ import {
export const createSignedURL = async (
config: PinataConfig | undefined,
options: SignedUrlOptions,
imgOpts: OptimizeImageOptions,
): Promise<string> => {
if (!config) {
throw new ValidationError("Pinata configuration is missing");
}

let newUrl: string = `${config?.pinataGateway}/files/${options.cid}`;

const params = new URLSearchParams();

if (imgOpts) {
if (imgOpts.width) params.append("img-width", imgOpts.width.toString());
if (imgOpts.height) params.append("img-height", imgOpts.height.toString());
if (imgOpts.dpr) params.append("img-dpr", imgOpts.dpr.toString());
if (imgOpts.fit) params.append("img-fit", imgOpts.fit);
if (imgOpts.gravity) params.append("img-gravity", imgOpts.gravity);
if (imgOpts.quality)
params.append("img-quality", imgOpts.quality.toString());
if (imgOpts.format) params.append("img-format", imgOpts.format);
if (imgOpts.animation !== undefined)
params.append("img-anim", imgOpts.animation.toString());
if (imgOpts.sharpen)
params.append("img-sharpen", imgOpts.sharpen.toString());
if (imgOpts.onError === true) params.append("img-onerror", "redirect");
if (imgOpts.metadata) params.append("img-metadata", imgOpts.metadata);
}

const queryString = params.toString();
if (queryString) {
newUrl += `?${queryString}`;
}

const date = options?.date || Math.floor(new Date().getTime() / 1000);

const payload = JSON.stringify({
Expand All @@ -35,33 +60,6 @@ export const createSignedURL = async (
endpoint = config.endpointUrl;
}

// const params = new URLSearchParams();
// if (config?.pinataGatewayKey) {
// params.append("pinataGatewayToken", config.pinataGatewayKey);
// }

// if (options) {
// if (options.width) params.append("img-width", options.width.toString());
// if (options.height) params.append("img-height", options.height.toString());
// if (options.dpr) params.append("img-dpr", options.dpr.toString());
// if (options.fit) params.append("img-fit", options.fit);
// if (options.gravity) params.append("img-gravity", options.gravity);
// if (options.quality)
// params.append("img-quality", options.quality.toString());
// if (options.format) params.append("img-format", options.format);
// if (options.animation !== undefined)
// params.append("img-anim", options.animation.toString());
// if (options.sharpen)
// params.append("img-sharpen", options.sharpen.toString());
// if (options.onError === true) params.append("img-onerror", "redirect");
// if (options.metadata) params.append("img-metadata", options.metadata);
// }

// const queryString = params.toString();
// if (queryString) {
// newUrl += `?${queryString}`;
// }

try {
const request = await fetch(`${endpoint}/files/sign`, {
method: "POST",
Expand Down
27 changes: 17 additions & 10 deletions src/core/gateway/getCid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,34 @@ export const getCid = async (

const date = Math.floor(new Date().getTime() / 1000);

console.log(newUrl);

const payload = JSON.stringify({
url: newUrl,
date: date,
expires: 30,
method: "GET",
});

const signedUrlRequest = await fetch(
"https://api.pinata.cloud/v3/files/sign",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config?.pinataJwt}`,
},
body: payload,
let endpoint: string = "https://api.pinata.cloud/v3";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

const signedUrlRequest = await fetch(`${endpoint}/files/sign`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config?.pinataJwt}`,
},
);
body: payload,
});

const signedUrl = await signedUrlRequest.json();

console.log(signedUrl);

// if (config?.pinataGatewayKey) {
// params.append("pinataGatewayToken", config.pinataGatewayKey);
// }
Expand Down
62 changes: 42 additions & 20 deletions src/core/pinataSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,12 @@ class Gateways {
this.config = newConfig;
}

get(cid: string): Promise<GetCIDResponse> {
return getCid(this.config, cid);
get(cid: string): OptimizeImageGetCid {
return new OptimizeImageGetCid(this.config, cid);
}

createSignedURL(options: SignedUrlOptions): Promise<string> {
return createSignedURL(this.config, options);
createSignedURL(options: SignedUrlOptions): OptimizeImageCreateSignedURL {
return new OptimizeImageCreateSignedURL(this.config, options);
}

// get(cid: string): OptimizeImage {
Expand Down Expand Up @@ -446,25 +446,47 @@ class Gateways {
// }
}

// class OptimizeImage {
// private config: PinataConfig | undefined;
// private cid: string;
// private options: OptimizeImageOptions = {};
class OptimizeImageGetCid {
private config: PinataConfig | undefined;
private cid: string;
private options: OptimizeImageOptions = {};

// constructor(config: PinataConfig | undefined, cid: string) {
// this.config = config;
// this.cid = cid;
// }
constructor(config: PinataConfig | undefined, cid: string) {
this.config = config;
this.cid = cid;
}

// optimizeImage(options: OptimizeImageOptions): OptimizeImage {
// this.options = { ...this.options, ...options };
// return this;
// }
optimizeImage(options: OptimizeImageOptions): OptimizeImageGetCid {
this.options = { ...this.options, ...options };
return this;
}

// then(onfulfilled?: ((value: GetCIDResponse) => any) | null): Promise<any> {
// return getCid(this.config, this.cid, this.options).then(onfulfilled);
// }
// }
then(onfulfilled?: ((value: GetCIDResponse) => any) | null): Promise<any> {
return getCid(this.config, this.cid, this.options).then(onfulfilled);
}
}

class OptimizeImageCreateSignedURL {
private config: PinataConfig | undefined;
private urlOpts: SignedUrlOptions;
private imgOpts: OptimizeImageOptions = {};

constructor(config: PinataConfig | undefined, urlOpts: SignedUrlOptions) {
this.config = config;
this.urlOpts = urlOpts;
}

optimizeImage(options: OptimizeImageOptions): OptimizeImageCreateSignedURL {
this.imgOpts = { ...this.imgOpts, ...options };
return this;
}

then(onfulfilled?: ((value: string) => any) | null): Promise<any> {
return createSignedURL(this.config, this.urlOpts, this.imgOpts).then(
onfulfilled,
);
}
}

// class Usage {
// config: PinataConfig | undefined;
Expand Down

0 comments on commit 52f575c

Please sign in to comment.