Socket
Socket
Sign inDemoInstall

multer-sharp

Package Overview
Dependencies
246
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    multer-sharp

Streaming multer storage engine permit to resize and upload to Google Cloud Storage.


Version published
Maintainers
1
Created

Readme

Source

Multer-Sharp

Codeship Status for ikhsanalatsary/multer-sharp Build Status Code Climate codecov.io Depedencies Status devDepedencies Status


Multer Sharp is streaming multer storage engine permit to resize and upload to Google Cloud Storage.

This project is mostly an integration piece for existing code samples from Multer's storage engine documentation. With add-ons include google-cloud and sharp

Requirement:

node v6 LTS version or latest version

Installation

npm:

npm install --save multer-sharp

yarn:

yarn add multer-sharp

Tests

npm test

Usage

const express = require('express');
const multer = require('multer');
const gcsSharp = require('multer-sharp');

const app = express();

// without resize image
const storage = gcsSharp({
    bucket: 'YOUR_BUCKET', // Required : bucket name to upload
    projectId: 'YOUR_PROJECTID', // Required : Google project ID
    keyFilename: 'YOUR_KEYFILENAME', // Required : JSON credentials file for Google Cloud Storage
    destination: 'public/image', // Optional : destination folder to store your file on Google Cloud Storage, default: ''
    acl: 'publicRead' // Required : acl credentials file for Google Cloud Storage, 'publicrRead' or 'private', default: 'private'
});
const upload = multer({ storage });

app.post('/upload', upload.single('myPic'), (req, res) => {
    console.log(req.file); // Print upload details
    res.send('Successfully uploaded!');
});

// or

// simple resize with custom filename
const storage2 = gcsSharp({
  filename: (req, file, cb) => {
      cb(null, `${file.fieldname}-newFilename`);
  },
  bucket: 'YOUR_BUCKET', // Required : bucket name to upload
  projectId: 'YOUR_PROJECTID', // Required : Google project ID
  keyFilename: 'YOUR_KEYFILENAME', // Required : JSON credentials file for Google Cloud Storage
  acl: 'publicRead', // Required : acl credentials file for Google Cloud Storage, 'publicrRead' or 'private', default: 'private'
  size: {
    width: 400,
    height: 400
  },
  max: true
});
const upload2 = multer({ storage: storage2 });

app.post('/uploadwithfilename', upload2.single('myPic'), (req, res, next) => {
    console.log(req.file); // Print upload details
    res.send('Successfully uploaded!');
});

/* If you need generate image with specific size
 * simply to adding `sizes` property
 * sizes must be an `array` and must be specify
 * with suffix, width / height property
 */
const storage = multerSharp({
  bucket: config.uploads.gcsUpload.bucket,
  projectId: config.uploads.gcsUpload.projectId,
  keyFilename: config.uploads.gcsUpload.keyFilename,
  acl: config.uploads.gcsUpload.acl,
  sizes: [
    { suffix: 'xlg', width: 1200, height: 1200 },
    { suffix: 'lg', width: 800, height: 800 },
    { suffix: 'md', width: 500, height: 500 },
    { suffix: 'sm', width: 300, height: 300 },
    { suffix: 'xs', width: 100 }
  ],
  max: true
});
const upload = multer({ storage });

app.post('/uploadmultiplesize', upload.single('myPic'), (req, res, next) => {
    console.log(req.file);
    /*
    * will print like this
    {
      originalname: 'nodejs-512.png',
      encoding: '7bit',
      mimetype: 'image/png',
      md: {
        path: 'https://storage.googleapis.com/multer-sharp.appspot.com/cd2105f5d60684a9f7c9fd2c340befed-md',
        mimetype: 'image/png',
        filename: 'cd2105f5d60684a9f7c9fd2c340befed-md'
      },
      sm: {
        path: 'https://storage.googleapis.com/multer-sharp.appspot.com/cd2105f5d60684a9f7c9fd2c340befed-sm',
        mimetype: 'image/png',
        filename: 'cd2105f5d60684a9f7c9fd2c340befed-sm'
      },
      xs: {
        path: 'https://storage.googleapis.com/multer-sharp.appspot.com/cd2105f5d60684a9f7c9fd2c340befed-xs',
        mimetype: 'image/png',
        filename: 'cd2105f5d60684a9f7c9fd2c340befed-xs'
      }
    }
    */
    res.send('Successfully uploaded!');
});

for more example you can see here

Options

const storage = gcsSharp(options);
Multer-Sharp options
optiondefaultrole
filenamerandomStringyour output filename
bucketnoRequired your bucket name on Google Cloud Storage to upload
projectIdnoRequired your project id on Google Cloud Storage to upload
keyFilenamenoRequired JSON credentials file for Google Cloud Storage
acl'private'Required acl credentials file for Google Cloud Storage, value: publicRead or private, doc: https://cloud.google.com/storage/docs/access-control/lists
destinationemptyStringOptional, destination folder to store your file on Google Cloud Storage
formatoriginalFileFormattype of output file to produce. valid value : 'jpeg', 'png', 'magick', 'webp', 'tiff', 'openslide', 'dz', 'ppm', 'fits', 'gif', 'svg', 'pdf', 'v', 'raw' or object. if object specify as follow: { type: 'png', option: { [...toFormatOptions] } } doc: sharpToFormat
sizenosize specification object for output image, as follow: { width: 300, height: 200, option: {[...resizeOptions]} } property height & option is optional. doc: sharpResizeOptions
sizesnoan Array of size specification object for output image and specify diff size with suffix, as follow: { suffix: 'md', width: 300, height: 200, option: {[...resizeOptions]} } property height & option is optional. doc: sharpResizeOptions
sharp options

Please visit this sharp for detailed overview of specific option.

multer-sharp embraces sharp option, as table below:

optiondefaultrole
resizetrueresize images as per their size mentioned in options.size
cropfalsecrop image
backgroundfalseset the background for the embed, flatten and extend operations.
embedfalseembed on canvas
maxfalseset maximum output dimension
minfalseset minimum output dimension
withoutEnlargementfalsedo not enlarge small images
ignoreAspectRatiofalseignore aspect ration while resizing images
extractfalseextract specific part of image
trimfalseTrim boring pixels from all edges
flattenfalseMerge alpha transparency channel, if any, with background.
extendfalseExtends/pads the edges of the image with background.
negatefalseProduces the negative of the image.
rotatefalseRotate the output image by either an explicit angle
flipfalseFlip the image about the vertical Y axis.
flopfalseFlop the image about the horizontal X axis.
blurfalseMild blur of the output image
sharpenfalseMild sharpen of the output image
gammafalseApply a gamma correction.
grayscale or greyscalefalseConvert to 8-bit greyscale; 256 shades of grey.
normalize or normalisefalseEnhance output image contrast by stretching its luminance to cover the full dynamic range.
withMetadatafalseInclude all metadata (EXIF, XMP, IPTC) from the input image in the output image.
convolvefalseConvolve the image with the specified kernel.
thresholdfalseAny pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0
toColourspace or toColorspacefalseSet the output colourspace. By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.

License

MIT Copyright (c) 2017 - forever Abdul Fattah Ikhsan

Keywords

FAQs

Last updated on 11 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc