This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
212 lines (192 loc) · 6.57 KB
/
app.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const path = require('path')
// get settings
try {
var settings = require('./settings.json')
} catch (e) {
var settings = {}
}
// setup server
const express = require('express')
const app = express()
app.set('views', './views')
app.set('view engine', 'pug')
app.use('/images', express.static('images'))
const server = app.listen(settings.port || 3000)
// setup database
const Datastore = require('nedb')
const pages = new Datastore({
filename: path.join(__dirname, 'pages.db'),
autoload: true
})
// setup image
const gm = require('gm')
const IMAGE_WIDTH = 540 * 3
const IMAGE_HEIGHT = 287 * 3
// setup constants
const BASE_URL = (process.env.USER === 'user')
? 'http://localhost:' + settings.port
: settings.publicUrl
const FACEBOOK_SHARE_URL = 'https://www.facebook.com/sharer/sharer.php?u='
// show sharing page for the facebook bot
// redirect to index everybody else
app.get('/', function (request, response) {
return response.render('index', getMainPageProps())
})
// handle popup window – redirect to share dialog
app.get('/popup', function (request, response) {
var rectanglePosition
try {
rectanglePosition = getQueryValues(request.query)
} catch (error) {
return response.send(error)
}
createOrFindPage(rectanglePosition, request.headers, function (error, pageId) {
if (error) {
console.error(error)
return response.send('server error')
}
const facebookUrl = FACEBOOK_SHARE_URL + encodeURIComponent(BASE_URL + '/share/' + pageId)
return response.redirect(facebookUrl)
})
})
// open share page
app.get('/share/:pageId', function (request, response) {
if (!request.params.pageId) return response.send('no pageId')
// if (
// request.headers['user-agent'].indexOf('bot') >= 0 ||
// request.headers['user-agent'].indexOf('facebook') >= 0 ||
// request.headers['user-agent'].indexOf('twitter') >= 0 ||
// request.headers['user-agent'].indexOf('vkontakte') >= 0
// ) {
return response.render('share', getSharePageProps(request.params.pageId))
// } else {
// return response.redirect('/')
// }
})
// redirect all other routes to the main page
app.get('*', function (request, response) {
return response.redirect('/')
})
function getQueryValues (query) {
if (
query == null ||
typeof query.windowWidth !== 'string' ||
typeof query.windowHeight !== 'string' ||
typeof query.top !== 'string' ||
typeof query.left !== 'string' ||
typeof query.width !== 'string' ||
typeof query.height !== 'string'
) throw new Error('invalid arguments')
let values = {
windowWidth: parseInt(query.windowWidth),
windowHeight: parseInt(query.windowHeight),
top: parseInt(query.top),
left: parseInt(query.left),
width: parseInt(query.width),
height: parseInt(query.height)
}
if (
values.windowWidth <= 0 ||
values.windowHeight <= 0 ||
values.top < 0 ||
values.left < 0 ||
values.width <= 0 ||
values.height <= 0
) throw new Error('invalid arguments')
if (values.left + values.width > values.windowWidth) {
values.windowWidth = values.left + values.width
}
if (values.top + values.height > values.windowHeight) {
values.windowHeight = values.top + values.height
}
return values
}
function getMainPageProps () {
return {
imageUrl: BASE_URL + '/images/index.png',
pageUrl: BASE_URL,
pageTitle: settings.indexPageTitle,
pageDescription: settings.pageDescription,
imageWidth: 1080,
imageHeight: 556,
gaClientId: settings.gaClientId,
}
}
function getSharePageProps (id) {
const imageUrl = BASE_URL + '/images/' + id + '.png'
return {
imageUrl: imageUrl,
pageUrl: BASE_URL + '/share/' + id,
pageTitle: settings.sharePageTitle,
pageDescription: settings.pageDescription,
imageWidth: IMAGE_WIDTH,
imageHeight: IMAGE_HEIGHT,
gaClientId: settings.gaClientId,
}
}
function createOrFindPage (rectanglePosition, headers, callback) {
// get image rectangle coordinates
const rectangleImageCoordinates = getRectangleImageCoordinates(rectanglePosition)
// search by those coordinates if this image already exists
pages.findOne(rectangleImageCoordinates).exec(function (error, found) {
if (error) return callback(error)
const now = Date.now()
// create object with client information
const client = {
originalSizes: rectanglePosition,
userIp: headers['x-forwarded-for'],
userAgent: headers['user-agent'],
timestamp: now
}
// if image exist, return it's id
if (found) {
pages.update({_id: found._id}, {
$push: {clients: client},
$set: {updatedAt: now}
}, function (error, result) {
if (error) console.error(error)
})
if (callback) return callback(null, found._id)
}
// if not, create new one and generate image
rectangleImageCoordinates.clients = [client]
rectangleImageCoordinates.createdAt = now
pages.insert(rectangleImageCoordinates, function (error, inserted) {
if (error) return callback(error)
// generate new image and return it's id on complete
generateImage(inserted._id, rectangleImageCoordinates, function (error, success) {
if (error) return callback(error)
if (callback) return callback(null, inserted._id)
})
})
})
}
function getRectangleImageCoordinates (rectanglePosition) {
const widthProportion = rectanglePosition.windowWidth / IMAGE_WIDTH
const height = rectanglePosition.windowHeight / widthProportion
const heightProportion = (height > IMAGE_HEIGHT) ? (height / IMAGE_HEIGHT) : 1
const correction = widthProportion * heightProportion
const leftShift = (height > IMAGE_HEIGHT) ? (IMAGE_WIDTH - rectanglePosition.windowWidth / correction) / 2 : 0
const topShift = (height < IMAGE_HEIGHT) ? (IMAGE_HEIGHT - rectanglePosition.windowHeight / correction) / 2 : 0
return {
x1: Math.floor(rectanglePosition.left / correction + leftShift),
y1: Math.floor(rectanglePosition.top / correction + topShift),
x2: Math.floor((rectanglePosition.left + rectanglePosition.width) / correction + leftShift),
y2: Math.floor((rectanglePosition.top + rectanglePosition.height) / correction + topShift)
}
}
function generateImage (pageId, rectangleImageCoordinates, callback) {
const filepath = path.join(__dirname, 'images', pageId) + '.png'
gm(IMAGE_WIDTH, IMAGE_HEIGHT, '#000000')
.fill('#ffffff')
.drawRectangle(
rectangleImageCoordinates.x1,
rectangleImageCoordinates.y1,
rectangleImageCoordinates.x2,
rectangleImageCoordinates.y2
)
.write(filepath, function (error) {
if (error) return callback(error)
callback(null, pageId)
})
}