()
| 160 | } |
| 161 | |
| 162 | const parseUpload = () => { |
| 163 | return new Promise((resolve, reject) => { |
| 164 | let hasFile = false; |
| 165 | let fileCount = 0; |
| 166 | let settled = false; |
| 167 | const uploadDirectory = path.resolve(__dirname, "../uploads"); |
| 168 | |
| 169 | const fail = (status, message) => { |
| 170 | if (settled) return; |
| 171 | settled = true; |
| 172 | reject({ status, message }); |
| 173 | }; |
| 174 | |
| 175 | const succeed = (value) => { |
| 176 | if (settled) return; |
| 177 | settled = true; |
| 178 | resolve(value); |
| 179 | }; |
| 180 | |
| 181 | req.pipe(req.busboy); |
| 182 | |
| 183 | req.busboy.on("file", (fieldName, file, info) => { |
| 184 | fileCount += 1; |
| 185 | if (fileCount > 1) { |
| 186 | file.resume(); |
| 187 | fail(400, "Only one logo file can be uploaded"); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | hasFile = true; |
| 192 | const mimeType = `${info?.mimeType || ""}`.toLowerCase(); |
| 193 | if (!isAllowedLogoMimeType(mimeType)) { |
| 194 | file.resume(); |
| 195 | fail(415, "Unsupported file type. Only png, jpg, gif, webp, and svg are allowed"); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | const chunks = []; |
| 200 | let totalSize = 0; |
| 201 | |
| 202 | file.on("data", (chunk) => { |
| 203 | if (settled) return; |
| 204 | |
| 205 | totalSize += chunk.length; |
| 206 | if (totalSize > MAX_LOGO_UPLOAD_SIZE_BYTES) { |
| 207 | file.resume(); |
| 208 | fail(413, "Logo exceeds maximum size (5MB)"); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | chunks.push(chunk); |
| 213 | }); |
| 214 | |
| 215 | file.on("error", () => { |
| 216 | fail(400, "Could not process uploaded file"); |
| 217 | }); |
| 218 | |
| 219 | file.on("end", async () => { |
no test coverage detected