Skip to content

Commit

Permalink
Merge pull request #3384 from LiteFarmOrg/LF-4387-migrate-upload-publ…
Browse files Browse the repository at this point in the history
…ic-images-functions-to-new-reusable-function

Lf 4387 migrate upload public images functions to new reusable function
  • Loading branch information
kathyavini authored Aug 16, 2024
2 parents 10b3685 + 6165207 commit 7c2615b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 84 deletions.
44 changes: 2 additions & 42 deletions packages/api/src/controllers/cropController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ import NominationCrop from '../models/nominationCropModel.js';
import CropModel from '../models/cropModel.js';
import CropVarietyModel from '../models/cropVarietyModel.js';
import objection from 'objection';
import {
getPublicS3BucketName,
s3,
imaginaryPost,
getPublicS3Url,
} from '../util/digitalOceanSpaces.js';
import { v4 as uuidv4 } from 'uuid';
import { PutObjectCommand } from '@aws-sdk/client-s3';
import { uploadPublicImage } from '../util/imageUpload.js';

const { transaction, Model, UniqueViolationError } = objection;

Expand Down Expand Up @@ -213,40 +206,7 @@ const cropController = {

uploadCropImage() {
return async (req, res, next) => {
try {
const TYPE = 'webp';
const fileName = `crop/${uuidv4()}.${TYPE}`;

const THUMBNAIL_FORMAT = 'webp';
const LENGTH = '208';

const compressedImage = await imaginaryPost(
req.file,
{
width: LENGTH,
height: LENGTH,
type: THUMBNAIL_FORMAT,
aspectratio: '1:1',
},
{ endpoint: 'smartcrop' },
);

await s3.send(
new PutObjectCommand({
Body: compressedImage.data,
Bucket: getPublicS3BucketName(),
Key: fileName,
ACL: 'public-read',
}),
);

return res.status(201).json({
url: `${getPublicS3Url()}/${fileName}`,
});
} catch (error) {
console.log(error);
return res.status(400).send('Fail to upload image');
}
await uploadPublicImage('crop')(req, res, next);
};
},

Expand Down
45 changes: 3 additions & 42 deletions packages/api/src/controllers/cropVarietyController.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import CropVarietyModel from '../models/cropVarietyModel.js';
import ManagementPlanModel from '../models/managementPlanModel.js';
import CropModel from '../models/cropModel.js';
import {
getPublicS3BucketName,
s3,
imaginaryPost,
getPublicS3Url,
} from '../util/digitalOceanSpaces.js';
import { v4 as uuidv4 } from 'uuid';
import baseController from './baseController.js';
import { PutObjectCommand } from '@aws-sdk/client-s3';
import { uploadPublicImage } from '../util/imageUpload.js';

const { post } = baseController;

const cropVarietyController = {
Expand Down Expand Up @@ -131,40 +125,7 @@ const cropVarietyController = {
},
uploadCropImage() {
return async (req, res, next) => {
try {
const TYPE = 'webp';
const fileName = `crop_variety/${uuidv4()}.${TYPE}`;

const THUMBNAIL_FORMAT = 'webp';
const LENGTH = '208';

const compressedImage = await imaginaryPost(
req.file,
{
width: LENGTH,
height: LENGTH,
type: THUMBNAIL_FORMAT,
aspectratio: '1:1',
},
{ endpoint: 'smartcrop' },
);

await s3.send(
new PutObjectCommand({
Body: compressedImage.data,
Bucket: getPublicS3BucketName(),
Key: fileName,
ACL: 'public-read',
}),
);

return res.status(201).json({
url: `${getPublicS3Url()}/${fileName}`,
});
} catch (error) {
console.log(error);
return res.status(400).send('Fail to upload image');
}
await uploadPublicImage('crop_variety')(req, res, next);
};
},
};
Expand Down

0 comments on commit 7c2615b

Please sign in to comment.