From f801b5f696a3c08d1c7836260111d698c5af01bd Mon Sep 17 00:00:00 2001 From: joeyklee Date: Mon, 29 Apr 2019 11:40:56 -0400 Subject: [PATCH] added export function --- .gitignore | 3 ++- index.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8cc39fd..5402818 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store node_modules -db/* \ No newline at end of file +db/* +exports/* \ No newline at end of file diff --git a/index.js b/index.js index 9a0ef81..1b2baae 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const express = require('express'); const logger = require('morgan'); const path = require('path'); const http = require('http'); +const fs = require('fs'); const port = process.env.PORT || 3030; const Datastore = require('nedb'); const pathToData = path.resolve(__dirname, "db/db") @@ -67,6 +68,25 @@ app.post("/api", (req, res) => { }); }) +/** + * + * Export all images to PNG on the server + * */ +app.get("/export/all", (req, res) => { + db.find({}).sort({'created':1}).exec(function (err, docs) { + if(err){ + return err; + } + docs.forEach(item => { + const outImage = item.image.replace(/^data:image\/png;base64,/, ''); + fs.writeFileSync(path.resolve(__dirname, `./exports/${item.created}.png`) , outImage, 'base64'); + console.log('writing ', `${item.created}.png`) + }) + res.send('done!') + }); +}) + + // use the http module to create an http server listening on the specified port http.createServer(app).listen(port, () =>{