90dc-core 1.19.8 → 1.19.9
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ImagesClient.d.ts","sourceRoot":"","sources":["../../../src/lib/clients/ImagesClient.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,KAAK,CAAC,MAAM,yBAAyB,CAAC;AAalD,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAA6C;IAC3D,OAAO,CAAC,GAAG,CAAgB;gBAEf,MAAM,EAAE,CAAC,CAAC,MAAM;IAa5B,OAAO,CAAC,aAAa;IA4CR,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IActD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAkBnD,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI3D,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAIxD,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlG,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;IAalF,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ/D,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,YAAY,GAAG,SAAS,CAAC;IAiBxE,qB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M,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlG,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;IAalF,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ/D,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,YAAY,GAAG,SAAS,CAAC;IAiBxE,qBAAqB,CAChC,KAAK,EAAE,CAAC,CAAC,sBAAsB,GAC9B,OAAO,CAAC,CAAC,CAAC,uBAAuB,CAAC;IAkC/B,qBAAqB,CACzB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,CAAC,CAAC,uBAAuB,EAAE,CAAC;IA2CjC,0BAA0B,CAC9B,SAAS,EAAE,MAAM,EACjB,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,CAAC,CAAC,uBAAuB,EAAE,CAAC;IA2CjC,uBAAuB,CAC3B,KAAK,EAAE,CAAC,CAAC,4BAA4B,GACpC,OAAO,CAAC,CAAC,CAAC,uBAAuB,GAAG,IAAI,CAAC;IAuBtC,mBAAmB,CACvB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,OAAO,CAAC;YA4BL,0BAA0B;YAgI1B,YAAY;IAgC1B,OAAO,CAAC,kBAAkB;IA2B1B,OAAO,CAAC,cAAc;IAKtB,OAAO,CAAC,WAAW;CAOpB"}
|
|
@@ -417,48 +417,69 @@ export class ImagesClient {
|
|
|
417
417
|
});
|
|
418
418
|
continue;
|
|
419
419
|
}
|
|
420
|
-
// Download the legacy photo (encrypted in storage)
|
|
421
|
-
const [encryptedContents] = await file.download();
|
|
422
|
-
// Decrypt the legacy photo
|
|
423
|
-
let decryptedBuffer;
|
|
424
420
|
try {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
421
|
+
// Download the legacy photo (encrypted in storage)
|
|
422
|
+
this.logger.debug("Downloading legacy photo", {
|
|
423
|
+
fileName
|
|
424
|
+
});
|
|
425
|
+
const [encryptedContents] = await file.download();
|
|
426
|
+
// Decrypt the legacy photo
|
|
427
|
+
this.logger.debug("Decrypting legacy photo", {
|
|
428
|
+
fileName,
|
|
429
|
+
size: encryptedContents.length
|
|
430
|
+
});
|
|
431
|
+
const decryptedBuffer = this.decryptLegacyImage(encryptedContents);
|
|
432
|
+
this.logger.debug("Decryption successful", {
|
|
428
433
|
fileName,
|
|
429
|
-
|
|
434
|
+
decryptedSize: decryptedBuffer.length
|
|
435
|
+
});
|
|
436
|
+
// Calculate photoDate based on day offset
|
|
437
|
+
const photoDate = new Date();
|
|
438
|
+
photoDate.setDate(photoDate.getDate() - (90 - day));
|
|
439
|
+
// Generate new UUID and path
|
|
440
|
+
const newUuid = uuidv4();
|
|
441
|
+
const newFirebasePath = `progress-pictures/${userUuid}/${newUuid}.png`;
|
|
442
|
+
// Save decrypted (plain) image to new Firebase location
|
|
443
|
+
this.logger.debug("Uploading to new location", {
|
|
444
|
+
newFirebasePath
|
|
445
|
+
});
|
|
446
|
+
await this.bucket.file(newFirebasePath).save(decryptedBuffer, {
|
|
447
|
+
metadata: {
|
|
448
|
+
contentType: "image/png"
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
this.logger.debug("Upload successful", {
|
|
452
|
+
newFirebasePath
|
|
453
|
+
});
|
|
454
|
+
// Verify file exists before creating database record
|
|
455
|
+
const [exists] = await this.bucket.file(newFirebasePath).exists();
|
|
456
|
+
if (!exists) {
|
|
457
|
+
throw new Error("File was not saved to Firebase Storage");
|
|
458
|
+
}
|
|
459
|
+
// Create database record only after successful upload
|
|
460
|
+
await UserProgressPhoto.create({
|
|
461
|
+
uuid: newUuid,
|
|
462
|
+
userUuid,
|
|
463
|
+
firebasePath: newFirebasePath,
|
|
464
|
+
photoDate,
|
|
465
|
+
programId
|
|
466
|
+
});
|
|
467
|
+
migratedCount++;
|
|
468
|
+
this.logger.info("Successfully migrated legacy photo", {
|
|
469
|
+
oldPath: fileName,
|
|
470
|
+
newPath: newFirebasePath,
|
|
471
|
+
day,
|
|
472
|
+
programId
|
|
473
|
+
});
|
|
474
|
+
} catch (photoError) {
|
|
475
|
+
this.logger.error("Failed to migrate photo, skipping", {
|
|
476
|
+
fileName,
|
|
477
|
+
error: photoError instanceof Error ? photoError.message : String(photoError),
|
|
478
|
+
stack: photoError instanceof Error ? photoError.stack : undefined
|
|
430
479
|
});
|
|
431
480
|
skippedCount++;
|
|
432
481
|
continue;
|
|
433
482
|
}
|
|
434
|
-
// Calculate photoDate based on day offset (we don't have program start date, so use upload time)
|
|
435
|
-
// In reality, this would need Program model to calculate exact date
|
|
436
|
-
const photoDate = new Date(); // Fallback: use current time
|
|
437
|
-
photoDate.setDate(photoDate.getDate() - (90 - day)); // Rough estimate based on day
|
|
438
|
-
// Generate new UUID and path
|
|
439
|
-
const newUuid = uuidv4();
|
|
440
|
-
const newFirebasePath = `progress-pictures/${userUuid}/${newUuid}.png`;
|
|
441
|
-
// Save decrypted (plain) image to new Firebase location
|
|
442
|
-
await this.bucket.file(newFirebasePath).save(decryptedBuffer, {
|
|
443
|
-
metadata: {
|
|
444
|
-
contentType: "image/png"
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
migratedCount++;
|
|
448
|
-
// Create database record
|
|
449
|
-
await UserProgressPhoto.create({
|
|
450
|
-
uuid: newUuid,
|
|
451
|
-
userUuid,
|
|
452
|
-
firebasePath: newFirebasePath,
|
|
453
|
-
photoDate,
|
|
454
|
-
programId
|
|
455
|
-
});
|
|
456
|
-
this.logger.info("Migrated legacy photo", {
|
|
457
|
-
oldPath: fileName,
|
|
458
|
-
newPath: newFirebasePath,
|
|
459
|
-
day,
|
|
460
|
-
programId
|
|
461
|
-
});
|
|
462
483
|
}
|
|
463
484
|
this.logger.info("Legacy photo migration completed", {
|
|
464
485
|
userUuid,
|
|
@@ -519,9 +540,12 @@ export class ImagesClient {
|
|
|
519
540
|
decipher.update(encryptedData),
|
|
520
541
|
decipher.final()
|
|
521
542
|
]);
|
|
522
|
-
//
|
|
523
|
-
//
|
|
524
|
-
|
|
543
|
+
// Original implementation: "data:image/jpeg;base64," + decryptedImageData.toString('base64').slice(20, ...)
|
|
544
|
+
// It converts to base64, slices 20 chars (removing "dataimage/jpegbase64" garbage), then returns
|
|
545
|
+
// We need to replicate this: base64 encode, slice 20 chars, base64 decode
|
|
546
|
+
const base64String = decryptedData.toString('base64');
|
|
547
|
+
const slicedBase64 = base64String.slice(20);
|
|
548
|
+
return Buffer.from(slicedBase64, 'base64');
|
|
525
549
|
} catch (error) {
|
|
526
550
|
this.logger.error("Failed to decrypt legacy image", {
|
|
527
551
|
error
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/clients/ImagesClient.ts"],"sourcesContent":["import admin from \"firebase-admin\";\nimport { Bucket } from \"@google-cloud/storage\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { readFileSync } from \"fs\";\nimport { createDecipheriv } from \"crypto\";\nimport { ExternalAPIError } from \"../Errors/AppError.js\";\nimport { Log } from \"../utils/Logger.js\";\nimport { validateBase64Image } from \"../utils/imageValidation.js\";\nimport { UserProgressPhoto } from \"../dbmodels/user/UserProgressPhoto.js\";\nimport type * as t from \"./types/images.types.js\";\n\nconst PROGRESS_DAYS = [1, 30, 60, 90];\n\ntype RecipePhotoType = \"ingredients\" | \"cover\" | \"method\";\nconst RECIPE_PHOTO_TYPES: RecipePhotoType[] = [\"ingredients\", \"cover\", \"method\"];\n\nconst imageName = {\n avatar: (userUuid: string) => `${userUuid}--avatar.png`,\n progress: (userUuid: string, day: number, programId: string) => `progress/${userUuid}--${day}day--${programId}.png`,\n recipe: (recipeUuid: string, type: RecipePhotoType) => `${recipeUuid}--${type}.png`,\n};\n\nexport class ImagesClient {\n private bucket: Bucket;\n private logger = Log.getInstance().extend(\"images-client\");\n private app: admin.app.App;\n\n constructor(config: t.Config) {\n if (config.firebaseApp) {\n // Use provided Firebase app\n this.app = config.firebaseApp;\n } else {\n // Initialize Firebase if needed (following FirebasePushNotificationClient pattern)\n this.initializeApp(config.storageBucket);\n this.app = admin.app();\n }\n\n this.bucket = this.app.storage().bucket(config.storageBucket);\n }\n\n private initializeApp(storageBucket: string): void {\n try {\n // Check if app already exists\n if (admin.apps?.length && admin.apps.length > 0) {\n this.logger.info(\"Using existing Firebase app\");\n return;\n }\n\n // Try to load service account from environment (for local development)\n const serviceAccountPath = process.env.FIREBASE_SERVICE_ACCOUNT_KEY;\n\n if (serviceAccountPath) {\n // Local development with service account file\n const serviceAccount = JSON.parse(readFileSync(serviceAccountPath, \"utf-8\"));\n\n admin.initializeApp({\n credential: admin.credential.cert(serviceAccount),\n storageBucket,\n });\n\n this.logger.info(\"Firebase initialized with service account file\", {\n projectId: serviceAccount.project_id,\n storageBucket,\n });\n } else {\n // Production: use application default credentials\n admin.initializeApp({\n credential: admin.credential.applicationDefault(),\n storageBucket,\n });\n\n this.logger.info(\"Firebase initialized with application default credentials\", {\n storageBucket,\n });\n }\n } catch (error) {\n this.logger.error(\"Failed to initialize Firebase Admin SDK\", { error });\n throw new ExternalAPIError(\n \"Failed to initialize Firebase\",\n `Error: ${String(error)}`\n );\n }\n }\n\n public async saveImage(base64: string, name: string): Promise<void> {\n try {\n this.logger.info(\"Saving image\", { name });\n\n const buffer = this.base64ToBuffer(base64);\n const file = this.bucket.file(name);\n await file.save(buffer, { contentType: \"image/png\" });\n\n this.logger.info(\"Image saved successfully\", { name });\n } catch (error) {\n this.handleError(error, \"saveImage\");\n }\n }\n\n public async getImage(name: string): Promise<string | undefined> {\n try {\n this.logger.info(\"Getting image\", { name });\n\n const file = this.bucket.file(name);\n const [exists] = await file.exists();\n if (!exists) {\n return undefined;\n }\n\n const [contents] = await file.download();\n return \"data:image/jpeg;base64,\" + contents.toString(\"base64\");\n } catch (error) {\n this.logger.error(`Failed to get image: ${name}`, { error });\n return undefined;\n }\n }\n\n public async saveAvatar(userUuid: string, base64: string): Promise<void> {\n await this.saveImage(base64, imageName.avatar(userUuid));\n }\n\n public async getAvatar(userUuid: string): Promise<string | undefined> {\n return this.getImage(imageName.avatar(userUuid));\n }\n\n public async saveProgressPhoto(userUuid: string, day: number, programId: string, base64: string): Promise<void> {\n await this.saveImage(base64, imageName.progress(userUuid, day, programId));\n }\n\n public async getProgressPhotos(userUuid: string, programId: string): Promise<t.ProgressPhoto[]> {\n const photos: t.ProgressPhoto[] = [];\n\n for (const day of PROGRESS_DAYS) {\n const base64 = await this.getImage(imageName.progress(userUuid, day, programId));\n if (base64) {\n photos.push({ day, base64 });\n }\n }\n\n return photos;\n }\n\n public async saveRecipePhotos(input: t.SaveRecipePhotosInput): Promise<void> {\n await Promise.all([\n this.saveImage(input.ingredientBase64, imageName.recipe(input.recipeUuid, \"ingredients\")),\n this.saveImage(input.coverBase64, imageName.recipe(input.recipeUuid, \"cover\")),\n this.saveImage(input.methodBase64, imageName.recipe(input.recipeUuid, \"method\")),\n ]);\n }\n\n public async getRecipePhotos(recipeUuid: string): Promise<t.RecipePhotos | undefined> {\n const results = await Promise.all(\n RECIPE_PHOTO_TYPES.map((type) => this.getImage(imageName.recipe(recipeUuid, type)))\n );\n\n if (results.every((r) => !r)) {\n return undefined;\n }\n\n // BUG: cover/method swapped — preserved from original ImageController.ts to keep backward compat with frontend\n return {\n ingredientBase64: results[0] ?? \"\",\n methodBase64: results[1] ?? \"\",\n coverBase64: results[2] ?? \"\",\n };\n }\n\n public async saveUserProgressPhoto(\n input: t.UserProgressPhotoInput\n ): Promise<t.UserProgressPhotoRecord> {\n // Validate image\n validateBase64Image(input.base64);\n\n // Generate UUID for the photo\n const uuid = uuidv4();\n\n // Prepare Firebase path\n const firebasePath = `progress-pictures/${input.userUuid}/${uuid}.png`;\n\n // Upload to Firebase Storage\n const base64Data = input.base64.replace(/^data:image\\/\\w+;base64,/, \"\");\n const buffer = Buffer.from(base64Data, \"base64\");\n\n const file = this.bucket.file(firebasePath);\n await file.save(buffer, {\n metadata: {\n contentType: \"image/png\",\n },\n });\n\n // Save metadata to database\n const photoDate = input.photoDate || new Date();\n const record = await UserProgressPhoto.create({\n uuid,\n userUuid: input.userUuid,\n firebasePath,\n photoDate,\n programId: input.programId || null,\n });\n\n return record.toJSON() as t.UserProgressPhotoRecord;\n }\n\n async getUserProgressPhotos(\n userUuid: string\n ): Promise<t.UserProgressPhotoRecord[]> {\n const records = await UserProgressPhoto.findAll({\n where: { userUuid },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // If no photos in new system, attempt to migrate from legacy Firebase structure\n if (records.length === 0) {\n this.logger.info(\"No photos in new system, checking for legacy photos to migrate\", { userUuid });\n await this.migrateLegacyPhotosForUser(userUuid);\n\n // Re-fetch after migration\n const migratedRecords = await UserProgressPhoto.findAll({\n where: { userUuid },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // Generate signed URLs for migrated records\n const withUrls = await Promise.all(\n (migratedRecords as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n // Generate signed URLs for all records\n const withUrls = await Promise.all(\n (records as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n async getProgressPhotosByProgram(\n programId: string,\n userUuid?: string\n ): Promise<t.UserProgressPhotoRecord[]> {\n const records = await UserProgressPhoto.findAll({\n where: { programId },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // If no photos found and userUuid provided, attempt migration\n if (records.length === 0 && userUuid) {\n this.logger.info(\"No photos found for program, attempting migration\", { programId, userUuid });\n await this.migrateLegacyPhotosForUser(userUuid);\n\n // Re-fetch after migration\n const migratedRecords = await UserProgressPhoto.findAll({\n where: { programId },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // Generate signed URLs\n const withUrls = await Promise.all(\n (migratedRecords as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n // Generate signed URLs for all records\n const withUrls = await Promise.all(\n (records as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n async updateProgressPhotoDate(\n input: t.UpdateProgressPhotoDateInput\n ): Promise<t.UserProgressPhotoRecord | null> {\n const [affectedRows] = await UserProgressPhoto.update(\n { photoDate: input.photoDate },\n {\n where: {\n uuid: input.uuid,\n userUuid: input.userUuid, // Ensure user owns the photo\n },\n }\n );\n\n if (affectedRows === 0) {\n return null; // Photo not found or user doesn't own it\n }\n\n const updated = await UserProgressPhoto.findOne({\n where: { uuid: input.uuid },\n raw: true,\n });\n\n return updated as t.UserProgressPhotoRecord;\n }\n\n async deleteProgressPhoto(\n uuid: string,\n userUuid: string\n ): Promise<boolean> {\n // Find the photo to get the firebasePath\n const photo = await UserProgressPhoto.findOne({\n where: { uuid, userUuid },\n raw: true,\n });\n\n if (!photo) {\n return false; // Photo not found or user doesn't own it\n }\n\n // Delete from Firebase Storage\n const file = this.bucket.file(photo.firebasePath);\n try {\n await file.delete();\n } catch (error) {\n // If file doesn't exist in storage, continue with database deletion\n console.warn(`Failed to delete file from storage: ${photo.firebasePath}`, error);\n }\n\n // Delete from database\n await UserProgressPhoto.destroy({\n where: { uuid, userUuid },\n });\n\n return true;\n }\n\n private async migrateLegacyPhotosForUser(userUuid: string): Promise<void> {\n try {\n this.logger.info(\"Starting legacy photo migration\", { userUuid });\n\n // List all files in the legacy /progress folder with pattern: progress/{userUuid}--{day}day--{programId}.png\n let files;\n try {\n [files] = await this.bucket.getFiles({\n prefix: `progress/${userUuid}--`,\n });\n } catch (authError) {\n this.logger.error(\"Firebase authentication failed during migration, skipping\", {\n userUuid,\n error: authError instanceof Error ? authError.message : String(authError),\n });\n return; // Skip migration if auth fails\n }\n\n if (files.length === 0) {\n this.logger.info(\"No legacy photos found for user\", { userUuid });\n return;\n }\n\n let migratedCount = 0;\n let skippedCount = 0;\n\n for (const file of files) {\n const fileName = file.name;\n\n // Parse legacy filename: progress/{userUuid}--{day}day--{programId}.png\n const match = fileName.match(/^progress\\/(.+)--(\\d+)day--(.+)\\.png$/);\n if (!match) {\n this.logger.warn(\"Skipping file with unexpected name format\", { fileName });\n continue;\n }\n\n const [, fileUserUuid, dayStr, programId] = match;\n const day = parseInt(dayStr, 10);\n\n // Verify this file belongs to the user\n if (fileUserUuid !== userUuid) {\n this.logger.warn(\"File doesn't belong to this user, skipping\", { fileName, userUuid, fileUserUuid });\n continue;\n }\n\n // Check if this photo was already migrated (by programId + userUuid + approximate day)\n const existing = await UserProgressPhoto.findOne({\n where: { userUuid, programId },\n raw: true,\n });\n\n if (existing) {\n this.logger.debug(\"Photo already migrated, skipping\", { fileName });\n continue;\n }\n\n // Download the legacy photo (encrypted in storage)\n const [encryptedContents] = await file.download();\n\n // Decrypt the legacy photo\n let decryptedBuffer: Buffer;\n try {\n decryptedBuffer = this.decryptLegacyImage(encryptedContents);\n } catch (decryptError) {\n this.logger.error(\"Failed to decrypt legacy photo, skipping\", { fileName, decryptError });\n skippedCount++;\n continue;\n }\n\n // Calculate photoDate based on day offset (we don't have program start date, so use upload time)\n // In reality, this would need Program model to calculate exact date\n const photoDate = new Date(); // Fallback: use current time\n photoDate.setDate(photoDate.getDate() - (90 - day)); // Rough estimate based on day\n\n // Generate new UUID and path\n const newUuid = uuidv4();\n const newFirebasePath = `progress-pictures/${userUuid}/${newUuid}.png`;\n\n // Save decrypted (plain) image to new Firebase location\n await this.bucket.file(newFirebasePath).save(decryptedBuffer, {\n metadata: {\n contentType: \"image/png\",\n },\n });\n\n migratedCount++;\n\n // Create database record\n await UserProgressPhoto.create({\n uuid: newUuid,\n userUuid,\n firebasePath: newFirebasePath,\n photoDate,\n programId,\n });\n\n this.logger.info(\"Migrated legacy photo\", {\n oldPath: fileName,\n newPath: newFirebasePath,\n day,\n programId,\n });\n }\n\n this.logger.info(\"Legacy photo migration completed\", {\n userUuid,\n migrated: migratedCount,\n skipped: skippedCount,\n });\n } catch (error) {\n this.logger.error(\"Failed to migrate legacy photos\", { userUuid, error });\n // Don't throw - migration failure shouldn't break the request\n }\n }\n\n private async getSignedUrl(firebasePath: string): Promise<string | undefined> {\n try {\n const file = this.bucket.file(firebasePath);\n\n // Check if file exists first\n const [exists] = await file.exists();\n if (!exists) {\n this.logger.warn(\"File not found for signed URL generation\", { firebasePath });\n return undefined;\n }\n\n // Generate signed URL with proper expiration date\n const expirationDate = new Date();\n expirationDate.setDate(expirationDate.getDate() + 7); // 7 days from now\n\n const [url] = await file.getSignedUrl({\n action: \"read\",\n expires: expirationDate,\n });\n\n this.logger.debug(\"Generated signed URL\", { firebasePath });\n return url;\n } catch (error) {\n this.logger.error(\"Failed to generate signed URL\", {\n firebasePath,\n error: error instanceof Error ? error.message : String(error),\n errorDetails: error\n });\n return undefined;\n }\n }\n\n private decryptLegacyImage(encryptedBuffer: Buffer): Buffer {\n try {\n const encryptionKey = process.env.ENCRYPTION_KEY;\n if (!encryptionKey) {\n throw new Error(\"ENCRYPTION_KEY not found in environment\");\n }\n\n // Extract IV from first 16 bytes\n const iv = encryptedBuffer.slice(0, 16);\n const encryptedData = encryptedBuffer.slice(16);\n\n // Decrypt using AES-256-CBC\n const decipher = createDecipheriv(\"aes-256-cbc\", Buffer.from(encryptionKey), iv);\n const decryptedData = Buffer.concat([decipher.update(encryptedData), decipher.final()]);\n\n // Old implementation adds extra prefix, remove it (20 bytes)\n // Original: \"data:image/jpeg;base64,\" + decryptedImageData.toString('base64').slice(20, ...)\n return decryptedData.slice(20);\n } catch (error) {\n this.logger.error(\"Failed to decrypt legacy image\", { error });\n throw new Error(\"Image decryption failed\");\n }\n }\n\n private base64ToBuffer(base64: string): Buffer {\n const data = base64.includes(\",\") ? base64.split(\",\")[1]! : base64;\n return Buffer.from(data, \"base64\");\n }\n\n private handleError(error: unknown, method: string): never {\n this.logger.error(`Firebase Storage error in ${method}`, { error });\n throw new ExternalAPIError(\n `Firebase Storage error: ${error instanceof Error ? error.message : String(error)}`,\n `Service: Firebase Storage, Method: ${method}`\n );\n }\n}\n"],"names":["admin","v4","uuidv4","readFileSync","createDecipheriv","ExternalAPIError","Log","validateBase64Image","UserProgressPhoto","PROGRESS_DAYS","RECIPE_PHOTO_TYPES","imageName","avatar","userUuid","progress","day","programId","recipe","recipeUuid","type","ImagesClient","bucket","logger","getInstance","extend","app","config","firebaseApp","initializeApp","storageBucket","storage","apps","length","info","serviceAccountPath","process","env","FIREBASE_SERVICE_ACCOUNT_KEY","serviceAccount","JSON","parse","credential","cert","projectId","project_id","applicationDefault","error","String","saveImage","base64","name","buffer","base64ToBuffer","file","save","contentType","handleError","getImage","exists","undefined","contents","download","toString","saveAvatar","getAvatar","saveProgressPhoto","getProgressPhotos","photos","push","saveRecipePhotos","input","Promise","all","ingredientBase64","coverBase64","methodBase64","getRecipePhotos","results","map","every","r","saveUserProgressPhoto","uuid","firebasePath","base64Data","replace","Buffer","from","metadata","photoDate","Date","record","create","toJSON","getUserProgressPhotos","records","findAll","where","order","raw","attributes","migrateLegacyPhotosForUser","migratedRecords","withUrls","url","getSignedUrl","getProgressPhotosByProgram","updateProgressPhotoDate","affectedRows","update","updated","findOne","deleteProgressPhoto","photo","delete","console","warn","destroy","files","getFiles","prefix","authError","Error","message","migratedCount","skippedCount","fileName","match","fileUserUuid","dayStr","parseInt","existing","debug","encryptedContents","decryptedBuffer","decryptLegacyImage","decryptError","setDate","getDate","newUuid","newFirebasePath","oldPath","newPath","migrated","skipped","expirationDate","action","expires","errorDetails","encryptedBuffer","encryptionKey","ENCRYPTION_KEY","iv","slice","encryptedData","decipher","decryptedData","concat","final","data","includes","split","method"],"mappings":"AAAA,OAAOA,WAAW,iBAAiB;AAEnC,SAASC,MAAMC,MAAM,QAAQ,OAAO;AACpC,SAASC,YAAY,QAAQ,KAAK;AAClC,SAASC,gBAAgB,QAAQ,SAAS;AAC1C,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,SAASC,GAAG,QAAQ,qBAAqB;AACzC,SAASC,mBAAmB,QAAQ,8BAA8B;AAClE,SAASC,iBAAiB,QAAQ,wCAAwC;AAG1E,MAAMC,gBAAgB;IAAC;IAAG;IAAI;IAAI;CAAG;AAGrC,MAAMC,qBAAwC;IAAC;IAAe;IAAS;CAAS;AAEhF,MAAMC,YAAY;IAChBC,QAAQ,CAACC,WAAqB,GAAGA,SAAS,YAAY,CAAC;IACvDC,UAAU,CAACD,UAAkBE,KAAaC,YAAsB,CAAC,SAAS,EAAEH,SAAS,EAAE,EAAEE,IAAI,KAAK,EAAEC,UAAU,IAAI,CAAC;IACnHC,QAAQ,CAACC,YAAoBC,OAA0B,GAAGD,WAAW,EAAE,EAAEC,KAAK,IAAI,CAAC;AACrF;AAEA,OAAO,MAAMC;IACHC,OAAe;IACfC,SAAShB,IAAIiB,WAAW,GAAGC,MAAM,CAAC,iBAAiB;IACnDC,IAAmB;IAE3B,YAAYC,MAAgB,CAAE;QAC5B,IAAIA,OAAOC,WAAW,EAAE;YACtB,4BAA4B;YAC5B,IAAI,CAACF,GAAG,GAAGC,OAAOC,WAAW;QAC/B,OAAO;YACL,mFAAmF;YACnF,IAAI,CAACC,aAAa,CAACF,OAAOG,aAAa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AAMC,gBAAgBL,gBAAgBI,KAAK,CAAC;YAE5C,4BAA4B;YAC5B,MAAME,WAAWvJ,iBAAiB,eAAekF,OAAOC,IAAI,CAAC+D,gBAAgBE;YAC7E,MAAMI,gBAAgBtE,OAAOuE,MAAM,CAAC;gBAACF,SAAS9C,MAAM,CAAC6C;gBAAgBC,SAASG,KAAK;aAAG;YAEtF,6DAA6D;YAC7D,6FAA6F;YAC7F,OAAOF,cAAcH,KAAK,CAAC;QAC7B,EAAE,OAAO3G,OAAO;YACd,IAAI,CAACxB,MAAM,CAACwB,KAAK,CAAC,kCAAkC;gBAAEA;YAAM;YAC5D,MAAM,IAAI4E,MAAM;QAClB;IACF;IAEQtE,eAAeH,MAAc,EAAU;QAC7C,MAAM8G,OAAO9G,OAAO+G,QAAQ,CAAC,OAAO/G,OAAOgH,KAAK,CAAC,IAAI,CAAC,EAAE,GAAIhH;QAC5D,OAAOqC,OAAOC,IAAI,CAACwE,MAAM;IAC3B;IAEQvG,YAAYV,KAAc,EAAEoH,MAAc,EAAS;QACzD,IAAI,CAAC5I,MAAM,CAACwB,KAAK,CAAC,CAAC,0BAA0B,EAAEoH,QAAQ,EAAE;YAAEpH;QAAM;QACjE,MAAM,IAAIzC,iBACR,CAAC,wBAAwB,EAAEyC,iBAAiB4E,QAAQ5E,MAAM6E,OAAO,GAAG5E,OAAOD,QAAQ,EACnF,CAAC,mCAAmC,EAAEoH,QAAQ;IAElD;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/clients/ImagesClient.ts"],"sourcesContent":["import admin from \"firebase-admin\";\nimport { Bucket } from \"@google-cloud/storage\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { readFileSync } from \"fs\";\nimport { createDecipheriv } from \"crypto\";\nimport { ExternalAPIError } from \"../Errors/AppError.js\";\nimport { Log } from \"../utils/Logger.js\";\nimport { validateBase64Image } from \"../utils/imageValidation.js\";\nimport { UserProgressPhoto } from \"../dbmodels/user/UserProgressPhoto.js\";\nimport type * as t from \"./types/images.types.js\";\n\nconst PROGRESS_DAYS = [1, 30, 60, 90];\n\ntype RecipePhotoType = \"ingredients\" | \"cover\" | \"method\";\nconst RECIPE_PHOTO_TYPES: RecipePhotoType[] = [\"ingredients\", \"cover\", \"method\"];\n\nconst imageName = {\n avatar: (userUuid: string) => `${userUuid}--avatar.png`,\n progress: (userUuid: string, day: number, programId: string) => `progress/${userUuid}--${day}day--${programId}.png`,\n recipe: (recipeUuid: string, type: RecipePhotoType) => `${recipeUuid}--${type}.png`,\n};\n\nexport class ImagesClient {\n private bucket: Bucket;\n private logger = Log.getInstance().extend(\"images-client\");\n private app: admin.app.App;\n\n constructor(config: t.Config) {\n if (config.firebaseApp) {\n // Use provided Firebase app\n this.app = config.firebaseApp;\n } else {\n // Initialize Firebase if needed (following FirebasePushNotificationClient pattern)\n this.initializeApp(config.storageBucket);\n this.app = admin.app();\n }\n\n this.bucket = this.app.storage().bucket(config.storageBucket);\n }\n\n private initializeApp(storageBucket: string): void {\n try {\n // Check if app already exists\n if (admin.apps?.length && admin.apps.length > 0) {\n this.logger.info(\"Using existing Firebase app\");\n return;\n }\n\n // Try to load service account from environment (for local development)\n const serviceAccountPath = process.env.FIREBASE_SERVICE_ACCOUNT_KEY;\n\n if (serviceAccountPath) {\n // Local development with service account file\n const serviceAccount = JSON.parse(readFileSync(serviceAccountPath, \"utf-8\"));\n\n admin.initializeApp({\n credential: admin.credential.cert(serviceAccount),\n storageBucket,\n });\n\n this.logger.info(\"Firebase initialized with service account file\", {\n projectId: serviceAccount.project_id,\n storageBucket,\n });\n } else {\n // Production: use application default credentials\n admin.initializeApp({\n credential: admin.credential.applicationDefault(),\n storageBucket,\n });\n\n this.logger.info(\"Firebase initialized with application default credentials\", {\n storageBucket,\n });\n }\n } catch (error) {\n this.logger.error(\"Failed to initialize Firebase Admin SDK\", { error });\n throw new ExternalAPIError(\n \"Failed to initialize Firebase\",\n `Error: ${String(error)}`\n );\n }\n }\n\n public async saveImage(base64: string, name: string): Promise<void> {\n try {\n this.logger.info(\"Saving image\", { name });\n\n const buffer = this.base64ToBuffer(base64);\n const file = this.bucket.file(name);\n await file.save(buffer, { contentType: \"image/png\" });\n\n this.logger.info(\"Image saved successfully\", { name });\n } catch (error) {\n this.handleError(error, \"saveImage\");\n }\n }\n\n public async getImage(name: string): Promise<string | undefined> {\n try {\n this.logger.info(\"Getting image\", { name });\n\n const file = this.bucket.file(name);\n const [exists] = await file.exists();\n if (!exists) {\n return undefined;\n }\n\n const [contents] = await file.download();\n return \"data:image/jpeg;base64,\" + contents.toString(\"base64\");\n } catch (error) {\n this.logger.error(`Failed to get image: ${name}`, { error });\n return undefined;\n }\n }\n\n public async saveAvatar(userUuid: string, base64: string): Promise<void> {\n await this.saveImage(base64, imageName.avatar(userUuid));\n }\n\n public async getAvatar(userUuid: string): Promise<string | undefined> {\n return this.getImage(imageName.avatar(userUuid));\n }\n\n public async saveProgressPhoto(userUuid: string, day: number, programId: string, base64: string): Promise<void> {\n await this.saveImage(base64, imageName.progress(userUuid, day, programId));\n }\n\n public async getProgressPhotos(userUuid: string, programId: string): Promise<t.ProgressPhoto[]> {\n const photos: t.ProgressPhoto[] = [];\n\n for (const day of PROGRESS_DAYS) {\n const base64 = await this.getImage(imageName.progress(userUuid, day, programId));\n if (base64) {\n photos.push({ day, base64 });\n }\n }\n\n return photos;\n }\n\n public async saveRecipePhotos(input: t.SaveRecipePhotosInput): Promise<void> {\n await Promise.all([\n this.saveImage(input.ingredientBase64, imageName.recipe(input.recipeUuid, \"ingredients\")),\n this.saveImage(input.coverBase64, imageName.recipe(input.recipeUuid, \"cover\")),\n this.saveImage(input.methodBase64, imageName.recipe(input.recipeUuid, \"method\")),\n ]);\n }\n\n public async getRecipePhotos(recipeUuid: string): Promise<t.RecipePhotos | undefined> {\n const results = await Promise.all(\n RECIPE_PHOTO_TYPES.map((type) => this.getImage(imageName.recipe(recipeUuid, type)))\n );\n\n if (results.every((r) => !r)) {\n return undefined;\n }\n\n // BUG: cover/method swapped — preserved from original ImageController.ts to keep backward compat with frontend\n return {\n ingredientBase64: results[0] ?? \"\",\n methodBase64: results[1] ?? \"\",\n coverBase64: results[2] ?? \"\",\n };\n }\n\n public async saveUserProgressPhoto(\n input: t.UserProgressPhotoInput\n ): Promise<t.UserProgressPhotoRecord> {\n // Validate image\n validateBase64Image(input.base64);\n\n // Generate UUID for the photo\n const uuid = uuidv4();\n\n // Prepare Firebase path\n const firebasePath = `progress-pictures/${input.userUuid}/${uuid}.png`;\n\n // Upload to Firebase Storage\n const base64Data = input.base64.replace(/^data:image\\/\\w+;base64,/, \"\");\n const buffer = Buffer.from(base64Data, \"base64\");\n\n const file = this.bucket.file(firebasePath);\n await file.save(buffer, {\n metadata: {\n contentType: \"image/png\",\n },\n });\n\n // Save metadata to database\n const photoDate = input.photoDate || new Date();\n const record = await UserProgressPhoto.create({\n uuid,\n userUuid: input.userUuid,\n firebasePath,\n photoDate,\n programId: input.programId || null,\n });\n\n return record.toJSON() as t.UserProgressPhotoRecord;\n }\n\n async getUserProgressPhotos(\n userUuid: string\n ): Promise<t.UserProgressPhotoRecord[]> {\n const records = await UserProgressPhoto.findAll({\n where: { userUuid },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // If no photos in new system, attempt to migrate from legacy Firebase structure\n if (records.length === 0) {\n this.logger.info(\"No photos in new system, checking for legacy photos to migrate\", { userUuid });\n await this.migrateLegacyPhotosForUser(userUuid);\n\n // Re-fetch after migration\n const migratedRecords = await UserProgressPhoto.findAll({\n where: { userUuid },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // Generate signed URLs for migrated records\n const withUrls = await Promise.all(\n (migratedRecords as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n // Generate signed URLs for all records\n const withUrls = await Promise.all(\n (records as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n async getProgressPhotosByProgram(\n programId: string,\n userUuid?: string\n ): Promise<t.UserProgressPhotoRecord[]> {\n const records = await UserProgressPhoto.findAll({\n where: { programId },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // If no photos found and userUuid provided, attempt migration\n if (records.length === 0 && userUuid) {\n this.logger.info(\"No photos found for program, attempting migration\", { programId, userUuid });\n await this.migrateLegacyPhotosForUser(userUuid);\n\n // Re-fetch after migration\n const migratedRecords = await UserProgressPhoto.findAll({\n where: { programId },\n order: [[\"photoDate\", \"DESC\"]],\n raw: true,\n attributes: ['uuid', 'userUuid', 'firebasePath', 'photoDate', 'programId', 'createdAt', 'updatedAt'],\n });\n\n // Generate signed URLs\n const withUrls = await Promise.all(\n (migratedRecords as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n // Generate signed URLs for all records\n const withUrls = await Promise.all(\n (records as t.UserProgressPhotoRecord[]).map(async (record) => ({\n ...record,\n url: await this.getSignedUrl(record.firebasePath),\n }))\n );\n\n return withUrls;\n }\n\n async updateProgressPhotoDate(\n input: t.UpdateProgressPhotoDateInput\n ): Promise<t.UserProgressPhotoRecord | null> {\n const [affectedRows] = await UserProgressPhoto.update(\n { photoDate: input.photoDate },\n {\n where: {\n uuid: input.uuid,\n userUuid: input.userUuid, // Ensure user owns the photo\n },\n }\n );\n\n if (affectedRows === 0) {\n return null; // Photo not found or user doesn't own it\n }\n\n const updated = await UserProgressPhoto.findOne({\n where: { uuid: input.uuid },\n raw: true,\n });\n\n return updated as t.UserProgressPhotoRecord;\n }\n\n async deleteProgressPhoto(\n uuid: string,\n userUuid: string\n ): Promise<boolean> {\n // Find the photo to get the firebasePath\n const photo = await UserProgressPhoto.findOne({\n where: { uuid, userUuid },\n raw: true,\n });\n\n if (!photo) {\n return false; // Photo not found or user doesn't own it\n }\n\n // Delete from Firebase Storage\n const file = this.bucket.file(photo.firebasePath);\n try {\n await file.delete();\n } catch (error) {\n // If file doesn't exist in storage, continue with database deletion\n console.warn(`Failed to delete file from storage: ${photo.firebasePath}`, error);\n }\n\n // Delete from database\n await UserProgressPhoto.destroy({\n where: { uuid, userUuid },\n });\n\n return true;\n }\n\n private async migrateLegacyPhotosForUser(userUuid: string): Promise<void> {\n try {\n this.logger.info(\"Starting legacy photo migration\", { userUuid });\n\n // List all files in the legacy /progress folder with pattern: progress/{userUuid}--{day}day--{programId}.png\n let files;\n try {\n [files] = await this.bucket.getFiles({\n prefix: `progress/${userUuid}--`,\n });\n } catch (authError) {\n this.logger.error(\"Firebase authentication failed during migration, skipping\", {\n userUuid,\n error: authError instanceof Error ? authError.message : String(authError),\n });\n return; // Skip migration if auth fails\n }\n\n if (files.length === 0) {\n this.logger.info(\"No legacy photos found for user\", { userUuid });\n return;\n }\n\n let migratedCount = 0;\n let skippedCount = 0;\n\n for (const file of files) {\n const fileName = file.name;\n\n // Parse legacy filename: progress/{userUuid}--{day}day--{programId}.png\n const match = fileName.match(/^progress\\/(.+)--(\\d+)day--(.+)\\.png$/);\n if (!match) {\n this.logger.warn(\"Skipping file with unexpected name format\", { fileName });\n continue;\n }\n\n const [, fileUserUuid, dayStr, programId] = match;\n const day = parseInt(dayStr, 10);\n\n // Verify this file belongs to the user\n if (fileUserUuid !== userUuid) {\n this.logger.warn(\"File doesn't belong to this user, skipping\", { fileName, userUuid, fileUserUuid });\n continue;\n }\n\n // Check if this photo was already migrated (by programId + userUuid + approximate day)\n const existing = await UserProgressPhoto.findOne({\n where: { userUuid, programId },\n raw: true,\n });\n\n if (existing) {\n this.logger.debug(\"Photo already migrated, skipping\", { fileName });\n continue;\n }\n\n try {\n // Download the legacy photo (encrypted in storage)\n this.logger.debug(\"Downloading legacy photo\", { fileName });\n const [encryptedContents] = await file.download();\n\n // Decrypt the legacy photo\n this.logger.debug(\"Decrypting legacy photo\", { fileName, size: encryptedContents.length });\n const decryptedBuffer = this.decryptLegacyImage(encryptedContents);\n this.logger.debug(\"Decryption successful\", { fileName, decryptedSize: decryptedBuffer.length });\n\n // Calculate photoDate based on day offset\n const photoDate = new Date();\n photoDate.setDate(photoDate.getDate() - (90 - day));\n\n // Generate new UUID and path\n const newUuid = uuidv4();\n const newFirebasePath = `progress-pictures/${userUuid}/${newUuid}.png`;\n\n // Save decrypted (plain) image to new Firebase location\n this.logger.debug(\"Uploading to new location\", { newFirebasePath });\n await this.bucket.file(newFirebasePath).save(decryptedBuffer, {\n metadata: {\n contentType: \"image/png\",\n },\n });\n this.logger.debug(\"Upload successful\", { newFirebasePath });\n\n // Verify file exists before creating database record\n const [exists] = await this.bucket.file(newFirebasePath).exists();\n if (!exists) {\n throw new Error(\"File was not saved to Firebase Storage\");\n }\n\n // Create database record only after successful upload\n await UserProgressPhoto.create({\n uuid: newUuid,\n userUuid,\n firebasePath: newFirebasePath,\n photoDate,\n programId,\n });\n\n migratedCount++;\n\n this.logger.info(\"Successfully migrated legacy photo\", {\n oldPath: fileName,\n newPath: newFirebasePath,\n day,\n programId,\n });\n } catch (photoError) {\n this.logger.error(\"Failed to migrate photo, skipping\", {\n fileName,\n error: photoError instanceof Error ? photoError.message : String(photoError),\n stack: photoError instanceof Error ? photoError.stack : undefined,\n });\n skippedCount++;\n continue;\n }\n }\n\n this.logger.info(\"Legacy photo migration completed\", {\n userUuid,\n migrated: migratedCount,\n skipped: skippedCount,\n });\n } catch (error) {\n this.logger.error(\"Failed to migrate legacy photos\", { userUuid, error });\n // Don't throw - migration failure shouldn't break the request\n }\n }\n\n private async getSignedUrl(firebasePath: string): Promise<string | undefined> {\n try {\n const file = this.bucket.file(firebasePath);\n\n // Check if file exists first\n const [exists] = await file.exists();\n if (!exists) {\n this.logger.warn(\"File not found for signed URL generation\", { firebasePath });\n return undefined;\n }\n\n // Generate signed URL with proper expiration date\n const expirationDate = new Date();\n expirationDate.setDate(expirationDate.getDate() + 7); // 7 days from now\n\n const [url] = await file.getSignedUrl({\n action: \"read\",\n expires: expirationDate,\n });\n\n this.logger.debug(\"Generated signed URL\", { firebasePath });\n return url;\n } catch (error) {\n this.logger.error(\"Failed to generate signed URL\", {\n firebasePath,\n error: error instanceof Error ? error.message : String(error),\n errorDetails: error\n });\n return undefined;\n }\n }\n\n private decryptLegacyImage(encryptedBuffer: Buffer): Buffer {\n try {\n const encryptionKey = process.env.ENCRYPTION_KEY;\n if (!encryptionKey) {\n throw new Error(\"ENCRYPTION_KEY not found in environment\");\n }\n\n // Extract IV from first 16 bytes\n const iv = encryptedBuffer.slice(0, 16);\n const encryptedData = encryptedBuffer.slice(16);\n\n // Decrypt using AES-256-CBC\n const decipher = createDecipheriv(\"aes-256-cbc\", Buffer.from(encryptionKey), iv);\n const decryptedData = Buffer.concat([decipher.update(encryptedData), decipher.final()]);\n\n // Original implementation: \"data:image/jpeg;base64,\" + decryptedImageData.toString('base64').slice(20, ...)\n // It converts to base64, slices 20 chars (removing \"dataimage/jpegbase64\" garbage), then returns\n // We need to replicate this: base64 encode, slice 20 chars, base64 decode\n const base64String = decryptedData.toString('base64');\n const slicedBase64 = base64String.slice(20);\n return Buffer.from(slicedBase64, 'base64');\n } catch (error) {\n this.logger.error(\"Failed to decrypt legacy image\", { error });\n throw new Error(\"Image decryption failed\");\n }\n }\n\n private base64ToBuffer(base64: string): Buffer {\n const data = base64.includes(\",\") ? base64.split(\",\")[1]! : base64;\n return Buffer.from(data, \"base64\");\n }\n\n private handleError(error: unknown, method: string): never {\n this.logger.error(`Firebase Storage error in ${method}`, { error });\n throw new ExternalAPIError(\n `Firebase Storage error: ${error instanceof Error ? error.message : String(error)}`,\n `Service: Firebase Storage, Method: ${method}`\n );\n }\n}\n"],"names":["admin","v4","uuidv4","readFileSync","createDecipheriv","ExternalAPIError","Log","validateBase64Image","UserProgressPhoto","PROGRESS_DAYS","RECIPE_PHOTO_TYPES","imageName","avatar","userUuid","progress","day","programId","recipe","recipeUuid","type","ImagesClient","bucket","logger","getInstance","extend","app","config","firebaseApp","initializeApp","storageBucket","storage","apps","length","info","serviceAccountPath","process","env","FIREBASE_SERVICE_ACCOUNT_KEY","serviceAccount","JSON","parse","credential","cert","projectId","project_id","applicationDefault","error","String","saveImage","base64","name","buffer","base64ToBuffer","file","save","contentType","handleError","getImage","exists","undefined","contents","download","toString","saveAvatar","getAvatar","saveProgressPhoto","getProgressPhotos","photos","push","saveRecipePhotos","input","Promise","all","ingredientBase64","coverBase64","methodBase64","getRecipePhotos","results","map","every","r","saveUserProgressPhoto","uuid","firebasePath","base64Data","replace","Buffer","from","metadata","photoDate","Date","record","create","toJSON","getUserProgressPhotos","records","findAll","where","order","raw","attributes","migrateLegacyPhotosForUser","migratedRecords","withUrls","url","getSignedUrl","getProgressPhotosByProgram","updateProgressPhotoDate","affectedRows","update","updated","findOne","deleteProgressPhoto","photo","delete","console","warn","destroy","files","getFiles","prefix","authError","Error","message","migratedCount","skippedCount","fileName","match","fileUserUuid","dayStr","parseInt","existing","debug","encryptedContents","size","decryptedBuffer","decryptLegacyImage","decryptedSize","setDate","getDate","newUuid","newFirebasePath","oldPath","newPath","photoError","stack","migrated","skipped","expirationDate","action","expires","errorDetails","encryptedBuffer","encryptionKey","ENCRYPTION_KEY","iv","slice","encryptedData","decipher","decryptedData","concat","final","base64String","slicedBase64","data","includes","split","method"],"mappings":"AAAA,OAAOA,WAAW,iBAAiB;AAEnC,SAASC,MAAMC,MAAM,QAAQ,OAAO;AACpC,SAASC,YAAY,QAAQ,KAAK;AAClC,SAASC,gBAAgB,QAAQ,SAAS;AAC1C,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,SAASC,GAAG,QAAQ,qBAAq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