Skip to content

Commit

Permalink
Merge pull request #11 from ranjandsingh/9-no-way-to-rename-the-file
Browse files Browse the repository at this point in the history
updated filename function to give option for a function to generate filename
  • Loading branch information
ranjandsingh authored Jul 15, 2023
2 parents 1afdc05 + 8c3dd17 commit 3611568
Showing 2 changed files with 55 additions and 19 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,17 @@ Small Utilty to use with Multer as storage engine to optimise images on the fly
const SharpMulter = require("sharp-multer");
const app = express();
// optional function to return new File Name
const newFilenameFunction = (og_filename, options) => {
const newname =
og_filename.split(".").slice(0, -1).join(".") +
`${options.useTimestamp ? "-" + Date.now() : ""}` +
"." +
options.fileFormat;
return newname;
};
const storage = SharpMulter({
destination: (req, file, callback) => callback(null, "images"),
@@ -27,6 +38,7 @@ Small Utilty to use with Multer as storage engine to optimise images on the fly
input: "./images/logo.png",
location: "top-right",
},
filename:newFilenameFunction, // optional
});
const upload = multer({ storage })
app.post("/upload", upload.single("avatar"), async (req, res) => {
@@ -40,14 +52,14 @@ Small Utilty to use with Multer as storage engine to optimise images on the fly

## Image Options

| Key | Option | Description |
| ---------------- | --------------------------------------------------- | ----------------------------------------------------- |
| fileFormat | `jpg / png/ webp 'Default:"jpg"` | Output file type |
| resize | `{height:"", widht:"",resizeMode:"" } 'Default:{}'` | If provided Images will be resized before saving |
| quality | `Number(0-100) 'Default :80'` | Reduces the qulity for better performance |
| useTimestamp | `true/false 'Default :false'`(optional) | Adds suffice to file name Ex: "Images_1653679779.jpg" |
| watermarkOptions | `{input:"", location:"",opacity:1-100} ` (optional) | Adds watermark on every Images before Saving |

| Key | Option | Description |
| ---------------- | ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| fileFormat | `jpg / png/ webp 'Default:"jpg"` | Output file type |
| resize | `{height:"", widht:"",resizeMode:"" } 'Default:{}'` | If provided Images will be resized before saving |
| quality | `Number(0-100) 'Default :80'` | Reduces the qulity for better performance |
| useTimestamp | `true/false 'Default :false'`(optional) | Adds suffice to file name Ex: "Images_1653679779.jpg" |
| watermarkOptions | `{input:"", location:"",opacity:1-100} ` (optional) | Adds watermark on every Images before Saving |
| filename | `(originalname,options) => return newname ` (optional) | Option to return a new name for saving file it will give you original name as first argument you must return a string with filename and extension like "image.png" |

### resizeMode

@@ -65,6 +77,22 @@ Small Utilty to use with Multer as storage engine to optimise images on the fly
watermarkOptions supports total 5 locations : `"center","top-left","top-right","bottom-left","bottom-right" `

### filename

Option to return a new name for saving file it will give you original name as first argument you must return a string with filename and extension like "image.png"
ex:

```js
const newFilenameFunction = (og_filename, options) => {
const newname =
og_filename.split(".").slice(0, -1).join(".") +
`${options.useTimestamp ? "-" + Date.now() : ""}` +
"." +
options.fileFormat;
return newname;
};
```

Feel free to open a Issue for features or bug fixes

Thanks
30 changes: 19 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ const getDestination = (req, file, cb) => {
cb(null, "/dev/null");
};

const getFilename = (name, input) =>
const getFilenameDefault = (name, input) =>
name.split(".").slice(0, -1).join(".") +
`${input.useTimestamp ? "-" + Date.now() : ""}` +
"." +
@@ -78,7 +78,8 @@ const handleSave = async (
cb,
imageOptions,
path,
watermarkOptions
watermarkOptions,
getFilename = getFilenameDefault
) => {
let stream = sharp();

@@ -92,15 +93,13 @@ const handleSave = async (
//handling image Options
stream = prepareSharpStream(stream, imageOptions);

await stream
.toFile(path + "/" + filename, function (err) {
if (err) console.log(err);
console.log("done");
cb(null, {
filename: filename,
path: path + "/" + filename,
});
await stream.toFile(path + "/" + filename, function (err) {
if (err) console.log(err);
cb(null, {
filename: filename,
path: path + "/" + filename,
});
});

// finally
file.stream.pipe(stream);
@@ -111,14 +110,23 @@ function MyCustomStorage(options) {
this.imageOptions = options.imageOptions ||
options.sharpOptions || { fileFormat: "jpg", quality: 80 };
this.watermarkOptions = options.watermarkOptions;
this.getFilename = options.filename || getFilenameDefault;
}

MyCustomStorage.prototype._handleFile = function _handleFile(req, file, cb) {
const imageOptions = this.imageOptions;
const watermarkOptions = this.watermarkOptions;
this.getDestination(req, file, function (err, path) {
if (err) return cb(err);
handleSave(req, file, cb, imageOptions, path, watermarkOptions);
handleSave(
req,
file,
cb,
imageOptions,
path,
watermarkOptions,
this.getFilename
);
});
};

0 comments on commit 3611568

Please sign in to comment.