IO-3092 implement backwards compatibility for Cloudinary documents.
This commit is contained in:
@@ -9,13 +9,10 @@ const crypto = require("crypto");
|
||||
const { InstanceRegion } = require("../utils/instanceMgr");
|
||||
const { GET_DOCUMENTS_BY_JOB } = require("../graphql-client/queries");
|
||||
//TODO: Remove hardcoded values.
|
||||
const imgproxyBaseUrl =
|
||||
process.env.IMGPROXY_BASE_URL ||
|
||||
// `https://k2car6fha7w5cbgry3j2td56ra0kdmwn.lambda-url.ca-central-1.on.aws` ||
|
||||
`https://d3ictiiutovkvi.cloudfront.net`;
|
||||
const imgproxyKey = process.env.IMGPROXY_KEY || `secret`;
|
||||
const imgproxySalt = process.env.IMGPROXY_SALT || `salt`;
|
||||
const imgproxyDestinationBucket = process.env.IMGPROXY_DESTINATION_BUCKET || `imex-shop-media`;
|
||||
const imgproxyBaseUrl = process.env.IMGPROXY_BASE_URL;
|
||||
const imgproxyKey = process.env.IMGPROXY_KEY;
|
||||
const imgproxySalt = process.env.IMGPROXY_SALT;
|
||||
const imgproxyDestinationBucket = process.env.IMGPROXY_DESTINATION_BUCKET;
|
||||
|
||||
//Generate a signed upload link for the S3 bucket.
|
||||
//All uploads must be going to the same shop and jobid.
|
||||
@@ -35,7 +32,7 @@ exports.generateSignedUploadUrls = async (req, res) => {
|
||||
const signedUrls = [];
|
||||
for (const filename of filenames) {
|
||||
// TODO: Implement a different, unique file naming convention.
|
||||
const key = GenerateKey({ bodyshopid, jobid, filename });
|
||||
const key = filename; //GenerateKey({ bodyshopid, jobid, filename });
|
||||
const client = new S3Client({ region: InstanceRegion() });
|
||||
const command = new PutObjectCommand({ Bucket: imgproxyDestinationBucket, Key: key });
|
||||
const presignedUrl = await getSignedUrl(client, command, { expiresIn: 360 });
|
||||
@@ -76,10 +73,17 @@ exports.getThumbnailUrls = async (req, res) => {
|
||||
|
||||
for (const document of data.documents) {
|
||||
//Format to follow:
|
||||
//<Cloudfront_to_lambdal>/<hmac with SHA of entire request URI path (with base64 encoded URL if needed), beginning with unencoded/unhashed Salt>/<remainder of url - resize params >/< base 64 URL encoded to image path>
|
||||
//<Cloudfront_to_lambda>/<hmac with SHA of entire request URI path (with base64 encoded URL if needed), beginning with unencoded/unhashed Salt>/<remainder of url - resize params >/< base 64 URL encoded to image path>
|
||||
|
||||
//When working with documents from Cloudinary, the URL does not include the extension.
|
||||
let key;
|
||||
if (/\.[^/.]+$/.test(document.key)) {
|
||||
key = document.key;
|
||||
} else {
|
||||
key = `${document.key}.${document.extension.toLowerCase()}`;
|
||||
}
|
||||
// Build the S3 path to the object.
|
||||
const fullS3Path = `s3://${imgproxyDestinationBucket}/${document.key}`;
|
||||
const fullS3Path = `s3://${imgproxyDestinationBucket}/${key}`;
|
||||
const base64UrlEncodedKeyString = base64UrlEncode(fullS3Path);
|
||||
//Thumbnail Generation Block
|
||||
const thumbProxyPath = `${thumbResizeParams}/${base64UrlEncodedKeyString}`;
|
||||
@@ -93,7 +97,10 @@ exports.getThumbnailUrls = async (req, res) => {
|
||||
const s3Props = {};
|
||||
if (!document.type.startsWith("image")) {
|
||||
//If not a picture, we need to get a signed download link to the file using S3 (or cloudfront preferably)
|
||||
const command = new GetObjectCommand({ Bucket: imgproxyDestinationBucket, Key: document.key });
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: imgproxyDestinationBucket,
|
||||
Key: key
|
||||
});
|
||||
const presignedGetUrl = await getSignedUrl(s3client, command, { expiresIn: 360 });
|
||||
s3Props.presignedGetUrl = presignedGetUrl;
|
||||
|
||||
@@ -142,7 +149,7 @@ exports.deleteFiles = async (req, res) => {
|
||||
function GenerateKey({ bodyshopid, jobid, filename }) {
|
||||
let nameArray = filename.split(".");
|
||||
let extension = nameArray.pop();
|
||||
return `${bodyshopid}/${jobid}/${nameArray.join(".")}-${Date.now()}.${extension}`;
|
||||
return `${bodyshopid}/${jobid}/${nameArray.join(".")}-${Date.now()}`;
|
||||
}
|
||||
|
||||
function base64UrlEncode(str) {
|
||||
|
||||
Reference in New Issue
Block a user