Skip to content
Extraits de code Groupes Projets
Valider 4d282953 rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Add Util function to download a file

parent c19771a0
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -6,11 +6,43 @@ import * as log from "npmlog";
import * as mime from "mime";
export class Util {
/**
* downloadFile - This function will take a URL and store the resulting data into
* a buffer.
*/
public static DownloadFile (url: string): Promise<Buffer> {
return new Promise((resolve, reject) => {
let ht;
if (url.startsWith("https")) {
ht = https;
} else {
ht = http;
}
const req = ht.get((url), (res) => {
let buffer = Buffer.alloc(0);
if (res.statusCode !== 200) {
reject(`Non 200 status code (${res.statusCode})`);
}
res.on("data", (d) => {
buffer = Buffer.concat([buffer, d]);
});
res.on("end", () => {
resolve(buffer);
});
});
req.on("error", (err) => {
reject(`Failed to download. ${err.code}`);
});
});
}
/**
* uploadContentFromUrl - Upload content from a given URL to the homeserver
* and return a MXC URL.
*/
public static uploadContentFromUrl(bridge: Bridge, url: string, id: string | Intent, name: string): Promise<any> {
public static UploadContentFromUrl(bridge: Bridge, url: string, id: string | Intent, name: string): Promise<any> {
let contenttype;
let size;
id = id || null;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter