Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S3 extension image upload #694

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions packages/s3_file_storage/services/awsFileUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports.awsFileUploader = {
const params = {
Bucket: bucketName,
Key: fileName,
Body: fileContent
Body: fileContent,
ContentType: file.mimetype,
ACL: 'public-read'
};

const uploadCommand = new PutObjectCommand(params);
Expand All @@ -32,10 +34,7 @@ module.exports.awsFileUploader = {
name: files[index].filename,
path: path.join(requestedPath, files[index].filename),
size: files[index].size,
url: `https://${bucketName}.s3.amazonaws.com/${path.join(
requestedPath,
files[index].filename
)}`
url: `https://s3.${getEnv('AWS_REGION')}.amazonaws.com/${bucketName}/${path.join(requestedPath, files[index].filename)}`
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,77 @@ const { update } = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const { error } = require('@evershop/evershop/src/lib/log/logger');

async function downloadObjectToBuffer(objectUrl) {
async function downloadObject(s3Client, objectUrl) {

const parsedUrl = new URL(objectUrl);
const bucketName = parsedUrl.host.split('.')[0]; // Extract the bucket name
const objectKey = parsedUrl.pathname.substr(1); // Extract the object key (remove leading '/')
let bucketName; let objectKey;

if (parsedUrl.host.startsWith("s3")) {
// Generic S3 URL format: s3.<region>.amazonaws.com/<bucket-name>/<object-key>
const pathParts = parsedUrl.pathname.split('/');
[,bucketName] = pathParts; // Extract bucket name
objectKey = pathParts.slice(2).join('/'); // Extract object key
} else {
// Bucket URL format: <bucket-name>.s3.amazonaws.com/<object-key>
[bucketName] = parsedUrl.host.split('.'); // Extract bucket name
objectKey = parsedUrl.pathname.substr(1); // Extract object key (remove leading '/')
}

if (!bucketName || !objectKey) {
throw new Error(`Failed to extract bucket name or object key from URL: ${objectUrl}`);
}


const params = {
Bucket: bucketName,
Key: objectKey
};

const getObjectCommand = new GetObjectCommand(params);
const s3Client = new S3Client({ region: getEnv('AWS_REGION') });
const data = await s3Client.send(getObjectCommand);

return data;
}

async function objectToBuffer(data) {
// Get content as a buffer from the data.Body object
const buffer = await data.Body.transformToByteArray();
return buffer;
}

async function resizeAndUploadImage(
s3Client,
originalObjectUrl,
originalImageBuffer,
resizedObjectUrl,
width,
height
height,
contentType
) {
const bucketName = getEnv('AWS_BUCKET_NAME');
const originalImageBuffer = await downloadObjectToBuffer(originalObjectUrl);
// Resize the image
const resizedImageBuffer = await sharp(originalImageBuffer)
.resize({ width, height, fit: 'inside' })
.toBuffer();

// Upload the resized image
const parsedUrl = new URL(resizedObjectUrl);
const objectKey = parsedUrl.pathname.substr(1); // Extract the object key (remove leading '/')
let objectKey; // Extract the object key (remove leading '/')

if (parsedUrl.host.startsWith("s3")) {
// Generic S3 URL: s3.<region>.amazonaws.com/<bucket-name>/<object-key>
const pathParts = parsedUrl.pathname.split('/');
objectKey = pathParts.slice(2).join('/'); // Extract only the object key
} else {
// Standard Bucket URL: <bucket-name>.s3.amazonaws.com/<object-key>
objectKey = parsedUrl.pathname.substr(1);
}

const uploadParams = {
Bucket: bucketName,
Key: objectKey,
Body: resizedImageBuffer
Body: resizedImageBuffer,
ACL: 'public-read',
ContentType: contentType
};

const uploadCommand = new PutObjectCommand(uploadParams);
Expand All @@ -76,39 +107,46 @@ module.exports = async function awsGenerateProductImageVariant(data) {
`-thumbnail${ext}`
);

const s3ObjectData = await downloadObject(s3Client, originalObjectUrl);
const originalImageBuffer = await objectToBuffer(s3ObjectData);
const contentType = s3ObjectData.ContentType || 'application/octet-stream';

// Upload the single variant
const singleUrl = await resizeAndUploadImage(
s3Client,
originalObjectUrl,
originalImageBuffer,
singleObjectUrl,
getConfig('catalog.product.image.single.width', 500),
getConfig('catalog.product.image.single.height', 500)
getConfig('catalog.product.image.single.height', 500),
contentType
);

// Upload the listing variant
const listingUrl = await resizeAndUploadImage(
s3Client,
originalObjectUrl,
originalImageBuffer,
listingObjectUrl,
getConfig('catalog.product.image.listing.width', 250),
getConfig('catalog.product.image.listing.height', 250)
getConfig('catalog.product.image.listing.height', 250),
contentType
);

// Upload the thumbnail variant
const thumnailUrl = await resizeAndUploadImage(
const thumbnailUrl = await resizeAndUploadImage(
s3Client,
originalObjectUrl,
originalImageBuffer,
thumbnailObjectUrl,
getConfig('catalog.product.image.thumbnail.width', 100),
getConfig('catalog.product.image.thumbnail.height', 100)
getConfig('catalog.product.image.thumbnail.height', 100),
contentType
);

// Update the record in the database with the new URLs in the variant columns
await update('product_image')
.given({
single_image: singleUrl,
listing_image: listingUrl,
thumb_image: thumnailUrl
thumb_image: thumbnailUrl
})
.where('product_image_product_id', '=', data.product_image_product_id)
.and('origin_image', '=', data.origin_image)
Expand Down