image processing lambda
This commit is contained in:
95
backend/migrations/20260105161056-create-image-metadata.js
Normal file
95
backend/migrations/20260105161056-create-image-metadata.js
Normal file
@@ -0,0 +1,95 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.createTable("ImageMetadata", {
|
||||
id: {
|
||||
type: Sequelize.UUID,
|
||||
defaultValue: Sequelize.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
s3Key: {
|
||||
type: Sequelize.TEXT,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
latitude: {
|
||||
type: Sequelize.DECIMAL(10, 8),
|
||||
allowNull: true,
|
||||
},
|
||||
longitude: {
|
||||
type: Sequelize.DECIMAL(11, 8),
|
||||
allowNull: true,
|
||||
},
|
||||
cameraMake: {
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: true,
|
||||
},
|
||||
cameraModel: {
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: true,
|
||||
},
|
||||
cameraSoftware: {
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: true,
|
||||
},
|
||||
dateTaken: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
width: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
height: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
orientation: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
fileSize: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
processingStatus: {
|
||||
type: Sequelize.ENUM("pending", "processing", "completed", "failed"),
|
||||
allowNull: false,
|
||||
defaultValue: "pending",
|
||||
},
|
||||
processedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
errorMessage: {
|
||||
type: Sequelize.TEXT,
|
||||
allowNull: true,
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
|
||||
// Add indexes
|
||||
await queryInterface.addIndex("ImageMetadata", ["s3Key"], {
|
||||
unique: true,
|
||||
name: "image_metadata_s3_key_unique",
|
||||
});
|
||||
await queryInterface.addIndex("ImageMetadata", ["latitude", "longitude"], {
|
||||
name: "image_metadata_geo",
|
||||
});
|
||||
await queryInterface.addIndex("ImageMetadata", ["processingStatus"], {
|
||||
name: "image_metadata_processing_status",
|
||||
});
|
||||
},
|
||||
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.dropTable("ImageMetadata");
|
||||
},
|
||||
};
|
||||
88
backend/models/ImageMetadata.js
Normal file
88
backend/models/ImageMetadata.js
Normal file
@@ -0,0 +1,88 @@
|
||||
const { DataTypes } = require("sequelize");
|
||||
const sequelize = require("../config/database");
|
||||
|
||||
const ImageMetadata = sequelize.define(
|
||||
"ImageMetadata",
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
s3Key: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
latitude: {
|
||||
type: DataTypes.DECIMAL(10, 8),
|
||||
allowNull: true,
|
||||
},
|
||||
longitude: {
|
||||
type: DataTypes.DECIMAL(11, 8),
|
||||
allowNull: true,
|
||||
},
|
||||
cameraMake: {
|
||||
type: DataTypes.STRING(100),
|
||||
allowNull: true,
|
||||
},
|
||||
cameraModel: {
|
||||
type: DataTypes.STRING(100),
|
||||
allowNull: true,
|
||||
},
|
||||
cameraSoftware: {
|
||||
type: DataTypes.STRING(100),
|
||||
allowNull: true,
|
||||
},
|
||||
dateTaken: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
width: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
height: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
orientation: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
fileSize: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
processingStatus: {
|
||||
type: DataTypes.ENUM("pending", "processing", "completed", "failed"),
|
||||
allowNull: false,
|
||||
defaultValue: "pending",
|
||||
},
|
||||
processedAt: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
errorMessage: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{
|
||||
fields: ["s3Key"],
|
||||
unique: true,
|
||||
},
|
||||
{
|
||||
fields: ["latitude", "longitude"],
|
||||
},
|
||||
{
|
||||
fields: ["processingStatus"],
|
||||
},
|
||||
],
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = ImageMetadata;
|
||||
@@ -10,6 +10,7 @@ const UserAddress = require("./UserAddress");
|
||||
const ConditionCheck = require("./ConditionCheck");
|
||||
const AlphaInvitation = require("./AlphaInvitation");
|
||||
const Feedback = require("./Feedback");
|
||||
const ImageMetadata = require("./ImageMetadata");
|
||||
|
||||
User.hasMany(Item, { as: "ownedItems", foreignKey: "ownerId" });
|
||||
Item.belongsTo(User, { as: "owner", foreignKey: "ownerId" });
|
||||
@@ -91,4 +92,5 @@ module.exports = {
|
||||
ConditionCheck,
|
||||
AlphaInvitation,
|
||||
Feedback,
|
||||
ImageMetadata,
|
||||
};
|
||||
|
||||
@@ -106,6 +106,15 @@ class S3Service {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if image processing (metadata stripping) is enabled
|
||||
* When enabled, uploads go to staging/ prefix and Lambda processes them
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isImageProcessingEnabled() {
|
||||
return process.env.IMAGE_PROCESSING_ENABLED === "true";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a presigned URL for uploading a file directly to S3
|
||||
* @param {string} uploadType - Type of upload (profile, item, message, forum, condition-check)
|
||||
@@ -113,7 +122,7 @@ class S3Service {
|
||||
* @param {string} fileName - Original filename (used for extension)
|
||||
* @param {number} fileSize - File size in bytes (required for size enforcement)
|
||||
* @param {string} [baseKey] - Optional base key (UUID) for coordinated variant uploads
|
||||
* @returns {Promise<{uploadUrl: string, key: string, publicUrl: string, expiresAt: Date}>}
|
||||
* @returns {Promise<{uploadUrl: string, key: string, stagingKey: string|null, publicUrl: string, expiresAt: Date}>}
|
||||
*/
|
||||
async getPresignedUploadUrl(uploadType, contentType, fileName, fileSize, baseKey = null) {
|
||||
if (!this.enabled) {
|
||||
@@ -150,12 +159,19 @@ class S3Service {
|
||||
|
||||
// Use provided baseKey or generate new UUID
|
||||
const uuid = baseKey || uuidv4();
|
||||
const key = `${config.folder}/${uuid}${suffix}${ext}`;
|
||||
|
||||
// Final key is where the processed image will be (what frontend stores in DB)
|
||||
const finalKey = `${config.folder}/${uuid}${suffix}${ext}`;
|
||||
|
||||
// When image processing is enabled, upload to staging/ prefix
|
||||
// Lambda will process and move to final location
|
||||
const useStaging = this.isImageProcessingEnabled();
|
||||
const uploadKey = useStaging ? `staging/${finalKey}` : finalKey;
|
||||
|
||||
const cacheDirective = config.public ? "public" : "private";
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: key,
|
||||
Key: uploadKey,
|
||||
ContentType: contentType,
|
||||
ContentLength: fileSize, // Enforce exact file size
|
||||
CacheControl: `${cacheDirective}, max-age=${config.cacheMaxAge}`,
|
||||
@@ -167,9 +183,10 @@ class S3Service {
|
||||
|
||||
return {
|
||||
uploadUrl,
|
||||
key,
|
||||
key: finalKey, // Frontend stores this in database
|
||||
stagingKey: useStaging ? uploadKey : null, // Actual upload location (if staging enabled)
|
||||
publicUrl: config.public
|
||||
? `https://${this.bucket}.s3.${this.region}.amazonaws.com/${key}`
|
||||
? `https://${this.bucket}.s3.${this.region}.amazonaws.com/${finalKey}`
|
||||
: null,
|
||||
expiresAt: new Date(Date.now() + PRESIGN_EXPIRY * 1000),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user