Skip to content

Commit cfaf9b9

Browse files
author
Zhicheng Chen
committed
init
1 parent fb86f3f commit cfaf9b9

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# snatch-server
22
Snatch backend node server base on expressjs.
3+
4+
This is the backend of [Snatch](https://github.com/ZhichengChen/snatch), Snatch is a Sketch plugin that upload layer to CDN directly.
5+
6+
## Usage
7+
8+
Install the dependencies
9+
10+
```bash
11+
npm install
12+
```
13+
14+
Once the installation is done, you can run the server:
15+
16+
```bash
17+
node index.js
18+
```
19+

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const express = require('express');
2+
const app = express();
3+
const bodyParser = require('body-parser');
4+
const sharp = require('sharp');
5+
6+
app.use(bodyParser.urlencoded({extended: false, limit: '50mb'}))
7+
app.use(bodyParser.json({limit:'50mb'}))
8+
9+
const boostSize = {
10+
'pic_wheel': [353, 353, 'png'],
11+
'pic_arrow': [45, 60, 'png'],
12+
'btn_luckydraw': [292, 77, 'png'],
13+
'bg_blue': [360, 640, 'jpg']
14+
}
15+
16+
app.post('/', function(req, res) {
17+
const size = boostSize[req.body.name];
18+
if (!size) {
19+
return res.send('Name invalid!');
20+
}
21+
let buff = new Buffer(req.body.image, 'base64');
22+
let resize = sharp(buff)
23+
.resize(size[0], size[1]);
24+
if (size[2]=='jpg') {
25+
resize = resize.jpeg();
26+
} else {
27+
resize = resize.png({adaptiveFiltering: true})
28+
}
29+
resize.toFile(req.body.name+'.'+size[2], (err) => {
30+
if (err) {
31+
return res.send(err);
32+
} else {
33+
return res.send('Upload Success!');
34+
}
35+
});
36+
});
37+
38+
app.listen(3000, function(){
39+
console.log('ok.');
40+
});

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "snatch-server",
3+
"version": "1.0.0",
4+
"description": "Snatch backend node server base on expressjs.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "chenzhicheng.com",
10+
"license": "ISC",
11+
"dependencies": {
12+
"body-parser": "^1.18.3",
13+
"express": "^4.16.4",
14+
"sharp": "^0.22.0"
15+
}
16+
}

0 commit comments

Comments
 (0)