mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 21:06:31 +03:00
Add demo server for http upload feature
This commit is contained in:
4
upload-server/.gitignore
vendored
Normal file
4
upload-server/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
uploads
|
||||||
|
node_modules
|
||||||
|
yarn*
|
||||||
|
.gitfoo
|
||||||
17
upload-server/README.md
Normal file
17
upload-server/README.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# deltachat-upload-server
|
||||||
|
|
||||||
|
Demo server for the HTTP file upload feature.
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
npm install
|
||||||
|
node server.js
|
||||||
|
```
|
||||||
|
|
||||||
|
Configure with environment variables:
|
||||||
|
* `UPLOAD_PATH`: Path to upload files to (default: `./uploads`)
|
||||||
|
* `PORT`: Port to listen on (default: `8080`)
|
||||||
|
* `HOSTNAME`: Hostname to listen on (default: `0.0.0.0`)
|
||||||
|
* `BASEURL`: Base URL for generated links (default: `http://[hostname]:[port]/`)
|
||||||
|
|
||||||
12
upload-server/package.json
Normal file
12
upload-server/package.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"name": "deltachat-upload-server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "server.js",
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node server.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.17.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
53
upload-server/server.js
Normal file
53
upload-server/server.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const p = require('path')
|
||||||
|
const crypto = require('crypto')
|
||||||
|
const express = require('express')
|
||||||
|
const fs = require('fs')
|
||||||
|
const { pipeline } = require('stream')
|
||||||
|
|
||||||
|
const app = express()
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
path: process.env.UPLOAD_PATH || p.resolve('./uploads'),
|
||||||
|
port: process.env.PORT || 8080,
|
||||||
|
hostname: process.env.HOSTNAME || '0.0.0.0',
|
||||||
|
baseurl: process.env.BASE_URL || null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.baseurl.endsWith('/')) config.baseurl = config.baseurl + '/'
|
||||||
|
|
||||||
|
if (!fs.existsSync(config.path)) {
|
||||||
|
fs.mkdirSync(config.path, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseUrl = config.baseurl || `http://${config.hostname}:${config.port}/`
|
||||||
|
|
||||||
|
app.post('*', (req, res) => {
|
||||||
|
const filename = crypto.randomBytes(12).toString('hex')
|
||||||
|
const ws = fs.createWriteStream(p.join(config.path, filename))
|
||||||
|
pipeline(req, ws, err => {
|
||||||
|
if (err) console.error(err)
|
||||||
|
if (err) res.status(500).send(err.message)
|
||||||
|
const url = baseUrl + filename
|
||||||
|
console.log('file uploaded: ' + filename)
|
||||||
|
res.send(url)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/:filename', (req, res) => {
|
||||||
|
const filepath = p.normalize(p.join(config.path, req.params.filename))
|
||||||
|
if (!filepath.startsWith(config.path)) {
|
||||||
|
return res.code(500).send('bad request')
|
||||||
|
}
|
||||||
|
const rs = fs.createReadStream(filepath)
|
||||||
|
res.setHeader('content-type', 'application/octet-stream')
|
||||||
|
pipeline(rs, res, err => {
|
||||||
|
if (err) console.error(err)
|
||||||
|
if (err) res.status(500).send(err.message)
|
||||||
|
res.end()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.listen(config.port, err => {
|
||||||
|
if (err) console.error(err)
|
||||||
|
else console.log('Listening on ' + baseUrl)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user