You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-30 04:23:07 +03:00
Separate impl for node and extend example node app to use it.
This commit is contained in:
@ -86,6 +86,21 @@ rl.on('line', function(line) {
|
|||||||
print("/invite Error: %s", err);
|
print("/invite Error: %s", err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else if (line.indexOf("/file ") === 0) {
|
||||||
|
var filename = line.split(" ")[1].trim();
|
||||||
|
var stream = fs.createReadStream(filename);
|
||||||
|
matrixClient.uploadContent({
|
||||||
|
stream: stream,
|
||||||
|
name: filename
|
||||||
|
}).done(function(url) {
|
||||||
|
var content = {
|
||||||
|
msgtype: "m.file",
|
||||||
|
body: filename,
|
||||||
|
url: JSON.parse(url).content_uri
|
||||||
|
};
|
||||||
|
matrixClient.sendMessage(viewingRoom.roomId, content);
|
||||||
|
});
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
matrixClient.sendTextMessage(viewingRoom.roomId, line).finally(function() {
|
matrixClient.sendTextMessage(viewingRoom.roomId, line).finally(function() {
|
||||||
printMessages();
|
printMessages();
|
||||||
@ -388,4 +403,4 @@ function fixWidth(str, len) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
matrixClient.startClient(numMessagesToShow); // messages for each room.
|
matrixClient.startClient(numMessagesToShow); // messages for each room.
|
||||||
|
@ -138,6 +138,16 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload content to the Home Server
|
||||||
|
* @param {File object} file A File object (in a browser) or in Node,
|
||||||
|
an object with properties:
|
||||||
|
name: The file's name
|
||||||
|
stream: A read stream
|
||||||
|
* @param {Function} callback Optional. The callback to invoke on
|
||||||
|
* success/failure. See the promise return values for more information.
|
||||||
|
* @return {module:client.Promise} Resolves to <code>{data: {Object},
|
||||||
|
*/
|
||||||
uploadContent: function(file, callback) {
|
uploadContent: function(file, callback) {
|
||||||
if (callback !== undefined && !utils.isFunction(callback)) {
|
if (callback !== undefined && !utils.isFunction(callback)) {
|
||||||
throw Error(
|
throw Error(
|
||||||
@ -145,6 +155,7 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
var defer = q.defer();
|
var defer = q.defer();
|
||||||
|
var url = this.opts.baseUrl+"/_matrix/media/v1/upload";
|
||||||
// browser-request doesn't support File objects because it deep-copies
|
// browser-request doesn't support File objects because it deep-copies
|
||||||
// the options using JSON.parse(JSON.stringify(options)). Instead of
|
// the options using JSON.parse(JSON.stringify(options)). Instead of
|
||||||
// loading the whole file into memory as a string and letting
|
// loading the whole file into memory as a string and letting
|
||||||
@ -152,42 +163,55 @@ module.exports.MatrixHttpApi.prototype = {
|
|||||||
// use XMLHttpRequest directly.
|
// use XMLHttpRequest directly.
|
||||||
// (browser-request doesn't support progress either, which is also kind
|
// (browser-request doesn't support progress either, which is also kind
|
||||||
// of important here)
|
// of important here)
|
||||||
var xhr = new XMLHttpRequest();
|
if (global.XMLHttpRequest) {
|
||||||
var cb = requestCallback(defer, callback, this.opts.onlyData);
|
var xhr = new global.XMLHttpRequest();
|
||||||
|
var cb = requestCallback(defer, callback, this.opts.onlyData);
|
||||||
|
|
||||||
var timeout_fn = function() {
|
var timeout_fn = function() {
|
||||||
xhr.abort();
|
xhr.abort();
|
||||||
cb(new Error('Timeout'));
|
cb(new Error('Timeout'));
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.timeout_timer = setTimeout(timeout_fn, 30000);
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
|
||||||
switch (xhr.readyState) {
|
|
||||||
case XMLHttpRequest.DONE:
|
|
||||||
clearTimeout(xhr.timeout_timer);
|
|
||||||
|
|
||||||
var resp = JSON.parse(xhr.responseText);
|
|
||||||
if (resp.content_uri === undefined) {
|
|
||||||
cb(new Error('Bad response'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cb(undefined, xhr, resp.content_uri);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
xhr.upload.addEventListener("progress", function(ev) {
|
|
||||||
clearTimeout(xhr.timeout_timer);
|
|
||||||
xhr.timeout_timer = setTimeout(timeout_fn, 30000);
|
xhr.timeout_timer = setTimeout(timeout_fn, 30000);
|
||||||
defer.notify(ev);
|
|
||||||
});
|
|
||||||
var url = this.opts.baseUrl+"/_matrix/media/v1/upload";
|
|
||||||
url += "?access_token="+encodeURIComponent(this.opts.accessToken);
|
|
||||||
url += "&filename="+encodeURIComponent(file.name);
|
|
||||||
|
|
||||||
xhr.open("POST", url);
|
xhr.onreadystatechange = function() {
|
||||||
xhr.send(file);
|
switch (xhr.readyState) {
|
||||||
|
case global.XMLHttpRequest.DONE:
|
||||||
|
clearTimeout(xhr.timeout_timer);
|
||||||
|
|
||||||
|
var resp = JSON.parse(xhr.responseText);
|
||||||
|
if (resp.content_uri === undefined) {
|
||||||
|
cb(new Error('Bad response'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(undefined, xhr, resp.content_uri);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.upload.addEventListener("progress", function(ev) {
|
||||||
|
clearTimeout(xhr.timeout_timer);
|
||||||
|
xhr.timeout_timer = setTimeout(timeout_fn, 30000);
|
||||||
|
defer.notify(ev);
|
||||||
|
});
|
||||||
|
url += "?access_token="+encodeURIComponent(this.opts.accessToken);
|
||||||
|
url += "&filename="+encodeURIComponent(file.name);
|
||||||
|
|
||||||
|
xhr.open("POST", url);
|
||||||
|
xhr.send(file);
|
||||||
|
} else {
|
||||||
|
var queryParams = {
|
||||||
|
filename: file.name,
|
||||||
|
access_token: this.opts.accessToken
|
||||||
|
};
|
||||||
|
file.stream.pipe(
|
||||||
|
this.opts.request({
|
||||||
|
uri: url,
|
||||||
|
qs: queryParams,
|
||||||
|
method: "POST"
|
||||||
|
}, requestCallback(defer, callback, this.opts.onlyData))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return defer.promise;
|
return defer.promise;
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user