@zodic/shared 0.0.79 → 0.0.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -194,4 +194,162 @@ export class LeonardoService {
194
194
  throw error;
195
195
  }
196
196
  }
197
+
198
+ async uploadImage(
199
+ file: File
200
+ ): Promise<{ uploadedImageId: string; uploadedImageUrl: string }> {
201
+ const fileName = file.name;
202
+ const fileExtension = fileName.split('.').pop()?.toLowerCase();
203
+ const mimeType = `image/${fileExtension}`;
204
+
205
+ if (!fileExtension) {
206
+ throw new Error('Invalid file extension');
207
+ }
208
+
209
+ console.log('Starting uploadImage with details:', {
210
+ fileName,
211
+ fileExtension,
212
+ mimeType,
213
+ });
214
+
215
+ try {
216
+ console.log('Requesting presigned URL...');
217
+ const presignedUrlData = await this.context
218
+ .api()
219
+ .callLeonardo.generatePresignedUrl({
220
+ extension: fileExtension,
221
+ });
222
+
223
+ if (
224
+ !presignedUrlData.url ||
225
+ !presignedUrlData.fields ||
226
+ !presignedUrlData.id
227
+ ) {
228
+ throw new Error('Invalid presigned URL response from API');
229
+ }
230
+
231
+ console.log('Uploading file...');
232
+ await this.uploadToPresignedUrl(
233
+ presignedUrlData.url,
234
+ presignedUrlData.fields,
235
+ file
236
+ );
237
+
238
+ const uploadedImageId = presignedUrlData.id;
239
+ const uploadedImageUrl = `https://cdn.leonardo.ai/users/b117a933-e5c9-45b2-96aa-4b619c2d74ba/initImages/${uploadedImageId}.${fileExtension}`;
240
+
241
+ console.log('File uploaded successfully:', {
242
+ uploadedImageId,
243
+ uploadedImageUrl,
244
+ });
245
+
246
+ return { uploadedImageId, uploadedImageUrl };
247
+ } catch (error: any) {
248
+ console.error('Error during image upload:', error.message);
249
+ throw new Error('Failed to upload image');
250
+ }
251
+ }
252
+
253
+ async uploadImageByUrl(
254
+ imageUrl: string
255
+ ): Promise<{ uploadedImageId: string; uploadedImageUrl: string }> {
256
+ console.log('Starting uploadImageByUrl with imageUrl:', imageUrl);
257
+
258
+ try {
259
+ // Fetch the image from the provided URL
260
+ console.log('Fetching image from URL...');
261
+ const response = await fetch(imageUrl);
262
+
263
+ if (!response.ok) {
264
+ throw new Error(
265
+ `Failed to fetch image from URL: ${response.statusText}`
266
+ );
267
+ }
268
+
269
+ const blob = await response.blob();
270
+ const fileExtension = imageUrl.split('.').pop()?.toLowerCase() || 'jpg';
271
+ const mimeType = blob.type || `image/${fileExtension}`;
272
+
273
+ if (!fileExtension || !mimeType.startsWith('image/')) {
274
+ throw new Error('Invalid image URL or file type');
275
+ }
276
+
277
+ console.log('Image fetched successfully:', {
278
+ fileExtension,
279
+ mimeType,
280
+ });
281
+
282
+ // Request a presigned URL for uploading
283
+ console.log('Requesting presigned URL...');
284
+ const presignedUrlData = await this.context
285
+ .api()
286
+ .callLeonardo.generatePresignedUrl({
287
+ extension: fileExtension,
288
+ });
289
+
290
+ if (
291
+ !presignedUrlData.url ||
292
+ !presignedUrlData.fields ||
293
+ !presignedUrlData.id
294
+ ) {
295
+ throw new Error('Invalid presigned URL response from API');
296
+ }
297
+
298
+ console.log('Presigned URL obtained:', presignedUrlData);
299
+
300
+ // Prepare the file to upload
301
+ const file = new File([blob], `upload.${fileExtension}`, {
302
+ type: mimeType,
303
+ });
304
+
305
+ console.log('Uploading file to presigned URL...');
306
+ await this.uploadToPresignedUrl(
307
+ presignedUrlData.url,
308
+ presignedUrlData.fields,
309
+ file as any
310
+ );
311
+
312
+ const uploadedImageId = presignedUrlData.id;
313
+ const uploadedImageUrl = `https://cdn.leonardo.ai/users/b117a933-e5c9-45b2-96aa-4b619c2d74ba/initImages/${uploadedImageId}.${fileExtension}`;
314
+
315
+ console.log('Image uploaded successfully:', {
316
+ uploadedImageId,
317
+ uploadedImageUrl,
318
+ });
319
+
320
+ return { uploadedImageId, uploadedImageUrl };
321
+ } catch (error: any) {
322
+ console.error('Error during uploadImageByUrl:', error.message);
323
+ throw new Error('Failed to upload image by URL');
324
+ }
325
+ }
326
+
327
+ private async uploadToPresignedUrl(
328
+ presignedUrl: string,
329
+ fields: string,
330
+ file: File
331
+ ): Promise<void> {
332
+ const parsedFields = JSON.parse(fields);
333
+ const formData = new FormData();
334
+
335
+ Object.entries(parsedFields).forEach(([key, value]) => {
336
+ formData.append(key, value as string);
337
+ });
338
+ formData.append('file', file);
339
+
340
+ console.log('Uploading with FormData:', Array.from(formData.entries()));
341
+
342
+ const response = await fetch(presignedUrl, {
343
+ method: 'POST',
344
+ body: formData,
345
+ });
346
+
347
+ if (!response.ok) {
348
+ const errorDetails = await response.text();
349
+ console.error('Upload failed:', errorDetails);
350
+ throw new Error(`Image upload failed: ${response.status}`);
351
+ }
352
+
353
+ console.log('Upload to presigned URL successful');
354
+ }
197
355
  }
@@ -1 +1,2 @@
1
1
  export * from './ConceptWorkflow';
2
+ export * from './ArchetypeWorkflow';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.79",
3
+ "version": "0.0.81",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {