Skip to content

Instantly share code, notes, and snippets.

@mubasshir
Last active January 31, 2020 13:03
Show Gist options
  • Save mubasshir/fcaf81a9f522d6ebe3a642c16fde3eac to your computer and use it in GitHub Desktop.
Save mubasshir/fcaf81a9f522d6ebe3a642c16fde3eac to your computer and use it in GitHub Desktop.
import * as path from 'path';
import * as imager from 'multer-imager';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
const ENV = process.env.NODE_ENV;
if (!ENV) {
const envFilePath = path.resolve(process.cwd(), 'env', !ENV ? '.env' : `.env.${ENV}`);
const envConfig = dotenv.parse(fs.readFileSync(envFilePath));
for (const k in envConfig) {
if (envConfig.hasOwnProperty(k)) {
process.env[k] = envConfig[k];
}
}
}
export const multerImagerStorage = imager({
dirname: 'images',
bucket: process.env.S3_BUCKET,
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_ACCESS_SECRET,
region: process.env.S3_REGION,
gm: { // [Optional]: define graphicsmagick options
width: 100, // doc: http://aheckmann.github.io/gm/docs.html#resize
},
s3: { // [Optional]: define s3 options
Metadata: { // http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
ACL: 'public-read',
},
},
filename: (req, file, cb) => {
const datetimestamp = Date.now();
const extenstion = path.extname(file.originalname);
let fileName = path.basename(file.originalname, extenstion);
// replace special chars with hyphen and remove extra hyphens
fileName = fileName.replace(/[^a-zA-Z]/g, '-').replace(/-{2,}/g, '-');
// check if lenght more than 20 then trim
fileName = fileName.substring(0, 20).toLowerCase();
// callback
cb(null, datetimestamp + '-' + fileName + extenstion);
},
});
import * as AWS from 'aws-sdk';
import * as multerS3 from 'multer-s3';
import * as path from 'path';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
const ENV = process.env.NODE_ENV;
if (!ENV) {
const envFilePath = path.resolve(process.cwd(), 'env', !ENV ? '.env' : `.env.${ENV}`);
const envConfig = dotenv.parse(fs.readFileSync(envFilePath));
for (const k in envConfig) {
if (envConfig.hasOwnProperty(k)) {
process.env[k] = envConfig[k];
}
}
}
const s3 = new AWS.S3();
AWS.config.update({
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_ACCESS_SECRET,
region: process.env.S3_REGION,
});
export const multerS3Storage = multerS3({
s3,
bucket: process.env.S3_BUCKET,
acl: 'public-read',
metadata: (req, file, cb) => {
cb(null, { fieldName: file.fieldname });
},
key: (req, file, cb) => {
const datetimestamp = Date.now();
const extenstion = path.extname(file.originalname);
let fileName = path.basename(file.originalname, extenstion);
// replace special chars with hyphen and remove extra hyphens
fileName = fileName.replace(/[^a-zA-Z]/g, '-').replace(/-{2,}/g, '-');
// check if lenght more than 20 then trim
fileName = fileName.substring(0, 20).toLowerCase();
// callback
cb(null, datetimestamp + '-' + fileName + extenstion);
},
});
import * as path from 'path';
import { UnsupportedMediaTypeException } from '@nestjs/common';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
import { multerImagerStorage } from './multer-imager-storage';
import { multerS3Storage } from './multer-s3-storage';
export const s3MulterOptions: MulterOptions = {
storage: multerS3Storage,
limits: {
// tslint:disable-next-line: radix
fileSize: parseInt(process.env.S3_MAX_FILE_LIMIT || '5') * 1024 * 1024, // 5MB
},
fileFilter: (req, file, callback) => {
const ext = path.extname(file.originalname);
if (ext !== '.png' && ext !== '.jpg' && ext !== '.jpeg') {
return callback(new UnsupportedMediaTypeException('Only images are allowed'), false);
}
return callback(null, true);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment