Fix attachment ids
Parse attachment ids out of the attachment pointer url and return them as strings because the copy parsed by JSON suffers a loss of precision. Convert them to and from the format expected by the protobuf using facilities from decodeIO.Long.
This commit is contained in:
parent
eebb14599f
commit
211129475c
3 changed files with 26 additions and 21 deletions
43
js/api.js
43
js/api.js
|
@ -27,6 +27,7 @@ window.textsecure.api = function() {
|
||||||
// Staging server
|
// Staging server
|
||||||
var URL_BASE = "https://textsecure-service-staging.whispersystems.org";
|
var URL_BASE = "https://textsecure-service-staging.whispersystems.org";
|
||||||
self.relay = "textsecure-service-staging.whispersystems.org";
|
self.relay = "textsecure-service-staging.whispersystems.org";
|
||||||
|
var ATTACHMENT_HOST = "whispersystems-textsecure-attachments-staging.s3.amazonaws.com"
|
||||||
|
|
||||||
// This is the real server
|
// This is the real server
|
||||||
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
||||||
|
@ -253,6 +254,7 @@ window.textsecure.api = function() {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var id_regex = RegExp( "^https:\/\/" + ATTACHMENT_HOST + "\/(\\d+)\?");
|
||||||
self.putAttachment = function(encryptedBin) {
|
self.putAttachment = function(encryptedBin) {
|
||||||
return doAjax({
|
return doAjax({
|
||||||
call : 'attachment',
|
call : 'attachment',
|
||||||
|
@ -261,27 +263,30 @@ window.textsecure.api = function() {
|
||||||
}).then(function(response) {
|
}).then(function(response) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
$.ajax(response.location, {
|
$.ajax(response.location, {
|
||||||
type : "PUT",
|
type : "PUT",
|
||||||
headers: {
|
headers : {"Content-Type" : "application/octet-stream"},
|
||||||
"Content-Type": "application/octet-stream"
|
data : encryptedBin,
|
||||||
|
success : function() {
|
||||||
|
try {
|
||||||
|
// Parse the id as a string from the location url
|
||||||
|
// (workaround for ids too large for Javascript numbers)
|
||||||
|
var id = response.location.match(id_regex)[1];
|
||||||
|
resolve(id);
|
||||||
|
} catch(e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
data: encryptedBin,
|
error : function(jqXHR, textStatus, errorThrown) {
|
||||||
|
var code = jqXHR.status;
|
||||||
|
if (code > 999 || code < 100)
|
||||||
|
code = -1;
|
||||||
|
|
||||||
success : function() {
|
var e = new Error(code);
|
||||||
resolve(response.id);
|
e.name = "HTTPError";
|
||||||
},
|
if (jqXHR.responseJSON)
|
||||||
|
e.response = jqXHR.responseJSON;
|
||||||
error : function(jqXHR, textStatus, errorThrown) {
|
reject(e);
|
||||||
var code = jqXHR.status;
|
}
|
||||||
if (code > 999 || code < 100)
|
|
||||||
code = -1;
|
|
||||||
|
|
||||||
var e = new Error(code);
|
|
||||||
e.name = "HTTPError";
|
|
||||||
if (jqXHR.responseJSON)
|
|
||||||
e.response = jqXHR.responseJSON;
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -593,7 +593,7 @@ window.textsecure.subscribeToPush = function(message_callback) {
|
||||||
throw new Error("Unknown flags in message");
|
throw new Error("Unknown flags in message");
|
||||||
|
|
||||||
var handleAttachment = function(attachment) {
|
var handleAttachment = function(attachment) {
|
||||||
return textsecure.api.getAttachment(attachment.id).then(function(encryptedBin) {
|
return textsecure.api.getAttachment(attachment.id.toString()).then(function(encryptedBin) {
|
||||||
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
||||||
attachment.decrypted = decryptedBin;
|
attachment.decrypted = decryptedBin;
|
||||||
});
|
});
|
||||||
|
|
|
@ -200,7 +200,7 @@ window.textsecure.messaging = function() {
|
||||||
var iv = textsecure.crypto.getRandomBytes(16);
|
var iv = textsecure.crypto.getRandomBytes(16);
|
||||||
return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
||||||
return textsecure.api.putAttachment(encryptedBin).then(function(id) {
|
return textsecure.api.putAttachment(encryptedBin).then(function(id) {
|
||||||
proto.id = id;
|
proto.id = dcodeIO.Long.fromString(id);
|
||||||
proto.contentType = attachment.contentType;
|
proto.contentType = attachment.contentType;
|
||||||
return proto;
|
return proto;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue