-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhicheng Chen
committed
Mar 25, 2019
1 parent
fb86f3f
commit cfaf9b9
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |