-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (76 loc) · 2.65 KB
/
server.js
File metadata and controls
97 lines (76 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var express = require('express')
var serveStatic = require('serve-static')
var path = require('path')
var fetch = require('isomorphic-fetch')
var fs = require('fs')
var IPFS = require('ipfs')
var ipfsNode = new IPFS()
var app = express()
app.use(serveStatic(path.join(__dirname, 'src')))
app.use(serveStatic(path.join(__dirname, 'build/contracts')))
// Example file: https://www.figma.com/file/RuXvVnnTvSIkccoz1ANOvEJE/Emojis
const figmaApiKey = "<your Figma API token here"
app.use('/get_images', async function(req, res, next) {
let result = await getImagesFromFigma(req.query.figmaId)
res.send(JSON.stringify(result))
})
async function getImagesFromFigma(figmaId) {
let result = await fetch('https://api.figma.com/v1/files/' + figmaId, {
method: 'GET',
headers: {
'X-Figma-Token': figmaApiKey
}
})
let figmaTreeStructure = await result.json()
//let's only keep nodes we want
//we'll only keep nodes in the 'emojis' page of our Figma file and only nodes that are frames and of size 500x500
let ethmojis = figmaTreeStructure.document.children
.filter(child => child.name.toLowerCase() == 'emojis')[0].children
.filter(child => child.type == 'FRAME' && child.absoluteBoundingBox.width == 500 && child.absoluteBoundingBox.height == 500)
.map(frame => {
return {
name: frame.name,
id: frame.id
}
})
//we'll call the /images API endpoint with batched ids
let ids = ethmojis.map(ethmoji => ethmoji.id).join(',')
let imageResult = await fetch('https://api.figma.com/v1/images/' + figmaId + '?scale=2&ids=' + ids, {
method: 'GET',
headers: {
'X-Figma-Token': figmaApiKey
}
})
let figmaImages = await imageResult.json()
figmaImages = figmaImages.images
//lets format these images as a hash of name : imageUrl
return ethmojis.reduce(function(map, ethmoji) {
map[ethmoji.name] = figmaImages[ethmoji.id]
return map
}, {})
}
app.use('/upload_to_ipfs', async function(req, res, next) {
//let's first download the image into a temporary folder
fetch(req.query.imageUrl)
.then(image => {
let filename = './tmp/' + req.query.name + '.png'
const fileStream = fs.createWriteStream(filename)
image.body.pipe(fileStream)
fileStream.on('finish', function() {
let uploadImage = {
path: req.query.name,
content: fs.createReadStream(filename)
}
ipfsNode.files.add(uploadImage, function(err, ipfsFile) {
if (!err) {
res.send({
'ipfsHash': ipfsFile[0].hash
})
} else {
console.log(err)
}
})
})
})
})
app.listen(3000)