Skip to content

Commit

Permalink
Add additional CreateWriteStreamOptions options
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenglongMa committed Dec 8, 2020
1 parent 454067a commit 660ef88
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 159 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ $ npm i @chenglongma/skipper-gcstorage
```

## Changelog

### Ver 2.1.0

Thanks [jspark-gigworks (Anselmo Park)](https://github.com/jspark-gigworks) so much for his comments!

1. Emit`writefile` event when finishing the job.
2. Support additional `CreateWriteStreamOptions` listed in https://googleapis.dev/nodejs/storage/latest/global.html#CreateWriteStreamOptions.

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

Expand All @@ -43,6 +51,10 @@ req.file('avatar')
maxBytes: 60000,
metadata: {},
public: true,
gzip: true,
// Other options in `CreateWriteStreamOptions`
// Refer to https://googleapis.dev/nodejs/storage/latest/global.html#CreateWriteStreamOptions
...CreateWriteStreamOptions,
resize: {
width: 500,
height: 500
Expand Down Expand Up @@ -74,4 +86,5 @@ Please don't check in your GCP credentials :)
## Acknowledgement

1. [Sails Skipper](https://github.com/sailshq/skipper)
2. [Skipper-S3](https://github.com/balderdashy/skipper-s3)
2. [Skipper-S3](https://github.com/balderdashy/skipper-s3)
3. [jspark-gigworks (Anselmo Park)](https://github.com/jspark-gigworks)
17 changes: 4 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,26 @@ module.exports = function SkipperGCS(globalOpts) {
// console.log('ERROR ON incoming readable file stream in Skipper Google Cloud Storage adapter (%s) ::', incomingFileStream.filename, unusedErr);
});//œ

const metadata = {};
_.defaults(metadata, options.metadata);
metadata.contentType = mime.getType(incomingFd);
options.metadata.contentType = mime.getType(incomingFd);

// The default `upload` implements a unique filename by combining:
// • a generated UUID (like "4d5f444-38b4-4dc3-b9c3-74cb7fbbc932")
// • the uploaded file's original extension (like ".jpg")
const file = bucket.file(incomingFd);
const isImage = metadata.contentType && metadata.contentType.startsWith('image');
const isImage = options.metadata.contentType && options.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, }))
stream.pipe(file.createWriteStream(options))
.on('error', (err) => receiver__.emit("error", err))
.on('finish', function () {
incomingFileStream.extra = file.metadata;
// Indicate that a file was persisted.
receiver__.emit('writefile', incomingFileStream);
if (options.public) {
file.makePublic().then(() => {
incomingFileStream.extra.Location = "https://storage.googleapis.com/" + options.bucket + "/" + incomingFd;
proceed();
});
} else {
proceed();
}
proceed();
});
});
};
Expand Down
Loading

0 comments on commit 660ef88

Please sign in to comment.