we uploadin files now

This commit is contained in:
oat 2020-09-02 19:03:49 +03:00
parent 69d93c1f9f
commit 665a372f5e
Signed by untrusted user who does not match committer: oat
GPG key ID: DD83A9617A252385
6 changed files with 71 additions and 25 deletions

View file

@ -1,16 +1,23 @@
<html>
<head>
<title>file upload</title>
<script>
async function upload() {
let file = document.getElementById('file');
console.log(file.files[0]);
await fetch('/upload', {
method: 'POST',
body: file.files[0]
});
}
</script>
</head>
<body>
<form action="/upload" method="post">
<label for="title">Title: </label><br>
<input type="text" id="title" name="title"><br>
<label for="artist">Artist: </label><br>
<input type="text" id="artist" name="artist"><br>
<label for="credit">Credit: </label><br>
<input type="text" id="credit" name="credit"><br>
<input type="submit" value="upload">
<form method="post" encType="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="submit" value="upload" onclick="upload()">
</form>
</body>
</html>

View file

@ -2,10 +2,13 @@ import * as express from 'express';
import * as mongoose from 'mongoose';
import * as fs from 'fs';
import * as winston from 'winston';
import * as fileUpload from 'express-fileupload';
import * as format from './lib/format';
import { File } from './schema';
import * as upload from './upload';
const config = JSON.parse(fs.readFileSync('./config/config.json', {encoding: 'utf8'}));
const db = mongoose.connect(`${config.dbconnectionURL}/${config.dbname}`, {
@ -37,15 +40,6 @@ const logger = winston.createLogger({
]
});
function returnStatic(page) {
return (req, res) => {
fs.readFile(`src/html/${page}`, 'utf8', (err, data) => {
if (err) throw err;
res.send(data);
});
};
}
logger.info('connecting to mongodb database');
db.then(() => {
logger.info('connected to database!');
@ -54,19 +48,13 @@ db.then(() => {
// @ts-ignore
app.use(express.urlencoded({extended: true}));
app.use(fileUpload({limits: { fileSize: 50 * 1024 * 1024 }}));
app.set('db', db);
app.set('config', config);
app.set('logger', logger);
app.get('/upload', returnStatic('upload.html'));
app.post('/upload', async (req, res) => { // only for testing, very abusable
const file = new File(req.body);
await file.save();
res.send('uploaded file');
});
upload.run(app);
app.get('/list', async (req, res) => { // only for testing
const docs = await File.find({});

10
src/lib/util.ts Normal file
View file

@ -0,0 +1,10 @@
import * as fs from 'fs';
export function returnStatic(page) {
return (req, res) => {
fs.readFile(`src/html/${page}`, 'utf8', (err, data) => {
if (err) throw err;
res.send(data);
});
};
}

11
src/upload.ts Normal file
View file

@ -0,0 +1,11 @@
import { returnStatic } from './lib/util';
export function run(app) {
app.get('/upload', returnStatic('upload.html'));
app.post('/upload', async (req, res) => { // only for testing, very abusable
console.log(req.files);
res.send('ok, i got something');
});
}