| 116 | } |
| 117 | |
| 118 | export async function sendFile(ctx: ParameterizedContext, fileAbsPath: string) { |
| 119 | const rate = Number(globalConfiguration.config.downloadSpeedRate) || 0; |
| 120 | if (rate <= 0) { |
| 121 | const fileDir = path.dirname(fileAbsPath); |
| 122 | const fileName = path.basename(fileAbsPath); |
| 123 | ctx.set("Content-Type", "application/octet-stream"); |
| 124 | await send(ctx, fileName, { root: fileDir + "/", hidden: true }); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | const fileName = path.basename(fileAbsPath); |
| 129 | const stats = await fsPromises.stat(fileAbsPath); |
| 130 | |
| 131 | ctx.set("Content-Type", "application/octet-stream"); |
| 132 | ctx.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName)}"`); |
| 133 | ctx.set("Content-Length", stats.size.toString()); |
| 134 | |
| 135 | const fileStream = createReadStream(fileAbsPath); |
| 136 | ctx.body = fileStream.pipe(new ThrottleTransform(rate)); |
| 137 | } |