This commit is contained in:
daniel-j
2020-03-03 18:41:51 +01:00
parent 0dd2217c7a
commit b7a5ffdaf3
3 changed files with 61 additions and 67 deletions

View File

@@ -4,6 +4,8 @@ const Koa = require('koa')
const Router = require('@koa/router')
const multer = require('@koa/multer')
const logger = require('koa-logger')
const sendfile = require('koa-sendfile')
const mkdirp = require('mkdirp')
const fs = require('fs')
const { spawn } = require('child_process')
@@ -49,6 +51,7 @@ function expireKey (key) {
return timer
}
const random = uniqueRandom(1000, 9999)
const app = new Koa()
@@ -130,9 +133,9 @@ router.get('/download/:key', async ctx => {
}
expireKey(key)
console.log('Sending file!')
ctx.set('Content-Type', 'application/epub+zip')
ctx.set('Content-disposition', 'attachment; filename=' + info.file.name);
ctx.body = fs.createReadStream(info.file.path)
await sendfile(ctx, info.file.path)
// ctx.type = 'application/epub+zip'
ctx.attachment(info.file.name)
})
@@ -146,7 +149,7 @@ router.post('/upload', upload.single('file'), async ctx => {
ctx.throw(400, 'Uploaded file does not end with .epub ' + ctx.request.file.originalname)
}
if (!ctx.request.file) {
if (!ctx.request.file || ctx.request.file.size === 0) {
ctx.throw(400, 'Invalid or no file submitted')
}
@@ -183,15 +186,14 @@ router.post('/upload', upload.single('file'), async ctx => {
info.file = {
name: filename,
path: data,
size: ctx.request.file.size,
// size: ctx.request.file.size,
uploaded: new Date()
}
console.log(info.file)
ctx.set('Location', '/')
ctx.body = null
ctx.redirect('back', '/')
})
router.delete('/file/:key', ctx => {
router.delete('/file/:key', async ctx => {
const key = ctx.params.key
const info = ctx.keys.get(key)
if (!info) {
@@ -218,22 +220,24 @@ router.get('/status/:key', async ctx => {
alive: info.alive,
file: info.file ? {
name: info.file.name,
size: info.file.size
// size: info.file.size
} : null
}
})
router.get('/', ctx => {
router.get('/', async ctx => {
const agent = ctx.get('user-agent')
console.log(agent)
ctx.set('Content-Type', 'text/html')
ctx.body = fs.createReadStream(agent.includes('Kobo') ? 'download.html' : 'upload.html')
await sendfile(ctx, agent.includes('Kobo') ? 'download.html' : 'upload.html')
})
app.use(logger())
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(port)
console.log('server is listening on port ' + port)
fs.rmdir('uploads', {recursive: true}, (err) => {
if (err) throw err
mkdirp.sync('uploads')
app.listen(port)
console.log('server is listening on port ' + port)
})

91
package-lock.json generated
View File

@@ -256,6 +256,11 @@
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
},
"etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
},
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
@@ -381,15 +386,29 @@
"passthrough-counter": "^1.0.0"
}
},
"koa-send": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/koa-send/-/koa-send-5.0.0.tgz",
"integrity": "sha512-90ZotV7t0p3uN9sRwW2D484rAaKIsD8tAVtypw/aBU+ryfV+fR2xrcAwhI8Wl6WRkojLUs/cB9SBSCuIb+IanQ==",
"koa-sendfile": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/koa-sendfile/-/koa-sendfile-2.0.1.tgz",
"integrity": "sha512-4Qxjhx/Nvh/NtRelViUzdZDqs2YmDsNSLvLq3m4vqYlXUUZdR6YktF4uwGn48EVFFWJPhtvTx1MBcZBM9AfTkQ==",
"requires": {
"debug": "^3.1.0",
"http-errors": "^1.6.3",
"mz": "^2.7.0",
"resolve-path": "^1.4.0"
"debug": "^3.2.5",
"etag": "^1.5.1",
"mz": "2"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"media-typer": {
@@ -421,12 +440,9 @@
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
},
"mkdirp": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"requires": {
"minimist": "0.0.8"
}
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.3.tgz",
"integrity": "sha512-6uCP4Qc0sWsgMLy1EOqqS/3rjDHOEnsStVr/4vtAIK2Y5i2kA7lFFejYrpIyiN9w0pYf4ckeCYT9f1r1P9KX5g=="
},
"ms": {
"version": "2.0.0",
@@ -446,6 +462,16 @@
"on-finished": "^2.3.0",
"type-is": "^1.6.4",
"xtend": "^4.0.0"
},
"dependencies": {
"mkdirp": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"requires": {
"minimist": "0.0.8"
}
}
}
},
"mz": {
@@ -491,11 +517,6 @@
"resolved": "https://registry.npmjs.org/passthrough-counter/-/passthrough-counter-1.0.0.tgz",
"integrity": "sha1-GWfZ5m2lcrXAI8eH2xEqOHqxZvo="
},
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
},
"path-to-regexp": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.8.0.tgz",
@@ -520,38 +541,6 @@
"string_decoder": "~0.10.x"
}
},
"resolve-path": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/resolve-path/-/resolve-path-1.4.0.tgz",
"integrity": "sha1-xL2p9e+y/OZSR4c6s2u02DT+Fvc=",
"requires": {
"http-errors": "~1.6.2",
"path-is-absolute": "1.0.1"
},
"dependencies": {
"http-errors": {
"version": "1.6.3",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz",
"integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=",
"requires": {
"depd": "~1.1.2",
"inherits": "2.0.3",
"setprototypeof": "1.1.0",
"statuses": ">= 1.4.0 < 2"
}
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
},
"setprototypeof": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz",
"integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ=="
}
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",

View File

@@ -13,7 +13,8 @@
"@koa/router": "^8.0.8",
"koa": "^2.11.0",
"koa-logger": "^3.2.1",
"koa-send": "^5.0.0",
"koa-sendfile": "^2.0.1",
"mkdirp": "^1.0.3",
"multer": "^1.4.2"
}
}