diff options
| -rw-r--r-- | lib/db/index.js | 10 | ||||
| -rw-r--r-- | lib/index.js | 31 | ||||
| -rw-r--r-- | lib/upload.js | 10 | ||||
| -rw-r--r-- | migrations/20171225172732_setup.js | 4 |
4 files changed, 48 insertions, 7 deletions
diff --git a/lib/db/index.js b/lib/db/index.js index 75ee2ad..9720235 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -8,7 +8,11 @@ var knex = connection.knex /* MODELS */ var Image = db.Image = bookshelf.Model.extend({ - tableName: 'images', + tableName: 'plops', + hasTimestamps: true, +}) +var ShaderImage = db.ShaderImage = bookshelf.Model.extend({ + tableName: 'im_cmd', hasTimestamps: false, }) @@ -26,7 +30,9 @@ db.getRandom = function () { qb.orderBy(knex.raw('RAND()')).limit(1) }).fetch() } - db.createImage = function(url){ return new Image({ url: url }).save() } +db.createShaderImage = function(data){ + return new ShaderImage(data).save() +} diff --git a/lib/index.js b/lib/index.js index ef1aa5c..6bdb722 100644 --- a/lib/index.js +++ b/lib/index.js @@ -70,5 +70,36 @@ site.init = function(){ } }) }) + + /* shaderblaster / asciiblaster apis */ + app.post("/cgi-bin/im/shader/upload", multer_upload.single('qqfile'), function(req, res){ + var now = Math.floor(Date.now() / 1000) + var filename = sanitize(req.body.filename) + '_' + now + '.' + upload.getExtension() + var path = '/im/ab/' + upload.put("shader", req.file, { + filename, path, + unacceptable: function(err){ + res.json({ error: err }) + }, + success: function(url){ + db.createShaderImage({ + // date, name, dir, newfile, tag, remote_addr + date: now, + name: sanitize(req.body.username), + dir: 'ab', + newfile: filename, + tag: sanitize(req.body.tag) || 'plop', + remote_addr: req.ip || '127.0.0.1', + }).then(function(image){ + res.json(image) + }) + } + }) + }) + + /* public urls last */ app.use('/p/', express.static(path.join(__dirname, '../public'))) } + +function sanitize (s){ return (s || "").replace(new RegExp("[^-_a-zA-Z0-9]", 'g'), "") } + diff --git a/lib/upload.js b/lib/upload.js index f0ea132..82ea9b5 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -15,6 +15,10 @@ var acceptableuploadTypes = { 'image/png': 'png' } +module.exports.getExtension = function(file) { + return acceptableuploadTypes[file.mimetype] +} + module.exports.put = function (key, file, opt) { var fileSize, fileType, filename var err @@ -22,10 +26,10 @@ module.exports.put = function (key, file, opt) { var ts = moment().format('YYYYMMDD') - var extension = acceptableuploadTypes[file.mimetype] - filename = crypto.createHash('md5').update(file.buffer).digest('hex') + "." + extension; + var extension = this.getExtension(file) + filename = opt.filename || crypto.createHash('md5').update(file.buffer).digest('hex') + "." + extension; - var remote_path = process.env.S3_DIR + filename + var remote_path = (opt.path || process.env.S3_DIR) + filename if (! extension) { err = "Unacceptable filetype" diff --git a/migrations/20171225172732_setup.js b/migrations/20171225172732_setup.js index 430f778..3e25d90 100644 --- a/migrations/20171225172732_setup.js +++ b/migrations/20171225172732_setup.js @@ -1,7 +1,7 @@ exports.up = function(knex, Promise) { - return knex.schema.createTable('images', function (table) { + return knex.schema.createTable('plops', function (table) { table.increments(); table.string('url'); table.string('ip'); @@ -11,5 +11,5 @@ exports.up = function(knex, Promise) { }; exports.down = function(knex, Promise) { - return knex.schema.dropTable('images') + return knex.schema.dropTable('plops') }; |
