* feat: allow self-signed certificates * fix: Extra information in self signed error * chore: simplified PR * feat: allow https proxies * fix: trim() may save one or two user headaches * Update web/context/FeatureToggle.tsx --------- Co-authored-by: Louis <louis@jan.ai> Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import { DownloadRoute } from '../../../api'
|
|
import { join } from 'path'
|
|
import { userSpacePath } from '../../extension/manager'
|
|
import { DownloadManager } from '../../download'
|
|
import { HttpServer } from '../HttpServer'
|
|
import { createWriteStream } from 'fs'
|
|
|
|
export const downloadRouter = async (app: HttpServer) => {
|
|
app.post(`/${DownloadRoute.downloadFile}`, async (req, res) => {
|
|
const strictSSL = !(req.query.ignoreSSL === 'true');
|
|
const proxy = req.query.proxy?.startsWith('http') ? req.query.proxy : undefined;
|
|
const body = JSON.parse(req.body as any)
|
|
const normalizedArgs = body.map((arg: any) => {
|
|
if (typeof arg === 'string' && arg.includes('file:/')) {
|
|
return join(userSpacePath, arg.replace('file:/', ''))
|
|
}
|
|
return arg
|
|
})
|
|
|
|
const localPath = normalizedArgs[1]
|
|
const fileName = localPath.split('/').pop() ?? ''
|
|
|
|
const request = require('request')
|
|
const progress = require('request-progress')
|
|
|
|
const rq = request({ url: normalizedArgs[0], strictSSL, proxy })
|
|
progress(rq, {})
|
|
.on('progress', function (state: any) {
|
|
console.log('download onProgress', state)
|
|
})
|
|
.on('error', function (err: Error) {
|
|
console.log('download onError', err)
|
|
})
|
|
.on('end', function () {
|
|
console.log('download onEnd')
|
|
})
|
|
.pipe(createWriteStream(normalizedArgs[1]))
|
|
|
|
DownloadManager.instance.setRequest(fileName, rq)
|
|
})
|
|
|
|
app.post(`/${DownloadRoute.abortDownload}`, async (req, res) => {
|
|
const body = JSON.parse(req.body as any)
|
|
const normalizedArgs = body.map((arg: any) => {
|
|
if (typeof arg === 'string' && arg.includes('file:/')) {
|
|
return join(userSpacePath, arg.replace('file:/', ''))
|
|
}
|
|
return arg
|
|
})
|
|
|
|
const localPath = normalizedArgs[0]
|
|
const fileName = localPath.split('/').pop() ?? ''
|
|
console.debug('fileName', fileName)
|
|
const rq = DownloadManager.instance.networkRequests[fileName]
|
|
DownloadManager.instance.networkRequests[fileName] = undefined
|
|
rq?.abort()
|
|
})
|
|
}
|