-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
192 lines (179 loc) · 4.95 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const fs = require("fs-extra");
const sharp = require("sharp");
module.exports = class MulterSharpResizer {
/**
* Constructor method
* @param {object} req
* @param {string || object} filename
* @param {array} sizes
* @param {string} uploadPath
* @param {string} fileUrl
* @param {Object} sharpOptions
*/
constructor(req, filename, sizes, uploadPath, fileUrl, sharpOptions) {
this.req = req;
this.filename = filename;
this.sizes = sizes;
this.uploadPath = uploadPath;
this.sharpOptions = sharpOptions || {};
this.fileUrl = fileUrl;
this.filesUploaded = [];
this.imageExt = null;
this.imageFilename = null;
this.imageUploadPath = null;
this.data = [];
this.tmpOriginalname = null;
}
/**
* Resize files method
*/
async resize() {
if (this.req.files) {
if (!this.req.files.map) {
for (const prop in this.req.files) {
await Promise.all(
this.req.files[prop].map(async (file, i) => {
await this.promiseAllResize(
file,
i,
prop,
typeof this.filename === "object"
? this.filename[prop]
: this.filename
);
})
);
}
return;
}
}
// Check multiple files
if (this.req.file) {
return await this.promiseAllResize(this.req.file);
}
// Promise.all() multiple files for resizing
await Promise.all(
this.req.files.map(async (file, i) => {
await this.promiseAllResize(file, i);
})
);
}
/**
* Get Data method
* Data transform, preparation and return
*/
getData() {
// Check multiple files
// Categorize files by size
if (this.req.files) {
if (!this.req.files.map) {
return this.removeProp(this.getDataWithFields(), "field");
} else {
for (let i = 0; i < this.req.files.length - 1; i++) {
this.data.push({
...this.filesUploaded.splice(0, this.sizes.length),
});
}
}
}
this.data.push(this.filesUploaded);
return this.data.map((file) =>
this.renameKeys({ ...this.sizes.map((size, i) => size.path) }, file)
);
}
/**
* Change keys name method
* @param {object} keysMap
* @param {object} obj
*/
renameKeys(keysMap, obj) {
return Object.keys(obj).reduce((acc, key) => {
this.tmpOriginalname = obj[key].originalname;
this.tmpField = obj[key].field;
delete obj[key].originalname;
delete obj[key].field;
return {
...acc,
originalname: this.tmpOriginalname,
field: this.tmpField,
...{ [keysMap[key] || key]: obj[key] },
};
}, {});
}
/**
* Promise.all() for resize files method
* @param {object} file
* @param {number} i
*/
promiseAllResize(file, i, prop = "", filenameParam = this.filename) {
Promise.all(
this.sizes.map((size) => {
this.imageExt = file.mimetype.split("/")[1];
this.imageFilename = `${filenameParam.split(/\.([^.]+)$/)[0]}${
i != undefined ? `-${i}` : ""
}-${size.path}.${this.imageExt}`;
this.imageUploadPath = this.uploadPath.concat(`/${size.path}`);
fs.mkdirsSync(this.imageUploadPath);
this.filesUploaded.push({
originalname: file.originalname,
...(prop && { field: prop }),
filename: this.imageFilename,
path: `${this.fileUrl}/${size.path}/${this.imageFilename}`,
});
return sharp(file.buffer)
.resize(size.width, size.height, this.sharpOptions)
.toFile(`${this.imageUploadPath}/${this.imageFilename}`);
})
);
}
/**
* Grouping data by field
* Return Data that send with multer fields method
*/
getDataWithFields() {
for (const prop in this.req.files) {
for (let i = 0; i < this.req.files[prop].length; i++) {
this.data.push({
...this.filesUploaded.splice(0, this.sizes.length),
});
}
}
return this.groupByFields(
this.data.map((file) =>
this.renameKeys({ ...this.sizes.map((size, i) => size.path) }, file)
),
"field"
);
}
/**
* Grouping data by specific property method
* @param {array} array
* @param {property} prop
*/
groupByFields(array, prop) {
return array.reduce(function (r, a) {
r[a[prop]] = r[a[prop]] || [];
r[a[prop]].push(a);
return r;
}, Object.create(null));
}
/**
* Remove specific property method
* @param {object} obj
* @param {string} propToDelete
*/
removeProp(obj, propToDelete) {
for (var property in obj) {
if (typeof obj[property] == "object") {
delete obj.property;
let newJsonData = this.removeProp(obj[property], propToDelete);
obj[property] = newJsonData;
} else {
if (property === propToDelete) {
delete obj[property];
}
}
}
return obj;
}
};