-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpSession.js
124 lines (107 loc) · 4.43 KB
/
httpSession.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
const { Utils } = require("./utils");
const ByteArray = imports.byteArray;
const Gio = imports.gi.Gio;
const Soup = imports.gi.Soup;
let _httpSession;
function HttpSession() {
this._init();
}
HttpSession.prototype = {
/**
* init
*/
_init: function () {
if (Soup.MAJOR_VERSION == 2) {
_httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
} else { //version 3
_httpSession = new Soup.Session();
}
_httpSession.set_user_agent("cinnamon");
},
/**
* queryMetada
* @param {string} url - The url to call
* @param {function} callback - The function to call when url returns data
* @returns {function} - Returns the function to call
*/
queryMetada: function (url, callback) {
Utils.log('downloading metadata from ' + url);
let request = Soup.Message.new('GET', url);
if (Soup.MAJOR_VERSION === 2) {
_httpSession.queue_message(request, (_httpSession, message) => {
if (message.status_code === 200) {
callback(message.response_body.data);
} else {
Utils.log(`Failed to acquire image metadata (${message.status_code})`);
callback(false);
}
});
} else { //version 3
_httpSession.send_and_read_async(request, Soup.MessagePriority.NORMAL, null, (_httpSession, message) => {
if (request.get_status() === 200) {
const bytes = _httpSession.send_and_read_finish(message);
callback(ByteArray.toString(bytes.get_data()));
} else {
Utils.log(`Failed to acquire image metadata (${request.get_status()})`);
callback(false);
}
});
}
},
/**
* downloadImageFromUrl
* @param {string} url - The url to call
* @param {string} wallpaperPath - Path where the wallpaper is saved
* @param {function} callback - The function to call when url returns data
* @returns {function} - Returns the function to call
*/
downloadImageFromUrl: function (url, wallpaperPath, callback) {
Utils.log('downloading new image from ' + url);
let gFile = Gio.file_new_for_path(wallpaperPath);
// open the file
let fStream = gFile.replace(null, false, Gio.FileCreateFlags.NONE, null);
// create a http message
let request = Soup.Message.new('GET', url);
if (Soup.MAJOR_VERSION === 2) {
// keep track of total bytes written
let bytesTotal = 0;
// got_chunk event
request.connect('got_chunk', function (message, chunk) {
if (message.status_code === 200) { // only save the data we want, not content of 301 redirect page
bytesTotal += fStream.write(chunk.get_data(), null);
}
});
// queue the http request
_httpSession.queue_message(request, (_httpSession, message) => {
// request completed
fStream.close(null);
const contentLength = message.response_headers.get_content_length();
if (message.status_code === 200 && contentLength === bytesTotal) {
callback();
} else {
Utils.log("Error " + request.get_status() + ".Couldn't fetch image from " + url);
gFile.delete(null);
callback(false);
}
});
} else { //version 3
_httpSession.send_and_read_async(request, Soup.MessagePriority.NORMAL, null, (_httpSession, message) => {
if (request.get_status() === 200) {
const bytes = _httpSession.send_and_read_finish(message);
if (bytes && bytes.get_size() > 0) {
fStream.write(bytes.get_data(), null);
}
// request completed
fStream.close(null);
Utils.log('Download successful');
callback();
} else {
Utils.log("Error " + request.get_status() + ".Couldn't fetch image from " + url);
callback(false);
}
});
}
}
};
module.exports = { HttpSession }