forked from SpaceSaver/TN4NJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_media_message.js
46 lines (42 loc) · 1.31 KB
/
multi_media_message.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
const Message = require("./message");
const https = require("https");
const fs = require("fs");
MUTLIMEDIA_MESSAGE_TYPE = 1;
class MultiMediaMessage extends Message {
constructor (msg_obj, outer_self) {
super(msg_obj, outer_self);
this.self = outer_self;
this.type = MUTLIMEDIA_MESSAGE_TYPE;
}
fetch() {
const myPromise = new Promise((resolve, reject) => {
const req = https.get(this.content);
req.on("response", res => {
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
this.raw_data = body;
this.content_type = res.headers["content-type"];
this.extension = this.content_type.split("/")[1];
resolve(body);
});
res.on("error", err => {
reject(err);
});
});
req.on("error", err => {
reject(err);
});
});
return myPromise;
}
mv (file_path) {
if (!file_path) {
file_path = `./file.${this.extension}`
}
fs.writeFileSync(file_path, this.raw_data, "binary");
}
}
module.exports=MultiMediaMessage;