forked from ranjandsingh/sharp-multer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (117 loc) · 3.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require("fs");
const sharp = require("sharp");
const getDestination = (req, file, cb) => {
cb(null, "/dev/null");
};
const getFilename = (name, input) =>
name.split(".").slice(0, -1).join(".") +
`${input.useTimestamp ? "_" + Date.getUnixTime() : ""}` +
"." +
input.fileFormat;
const prepareSharpStream = (SharpStram, input) => {
if (input.resize) {
const { width, height, resizeMode } = input.resize;
SharpStram.resize({
width,
height,
fit: resizeMode,
});
}
switch (input.fileFormat) {
case "png":
return SharpStram.png(input.quality ? { quality: input.quality } : {});
case "webp":
return SharpStram.webp(input.quality ? { quality: input.quality } : {});
case "jpg":
return SharpStram.jpeg(input.quality ? { quality: input.quality } : {});
default:
return SharpStram.jpeg(input.quality ? { quality: input.quality } : {});
}
};
const handleWatermark = async (SharpStram, input) => {
let gravity = "";
switch (input.location) {
case "top-left":
gravity = "northwest";
break;
case "top-right":
gravity = "northeast";
break;
case "bottom-left":
gravity = "southwest";
break;
case "bottom-right":
gravity = "southeast";
break;
}
// preparing watermark with transparency
const opacity = input.opacity ? Number(input.opacity) * 2.55 : 255;
const watermarkStream = await sharp(input.input)
.composite([
{
input: Buffer.from([0, 0, 0, opacity]),
raw: {
width: 1,
height: 1,
channels: 4,
},
tile: true,
blend: "dest-in",
},
])
.toFormat("png")
.toBuffer();
return SharpStram.composite([
{ input: watermarkStream, gravity: gravity || "center", blend: "atop" },
]);
};
const handleSave = async (
req,
file,
cb,
imageOptions,
path,
watermarkOptions
) => {
let stream = sharp();
// preparing harp functions based on inputs
// checking if watermark is provided or not
if (watermarkOptions)
stream = await handleWatermark(stream, watermarkOptions);
let filename = getFilename(file.originalname, imageOptions);
//handling image Options
stream = prepareSharpStream(stream, imageOptions);
stream
.toFile(path + "/" + filename, function (err) {
if (err) console.log(err);
})
.on("finish", function () {
console.log("done");
cb(null, {
filename: filename,
path: path + "/" + filename,
});
});
// finally
file.stream.pipe(stream);
};
function MyCustomStorage(options) {
this.getDestination = options.destination || getDestination;
this.imageOptions = options.imageOptions ||
options.sharpOptions || { fileFormat: "jpg", quality: 80 };
this.watermarkOptions = options.watermarkOptions;
}
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);
});
};
MyCustomStorage.prototype._removeFile = function _removeFile(req, file, cb) {
fs.unlink(file.path, cb);
};
module.exports = function (opts) {
return new MyCustomStorage(opts);
};