Check for user agent, return raw

This commit is contained in:
Evert Prants 2019-08-06 15:35:01 +03:00
parent 682197821a
commit b69184eaea
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 11 additions and 0 deletions

View File

@ -30,6 +30,9 @@ async function init () {
let fichePath = path.join(root, name, 'index.txt')
let reqRaw = req.query.raw != null
let ua = req.get('User-Agent')
if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true
let text
try {

View File

@ -26,6 +26,8 @@ function asyncForm (req, form) {
async function clearDatabase (config, dbPromise) {
let db = await dbPromise
// Remove expired files
let files = await db.all('SELECT * FROM File WHERE upload < ?', new Date() - (config.expiry * 1000))
if (files.length > 0) {
for (let i in files) {
@ -49,17 +51,22 @@ async function clearDatabase (config, dbPromise) {
}
async function init () {
// Load configuration
let config = await cfgLoader
let root = path.resolve(config.root)
// Check for root directory
await fs.access(root, fsa.constants.F_OK)
// Initialize database
const dbPromise = Promise.resolve()
.then(() => sqlite.open(path.join(__dirname, config.database), { Promise, cache: true }))
.then(db => db.migrate({ migrationsPath: path.join(__dirname, 'migrations') }))
await clearDatabase(config, dbPromise)
// Serve a file or a hash
// Files should be served from an external web server (such as nginx) whenever possible.
router.get('/:hash', async (req, res, next) => {
if (!req.params.hash) return res.status(400).send('Invalid request')
@ -77,6 +84,7 @@ async function init () {
res.sendFile(path.join(root, file.path))
})
// Upload a file or publish a hash
router.post('/publish', async (req, res, next) => {
let ip = req.ip
let token = req.header('token') || req.body.token