UNPKG

@aut-labs/sdk

Version:

The TS/JS SDK package aims to make it easy for frontends/backends to integrate with Aut Smart Contracts

121 lines 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IPFSClient = void 0; function createThumbnail(file, maxWidth = 500, maxHeight = 500) { return new Promise((resolve, reject) => { // STEP 1: Create an image element const img = new Image(); img.src = URL.createObjectURL(file); img.onload = () => { // STEP 2: Draw the image onto a canvas const canvas = document.createElement("canvas"); const ctx = canvas.getContext("2d"); const scale = Math.min(maxWidth / img.width, maxHeight / img.height); const width = img.width * scale; const height = img.height * scale; canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0, width, height); // STEP 3: Convert canvas to a Blob canvas.toBlob((blob) => { if (blob) { // Resolve the promise with the Blob resolve(blob); } else { // Reject the promise if there was an error converting canvas to Blob reject(new Error("Unable to create thumbnail")); } }, "image/png"); // Choose your desired output format // Release the Object URL now that we've used it URL.revokeObjectURL(img.src); }; img.onerror = reject; }); } class IPFSClient { constructor(config) { this.config = config; this._baseUrl = "https://api.pinata.cloud"; } async makeRequest(endpoint, method, body, headers = {}) { const url = `${this._baseUrl}/${endpoint}`; const response = await fetch(url, { method, headers: { ...headers, pinata_api_key: this.config.apiKey, pinata_secret_api_key: this.config.secretApiKey }, body }); if (!response.ok) { const errorBody = await response.text(); throw new Error(`IPFS API request failed: ${response.status} ${response.statusText} - ${errorBody}`); } return response.json(); } async sendFileToIPFSWithThumbnail(file) { if (file) { try { const originaHash = await this.sendFileToIPFS(file); const thumbnail = await createThumbnail(file); const formData = new FormData(); formData.append("file", thumbnail); const resFile = await this.makeRequest(`pinning/pinFileToIPFS`, "POST", formData); const thumbnailHash = `ipfs://${resFile.IpfsHash}`; console.log("Thumbnail: ", this.retrieveUrl(thumbnailHash)); console.log("Original: ", this.retrieveUrl(originaHash)); return { original: originaHash, thumbnail: thumbnailHash }; } catch (error) { console.error("Error sending File to IPFS: ", error); throw error; // It is better to throw the error } } throw new Error("No file provided"); } async sendFileToIPFS(file) { if (file) { try { const formData = new FormData(); formData.append("file", file); const resFile = await this.makeRequest(`pinning/pinFileToIPFS`, "POST", formData); const ImgHash = `ipfs://${resFile.IpfsHash}`; console.log("Image: ", this.retrieveUrl(ImgHash)); return ImgHash; } catch (error) { console.error("Error sending File to IPFS: ", error); throw error; // It is better to throw the error } } throw new Error("No file provided"); } async sendJSONToIPFS(jsonData) { try { const resJSON = await this.makeRequest(`pinning/pinJSONToIPFS`, "POST", JSON.stringify(jsonData), { "Content-Type": "application/json" }); const JSONHash = `ipfs://${resJSON.IpfsHash}`; console.log("JSON: ", this.retrieveUrl(JSONHash)); return JSONHash; } catch (error) { console.error("Error sending JSON to IPFS: ", error); throw error; } } retrieveUrl(ipfsHash) { const prefix = "ipfs://"; if (ipfsHash.startsWith(prefix)) { ipfsHash = ipfsHash.substring(prefix.length); } return `${this.config.gatewayUrl}${ipfsHash}`; } } exports.IPFSClient = IPFSClient; //# sourceMappingURL=ipfs.service.js.map