From 73d5962c5c0495267c64ba33dbb3d98c1eb61524 Mon Sep 17 00:00:00 2001 From: oat Date: Mon, 12 Oct 2020 01:21:32 +0300 Subject: [PATCH 1/2] file uploading, now with the actual file --- .gitignore | 3 ++- public/list.html | 2 +- src/index.ts | 1 + src/lib/util.ts | 10 ---------- src/upload.ts | 22 +++++++++++++++++----- 5 files changed, 21 insertions(+), 17 deletions(-) delete mode 100644 src/lib/util.ts diff --git a/.gitignore b/.gitignore index 4acb044..efe017f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules built config/config.json *.log -.env \ No newline at end of file +.env +storage/files/* \ No newline at end of file diff --git a/public/list.html b/public/list.html index bef0d8b..f9f9604 100644 --- a/public/list.html +++ b/public/list.html @@ -18,7 +18,7 @@ const el = document.getElementById('doc-list'); for (const doc of data) { let p = document.createElement('p'); - p.innerText = `${doc.artist} - ${doc.title} by ${doc.credit}`; + p.innerHTML = `${doc.artist} - ${doc.title} by ${doc.credit} download`; el.insertAdjacentElement('beforeend', p); let charts = document.createElement('ul'); diff --git a/src/index.ts b/src/index.ts index d58c801..b4cf6ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,6 +54,7 @@ db.then(() => { app.use(express.urlencoded({extended: true})); app.use(fileUpload({limits: { fileSize: 50 * 1024 * 1024 }})); app.use(express.static('public', {extensions: ['html', 'htm']})); + app.use(express.static('storage', {extensions: ['zip']})); app.use('/assets', express.static('assets')); app.set('db', db); diff --git a/src/lib/util.ts b/src/lib/util.ts deleted file mode 100644 index 77a0c60..0000000 --- a/src/lib/util.ts +++ /dev/null @@ -1,10 +0,0 @@ -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); - }); - }; -} \ No newline at end of file diff --git a/src/upload.ts b/src/upload.ts index 98006c6..1a57841 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -23,7 +23,7 @@ export function run(app) { storeEntries: true }); - zip.on('ready', () => { + zip.on('ready', async () => { const smFile = Object.values(zip.entries()).find((f: any) => !f.isDirectory && (f.name.endsWith('.sm')) ); @@ -36,11 +36,23 @@ export function run(app) { logger.info(`${chart.artist} - ${chart.title} was just uploaded`); - const file = new File(chart); - file.save(); + let id = 0; + for (const f of await File.find({})) { + id = Math.max(f.id, id); + } + chart.id = id + 1; - // TODO: filter out _id and __v? possibly more - res.send(chart); + chart.createdAt = new Date(); + + fs.writeFile('./storage/files/' + (id + 1) + '.zip', file.data, (err) => { + if (err) throw err; + + const file = new File(chart); + file.save(); + + // TODO: filter out _id and __v? possibly more + res.send(chart); + }); } zip.close(); From 6b07ef943d99fe6ad617029fc33b45c1517c36b8 Mon Sep 17 00:00:00 2001 From: oat Date: Mon, 12 Oct 2020 01:24:33 +0300 Subject: [PATCH 2/2] fix up html formatting --- public/index.html | 16 ++++++------- public/list.html | 58 +++++++++++++++++++++++----------------------- public/upload.html | 2 +- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/public/index.html b/public/index.html index a9d09dd..e7190fe 100644 --- a/public/index.html +++ b/public/index.html @@ -1,14 +1,14 @@ - - - - Index - + + + + Index + - -

Hi

- + +

Hi

+ \ No newline at end of file diff --git a/public/list.html b/public/list.html index f9f9604..258bb27 100644 --- a/public/list.html +++ b/public/list.html @@ -1,37 +1,37 @@ - - - - list - + + + + list + - -
-
+ +
+
- - + - + let charts = document.createElement('ul'); + for (const chart of doc.charts) { + let l = document.createElement('li'); + l.innerText = `${chart.difficulty} ${chart.rating} - ${chart.name}\n` + + `${chart.steps} steps, ${chart.mines} mines, ${chart.jumps} jumps, ${chart.hands} hands, ${chart.holds} holds, ${chart.rolls} rolls` + charts.insertAdjacentElement('beforeend', l); + } + el.insertAdjacentElement('beforeend', charts); + } + }); + + \ No newline at end of file diff --git a/public/upload.html b/public/upload.html index 850b5b3..13b5845 100644 --- a/public/upload.html +++ b/public/upload.html @@ -19,7 +19,7 @@ const file = document.getElementById('file'); if (file.files.length) { console.log(file.files[0]); - + const formData = new FormData(); formData.append('file', file.files[0]); try {