(req, res)
| 209 | } |
| 210 | |
| 211 | async getHandler(req, res) { |
| 212 | const config = Config.get(req.params.appId); |
| 213 | if (!config) { |
| 214 | const error = createSanitizedHttpError(403, 'Invalid application ID.', config); |
| 215 | res.status(error.status); |
| 216 | res.json({ error: error.message }); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | FilesRouter._validateFileDownload(req, config); |
| 221 | |
| 222 | let filename = FilesRouter._getFilenameFromParams(req); |
| 223 | try { |
| 224 | const filesController = config.filesController; |
| 225 | const mime = (await import('mime')).default; |
| 226 | let contentType = mime.getType(filename); |
| 227 | let file = new Parse.File(filename, { base64: '' }, contentType); |
| 228 | const fileAuth = req.auth; |
| 229 | const triggerResult = await triggers.maybeRunFileTrigger( |
| 230 | triggers.Types.beforeFind, |
| 231 | { file }, |
| 232 | config, |
| 233 | fileAuth |
| 234 | ); |
| 235 | if (triggerResult?.file?._name) { |
| 236 | filename = triggerResult?.file?._name; |
| 237 | contentType = mime.getType(filename); |
| 238 | } |
| 239 | |
| 240 | const defaultResponseHeaders = { 'X-Content-Type-Options': 'nosniff' }; |
| 241 | |
| 242 | if (isFileStreamable(req, filesController)) { |
| 243 | const afterFind = await triggers.maybeRunFileTrigger( |
| 244 | triggers.Types.afterFind, |
| 245 | { file, forceDownload: false, responseHeaders: { ...defaultResponseHeaders } }, |
| 246 | config, |
| 247 | fileAuth |
| 248 | ); |
| 249 | if (afterFind?.forceDownload) { |
| 250 | res.set('Content-Disposition', `attachment;filename=${afterFind.file?._name || filename}`); |
| 251 | } |
| 252 | for (const [key, value] of Object.entries(afterFind?.responseHeaders ?? defaultResponseHeaders)) { |
| 253 | res.set(key, value); |
| 254 | } |
| 255 | filesController.handleFileStream(config, filename, req, res, contentType).catch(() => { |
| 256 | res.status(404); |
| 257 | res.set('Content-Type', 'text/plain'); |
| 258 | res.end('File not found.'); |
| 259 | }); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | let data = await filesController.getFileData(config, filename).catch(() => { |
| 264 | res.status(404); |
| 265 | res.set('Content-Type', 'text/plain'); |
| 266 | res.end('File not found.'); |
| 267 | }); |
| 268 | if (!data) { |
nothing calls this directly
no test coverage detected