forked from oat/in-the-database-2
we uploadin files now
This commit is contained in:
parent
69d93c1f9f
commit
665a372f5e
6 changed files with 71 additions and 25 deletions
29
package-lock.json
generated
29
package-lock.json
generated
|
@ -483,6 +483,14 @@
|
|||
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.4.tgz",
|
||||
"integrity": "sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q=="
|
||||
},
|
||||
"busboy": {
|
||||
"version": "0.3.1",
|
||||
"resolved": "https://registry.npmjs.org/busboy/-/busboy-0.3.1.tgz",
|
||||
"integrity": "sha512-y7tTxhGKXcyBxRKAni+awqx8uqaJKrSFSNFSeRG5CsWNdmy2BIK+6VGWEW7TZnIO/533mtMEA4rOevQV815YJw==",
|
||||
"requires": {
|
||||
"dicer": "0.3.0"
|
||||
}
|
||||
},
|
||||
"bytes": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
|
||||
|
@ -665,6 +673,14 @@
|
|||
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
|
||||
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
|
||||
},
|
||||
"dicer": {
|
||||
"version": "0.3.0",
|
||||
"resolved": "https://registry.npmjs.org/dicer/-/dicer-0.3.0.tgz",
|
||||
"integrity": "sha512-MdceRRWqltEG2dZqO769g27N/3PXfcKl04VhYnBlo2YhH7zPi88VebsjTKclaOyiuMaGU72hTfw3VkUitGcVCA==",
|
||||
"requires": {
|
||||
"streamsearch": "0.1.2"
|
||||
}
|
||||
},
|
||||
"dir-glob": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz",
|
||||
|
@ -922,6 +938,14 @@
|
|||
"vary": "~1.1.2"
|
||||
}
|
||||
},
|
||||
"express-fileupload": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.2.0.tgz",
|
||||
"integrity": "sha512-oe4WpKcSppXnl5peornawWUa6tKmIc1/kJxMNRGJR1A0v4zyLL6VsFR6wZ8P2a4Iq3aGx8xae3Vlr+MOMQhFPw==",
|
||||
"requires": {
|
||||
"busboy": "^0.3.1"
|
||||
}
|
||||
},
|
||||
"fast-deep-equal": {
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||
|
@ -1812,6 +1836,11 @@
|
|||
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
|
||||
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
|
||||
},
|
||||
"streamsearch": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
|
||||
"integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo="
|
||||
},
|
||||
"string-width": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
"@types/express": "github:types/express",
|
||||
"@types/mongoose": "^5.7.36",
|
||||
"express": "^4.17.1",
|
||||
"express-fileupload": "^1.2.0",
|
||||
"mongoose": "^5.10.2",
|
||||
"mongoose-int32": "^0.4.1",
|
||||
"typescript": "^4.0.2",
|
||||
|
|
|
@ -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>
|
22
src/index.ts
22
src/index.ts
|
@ -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
10
src/lib/util.ts
Normal 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
11
src/upload.ts
Normal 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');
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue