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 0000000..f8f2730 Binary files /dev/null and b/db differ 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