Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirects on avatarUrl downloading #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function uploadContentFromUrl(bridge, url, id, name) {
id = id || null;
name = name || null;
return new Promise((resolve, reject) => {
request(url, { encoding: null }, (err, res, body) => {
if (err)
return reject("Failed to download.");

const ht = url.startsWith("https") ? https : http;

ht.get((url), (res) => {
if (res.headers.hasOwnProperty("content-type")) {
contenttype = res.headers["content-type"];
} else {
Expand All @@ -48,41 +48,20 @@ function uploadContentFromUrl(bridge, url, id, name) {
const parts = url.split("/");
name = parts[parts.length - 1];
}
let size = parseInt(res.headers["content-length"]);
if (isNaN(size)) {
LogService.warn("UploadContentFromUrl", "Content-length is not valid. Assuming 512kb size");
size = 512 * 1024;
}
let buffer;
if (Buffer.alloc) {//Since 5.10
buffer = Buffer.alloc(size);
} else {//Deprecated
buffer = new Buffer(size);
}

let bsize = 0;
res.on('data', (d) => {
d.copy(buffer, bsize);
bsize += d.length;
});
res.on('error', () => {
reject("Failed to download.");
});
res.on('end', () => {
resolve(buffer);
});
resolve(body);
})
}).then((buffer) => {
if (typeof id === "string" || id == null) {
id = bridge.getIntent(id);
}
return id.getClient().uploadContent({
stream: buffer,
return id.getClient().uploadContent(buffer, {
name: name,
type: contenttype
type: contenttype,
rawResponse: false
});
}).then((response) => {
const content_uri = JSON.parse(response).content_uri;
const content_uri = response.content_uri;
LogService.info("UploadContent", "Media uploaded to " + content_uri);
return content_uri;
}).catch(function (reason) {
Expand Down