Skip to content

Commit

Permalink
Add resize options, which can compress the **images** before upload…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
ChenglongMa committed Nov 13, 2020
1 parent 313d25f commit d146320
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ $ npm i skipper-gcstorage
$ npm i @chenglongma/skipper-gcstorage
```

## Changelog
### Ver 2.0.0
1. Add `resize` options, which can compress the **images** before uploading.

## Usage

```javascript
Expand All @@ -38,7 +42,11 @@ req.file('avatar')
}, // Refer to https://googleapis.dev/nodejs/storage/latest/global.html#CreateBucketRequest
maxBytes: 60000,
metadata: {},
public: true
public: true,
resize: {
width: 500,
height: 500
}, // Refer to https://sharp.pixelplumbing.com/api-resize#resize
}, function whenDone(err, uploadedFiles) {
if (err) {
return res.serverError(err);
Expand Down
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Writable = require("stream").Writable;
const _ = require("lodash");
const { Storage } = require('@google-cloud/storage');
const mime = require("mime");
const sharp = require('sharp');

/**
* skipper-gcs
Expand All @@ -14,6 +15,9 @@ const mime = require("mime");
* @property {Number?} maxBytes
* @property {Object?} metadata The metadata of gcs file
* @property {Bool?} public Whether to make the file public
* @property {Object?} resize // refer to https://sharp.pixelplumbing.com/api-resize#resize
* @property {Number?} width
* @property {Number?} height
*
* @returns {Dictionary}
* @property {Function} ls
Expand All @@ -25,6 +29,7 @@ module.exports = function SkipperGCS(globalOpts) {
globalOpts = globalOpts || {};
_.defaults(globalOpts, {
bucket: "",
resize: {}
});

const adapter = {
Expand Down Expand Up @@ -90,7 +95,7 @@ module.exports = function SkipperGCS(globalOpts) {
}

incomingFileStream.once('error', (unusedErr) => {
// console.log('ERROR ON incoming readable file stream in Skipper S3 adapter (%s) ::', incomingFileStream.filename, unusedErr);
// console.log('ERROR ON incoming readable file stream in Skipper Google Cloud Storage adapter (%s) ::', incomingFileStream.filename, unusedErr);
});//œ

const metadata = {};
Expand All @@ -101,9 +106,14 @@ module.exports = function SkipperGCS(globalOpts) {
// • a generated UUID (like "4d5f444-38b4-4dc3-b9c3-74cb7fbbc932")
// • the uploaded file's original extension (like ".jpg")
const file = bucket.file(incomingFd);
incomingFileStream.pipe(file.createWriteStream({
metadata: metadata,
}))
const isImage = metadata.contentType && metadata.contentType.startsWith('image');
const resize = { ...options.resize, fit: 'inside' };
const transformer = sharp().rotate().resize(resize);
const stream = isImage && (resize.width || resize.height)
? incomingFileStream.pipe(transformer)
: incomingFileStream;

stream.pipe(file.createWriteStream({ metadata: metadata, }))
.on('error', (err) => receiver__.emit("error", err))
.on('finish', function () {
incomingFileStream.extra = file.metadata;
Expand Down Expand Up @@ -142,7 +152,7 @@ function _getBucket(options) {

/**
* Get a bucket from gcs. Creat a new one if not exists.
* @param {object} options Options to access the bucket.
* @param {object} options Options to access the bucket.
* @param {function} cb Callback function executed after creation
*/
function _getOrCreatBucket(options, cb) {
Expand Down
Loading

0 comments on commit d146320

Please sign in to comment.