UNPKG

nexus-react-core

Version:

A comprehensive React toolkit with services, hooks, and Redux store management

57 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveImageFromUrl = saveImageFromUrl; exports.base64ToBlob = base64ToBlob; const apiCall_1 = require("../hooks/apiCall"); /** * Saves an image from a URL to the server * This is particularly useful for DALL-E generated images which expire after 24 hours * @param imageUrl The URL of the image to save * @returns The URL of the saved image on the server */ async function saveImageFromUrl(imageUrl) { try { // Send the URL to the backend, which will download and save the image const response = await (0, apiCall_1.apiCall)("/images/save-from-url", "POST", { url: imageUrl, source: "dalle", // Tag the source of the image }); // Return the permanent URL provided by the backend if (response && response.url) { console.log("Image successfully saved to server:", response.url); return response.url; } else { console.error("Unexpected response format from server:", response); throw new Error("Failed to save image: Invalid server response"); } } catch (error) { console.error("Error saving image to server:", error); // Return the original URL if the save fails // At least the app will work until the URL expires return imageUrl; } } /** * Converts a base64 image to a Blob * @param base64 The base64 string of the image * @returns A Blob containing the image data */ function base64ToBlob(base64) { const parts = base64.split(";base64,"); const contentType = parts[0].split(":")[1]; const byteCharacters = atob(parts[1]); const byteArrays = []; for (let i = 0; i < byteCharacters.length; i += 512) { const slice = byteCharacters.slice(i, i + 512); const byteNumbers = new Array(slice.length); for (let j = 0; j < slice.length; j++) { byteNumbers[j] = slice.charCodeAt(j); } const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } return new Blob(byteArrays, { type: contentType }); } //# sourceMappingURL=imageService.js.map