Nextrek
Diff Revisions
1126
vs
1127
for /socketIO/chat.js
|
@@ -1,3 +1,6 @@ |
|
1 |
+ |
const fs = require('fs'); |
|
2 |
+ |
const sharp = require('sharp'); |
|
3 |
+ |
|
1 |
4 |
|
const CHAT = Symbol.for("My.App.Namespace.chat"); |
2 |
5 |
|
|
3 |
6 |
|
var globalSymbols = Object.getOwnPropertySymbols(global); |
|
@@ -126,6 +129,43 @@ |
126 |
129 |
|
} |
127 |
130 |
|
}, |
128 |
131 |
|
|
|
132 |
+ |
storeImage(data, user) { |
|
133 |
+ |
return new Promise((resolve, reject) => { |
|
134 |
+ |
const matchExtension = /^data:image\/(\w+)/i.exec(data.content); |
|
135 |
+ |
if (matchExtension === null) { |
|
136 |
+ |
reject('Invalid data type'); |
|
137 |
+ |
} |
|
138 |
+ |
|
|
139 |
+ |
const extension = matchExtension[1]; |
|
140 |
+ |
const filename = `${user.room}_${Date.now()}.${extension}`; |
|
141 |
+ |
|
|
142 |
+ |
console.log('user ' + user.room + ' saving file name: ' + filename); |
|
143 |
+ |
|
|
144 |
+ |
const dir = `./public/imagesFull/${user.room}`; |
|
145 |
+ |
const dir2 = `./public/imagesThumb/${user.room}`; |
|
146 |
+ |
if (!fs.existsSync(dir)){ |
|
147 |
+ |
fs.mkdirSync(dir); |
|
148 |
+ |
} |
|
149 |
+ |
if (!fs.existsSync(dir2)){ |
|
150 |
+ |
fs.mkdirSync(dir2); |
|
151 |
+ |
} |
|
152 |
+ |
|
|
153 |
+ |
const base64Data = data.content.replace(/^data:image\/\w+;base64,/, ''); |
|
154 |
+ |
|
|
155 |
+ |
fs.writeFile(dir +"/"+filename, base64Data, 'base64', function(err) { |
|
156 |
+ |
console.log(err); |
|
157 |
+ |
}); |
|
158 |
+ |
|
|
159 |
+ |
let imgBuffer = Buffer.from(base64Data, 'base64'); |
|
160 |
+ |
|
|
161 |
+ |
sharp(imgBuffer) |
|
162 |
+ |
.resize(160, 120) |
|
163 |
+ |
.toFile(dir2 + "/" + filename, (err, info) => { |
|
164 |
+ |
resolve(filename); |
|
165 |
+ |
}); |
|
166 |
+ |
}) |
|
167 |
+ |
}, |
|
168 |
+ |
|
129 |
169 |
|
event(type, data) { |
130 |
170 |
|
return { |
131 |
171 |
|
type, |
|
@@ -135,12 +175,33 @@ |
135 |
175 |
|
}, |
136 |
176 |
|
|
137 |
177 |
|
message(data, room, from) { |
138 |
|
- |
return { |
139 |
|
- |
from, |
140 |
|
- |
room: room, |
141 |
|
- |
content: data.content, |
142 |
|
- |
contentType: data.contentType |
143 |
|
- |
} |
|
178 |
+ |
return new Promise((resolve, reject) => { |
|
179 |
+ |
switch (data.contentType) { |
|
180 |
+ |
case 'IMAGE': |
|
181 |
+ |
this.storeImage(data, from) |
|
182 |
+ |
.then(filename => { |
|
183 |
+ |
resolve({ |
|
184 |
+ |
from, |
|
185 |
+ |
room: room, |
|
186 |
+ |
content: filename, |
|
187 |
+ |
contentType: data.contentType |
|
188 |
+ |
}); |
|
189 |
+ |
}) |
|
190 |
+ |
.catch(error => { |
|
191 |
+ |
data.content = error; |
|
192 |
+ |
}); |
|
193 |
+ |
break; |
|
194 |
+ |
|
|
195 |
+ |
default: |
|
196 |
+ |
resolve({ |
|
197 |
+ |
from, |
|
198 |
+ |
room: room, |
|
199 |
+ |
content: data.content, |
|
200 |
+ |
contentType: data.contentType |
|
201 |
+ |
}); |
|
202 |
+ |
break; |
|
203 |
+ |
} |
|
204 |
+ |
}); |
144 |
205 |
|
} |
145 |
206 |
|
}; |
146 |
207 |
|
} |