diff --git a/README.md b/README.md index 035d73b..9ad2f7c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ # snatch-server Snatch backend node server base on expressjs. + +This is the backend of [Snatch](https://github.com/ZhichengChen/snatch), Snatch is a Sketch plugin that upload layer to CDN directly. + +## Usage + +Install the dependencies + +```bash +npm install +``` + +Once the installation is done, you can run the server: + +```bash +node index.js +``` + diff --git a/index.js b/index.js new file mode 100644 index 0000000..52b6a05 --- /dev/null +++ b/index.js @@ -0,0 +1,40 @@ +const express = require('express'); +const app = express(); +const bodyParser = require('body-parser'); +const sharp = require('sharp'); + +app.use(bodyParser.urlencoded({extended: false, limit: '50mb'})) +app.use(bodyParser.json({limit:'50mb'})) + +const boostSize = { + 'pic_wheel': [353, 353, 'png'], + 'pic_arrow': [45, 60, 'png'], + 'btn_luckydraw': [292, 77, 'png'], + 'bg_blue': [360, 640, 'jpg'] +} + +app.post('/', function(req, res) { + const size = boostSize[req.body.name]; + if (!size) { + return res.send('Name invalid!'); + } + let buff = new Buffer(req.body.image, 'base64'); + let resize = sharp(buff) + .resize(size[0], size[1]); + if (size[2]=='jpg') { + resize = resize.jpeg(); + } else { + resize = resize.png({adaptiveFiltering: true}) + } + resize.toFile(req.body.name+'.'+size[2], (err) => { + if (err) { + return res.send(err); + } else { + return res.send('Upload Success!'); + } + }); +}); + +app.listen(3000, function(){ + console.log('ok.'); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..59a7141 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "snatch-server", + "version": "1.0.0", + "description": "Snatch backend node server base on expressjs.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "chenzhicheng.com", + "license": "ISC", + "dependencies": { + "body-parser": "^1.18.3", + "express": "^4.16.4", + "sharp": "^0.22.0" + } +}