From 376cf10663f76d99721ee8596634488868482606 Mon Sep 17 00:00:00 2001 From: Pitu Date: Fri, 13 Jan 2017 04:34:21 -0300 Subject: [PATCH] First version --- .eslintrc.json | 30 +++++++++++++++++++ .gitignore | 4 +++ README.md | 5 ++++ controllers/uploadController.js | 49 ++++++++++++++++++++++++++++++++ db | Bin 0 -> 16384 bytes lolisafe.js | 31 ++++++++++++++++++++ package.json | 25 ++++++++++++++++ routes.js | 6 ++++ 8 files changed, 150 insertions(+) create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 controllers/uploadController.js create mode 100644 db create mode 100644 lolisafe.js create mode 100644 package.json create mode 100644 routes.js diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..50c00fb --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,30 @@ +{ + "env": { + "es6": true, + "node": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + "tab" + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "never" + ], + "no-unused-vars": "warn", + "no-console": "off" + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d59998 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +uploads/ +logs/ +config.js \ No newline at end of file diff --git a/README.md b/README.md index 1284b92..96f6abd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ +<<<<<<< HEAD +# loli-bank +Pomf-like image uploading service, written in NodeJS +======= # loli-safe Pomf-like file uploading service, written in NodeJS +>>>>>>> b285f244150d0eb79376716501a97b9416497344 diff --git a/controllers/uploadController.js b/controllers/uploadController.js new file mode 100644 index 0000000..b39e702 --- /dev/null +++ b/controllers/uploadController.js @@ -0,0 +1,49 @@ +const path = require('path') +const config = require('../config.js') +const multer = require('multer') +const randomstring = require('randomstring') +const db = require('knex')(config.database) + +let uploadsController = {} + +const storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, './' + config.uploads.folder + '/') + }, + filename: function (req, file, cb) { + cb(null, randomstring.generate(config.fileLength) + path.extname(file.originalname)) + } +}) + +const upload = multer({ + storage: storage, + limits: { fileSize: config.uploads.maxsize } +}).single('file') + +uploadsController.upload = function(req, res, next){ + + let gallery = req.headers.gallery + + if(!config.privacy.public) + if(!config.privacy.IPs.includes(req.ip)) return res.status(401).send('Not Authorized!') + + upload(req, res, function (err) { + if (err) { + console.error(err) + return res.json({ error: err }) + } + + db.table('files').insert({ + file: req.file.filename, + galleryid: gallery + }).then(() => { + res.json({ + 'filename': req.file.filename + }) + }) + + }) + +} + +module.exports = uploadsController \ No newline at end of file diff --git a/db b/db new file mode 100644 index 0000000000000000000000000000000000000000..f8f2730750bd5894c16b2c77ad34b40ab8ec5774 GIT binary patch literal 16384 zcmeI&!A`<390u@q0~Zr<-gY1+%}fjgLyV|r(V#KmAc{dxrb5B#HeeJI;tAi%H}DC3 z0-wOdyX}MoF!A8Y;MZ)lU$=GL@Agl)sFXb^ShF1kT(SaLBb1T@#t0#nIuq)gG(!!h zGo4)cp|?n$UZvCx=ISG{Yj#p~Xb^w^1Rwwb2tWV=5P$##An?BgMwV$gjzdR*cUR@dgL=tqM91Rwwb2tWV=5P$##AOHafKwyCdbp225526k<2tWV=5P$## zAOHafKmY;|fB*y*Nnn|p4vqg-k<|5n(talPyZvUr+AnH|1_1~_00Izz00bZa0SG_< z0uX?}UlB+p6J(v1yT>Bm30uZWLPa76_a{28m^#(YDt?>{opw(P`*ET=U2gAQ>GTP5 CiK4Xt literal 0 HcmV?d00001 diff --git a/lolisafe.js b/lolisafe.js new file mode 100644 index 0000000..f99df78 --- /dev/null +++ b/lolisafe.js @@ -0,0 +1,31 @@ +const config = require('./config.js') +const routes = require('./routes.js') +const express = require('express') +const db = require('knex')(config.database) +const fs = require('fs') +const safe = express() + +fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder) +fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder) +fs.existsSync('db') || fs.writeFile('db', '') + +safe.use('/', express.static('./uploads')) +safe.use('/api' , routes) +safe.use('/panel', express.static('./dashboard')) + +// Create the tables we need to store galleries and files +db.schema.createTableIfNotExists('gallery', function (table) { + table.increments() + table.string('name') + table.timestamps() +}).then(() => {}) + +db.schema.createTableIfNotExists('files', function (table) { + table.increments() + table.string('file') + table.integer('galleryid') +}).then(() => {}) + +safe.enable('trust proxy') + +safe.listen(config.port, () => console.log(`loli-safe started on port ${config.port}`)) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f4ce9e4 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "loli-safe", + "version": "1.0.0", + "description": "Pomf-like uploading service, written in NodeJS", + "author": "kanadeko", + "main": "lolibank.js", + "repository": { + "type": "git", + "url": "https://github.com/kanadeko/loli-safe" + }, + "bugs": { + "url": "https://github.com/kanadeko/loli-safe/issues" + }, + "engines": { + "node": ">=7.0.0" + }, + "license": "MIT", + "dependencies": { + "express": "^4.14.0", + "knex": "^0.12.6", + "multer": "^1.2.1", + "randomstring": "^1.1.5", + "sqlite3": "^3.1.8" + } +} diff --git a/routes.js b/routes.js new file mode 100644 index 0000000..60b0090 --- /dev/null +++ b/routes.js @@ -0,0 +1,6 @@ +const routes = require('express').Router() +const uploadController = require('./controllers/uploadController') + +routes.post ('/upload', (req, res, next) => uploadController.upload(req, res, next)) + +module.exports = routes \ No newline at end of file