|
|
@ -63,7 +63,8 @@
|
|
|
|
],
|
|
|
|
],
|
|
|
|
pdf: {},
|
|
|
|
pdf: {},
|
|
|
|
download: {
|
|
|
|
download: {
|
|
|
|
text: "Download"
|
|
|
|
text: "Save",
|
|
|
|
|
|
|
|
textDl: "Load attachment"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Plugins: {
|
|
|
|
Plugins: {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -114,8 +115,8 @@
|
|
|
|
},
|
|
|
|
},
|
|
|
|
download: function (metadata, url, content, cfg, cb) {
|
|
|
|
download: function (metadata, url, content, cfg, cb) {
|
|
|
|
var btn = document.createElement('button');
|
|
|
|
var btn = document.createElement('button');
|
|
|
|
btn.setAttribute('class', 'btn btn-success');
|
|
|
|
btn.setAttribute('class', 'btn btn-default');
|
|
|
|
btn.innerHTML = cfg.download.text + '<br>' +
|
|
|
|
btn.innerHTML = '<i class="fa fa-save"></i>' + cfg.download.text + '<br>' +
|
|
|
|
(metadata.name ? '<b>' + fixHTML(metadata.name) + '</b>' : '');
|
|
|
|
(metadata.name ? '<b>' + fixHTML(metadata.name) + '</b>' : '');
|
|
|
|
btn.addEventListener('click', function () {
|
|
|
|
btn.addEventListener('click', function () {
|
|
|
|
saveFile(content, url, metadata.name);
|
|
|
|
saveFile(content, url, metadata.name);
|
|
|
@ -125,6 +126,92 @@
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var makeProgressBar = function (cfg, mediaObject) {
|
|
|
|
|
|
|
|
// XXX CSP: we'll need to add style in cryptpad's less
|
|
|
|
|
|
|
|
var style = (function(){/*
|
|
|
|
|
|
|
|
.mediatag-progress-container {
|
|
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
|
|
border: 1px solid #0087FF;
|
|
|
|
|
|
|
|
background: white;
|
|
|
|
|
|
|
|
height: 25px;
|
|
|
|
|
|
|
|
display: inline-flex;
|
|
|
|
|
|
|
|
width: 200px;
|
|
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
|
|
|
vertical-align: top;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.mediatag-progress-bar {
|
|
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
|
|
left: 0;
|
|
|
|
|
|
|
|
top: 0;
|
|
|
|
|
|
|
|
bottom: 0;
|
|
|
|
|
|
|
|
background: #0087FF;
|
|
|
|
|
|
|
|
width: 0%;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.mediatag-progress-text {
|
|
|
|
|
|
|
|
height: 25px;
|
|
|
|
|
|
|
|
margin-left: 5px;
|
|
|
|
|
|
|
|
line-height: 25px;
|
|
|
|
|
|
|
|
vertical-align: top;
|
|
|
|
|
|
|
|
width: auto;
|
|
|
|
|
|
|
|
display: inline-block;
|
|
|
|
|
|
|
|
color: #3F4141;
|
|
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*/}).toString().slice(14, -3);
|
|
|
|
|
|
|
|
var container = document.createElement('div');
|
|
|
|
|
|
|
|
container.classList.add('mediatag-progress-container');
|
|
|
|
|
|
|
|
var bar = document.createElement('div');
|
|
|
|
|
|
|
|
bar.classList.add('mediatag-progress-bar');
|
|
|
|
|
|
|
|
container.appendChild(bar);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var text = document.createElement('span');
|
|
|
|
|
|
|
|
text.classList.add('mediatag-progress-text');
|
|
|
|
|
|
|
|
text.innerText = '0%';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mediaObject.on('progress', function (obj) {
|
|
|
|
|
|
|
|
var percent = obj.progress;
|
|
|
|
|
|
|
|
text.innerText = (Math.round(percent*10))/10+'%';
|
|
|
|
|
|
|
|
bar.setAttribute('style', 'width:'+percent+'%;');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mediaObject.tag.innerHTML = '<style>'+style+'</style>';
|
|
|
|
|
|
|
|
mediaObject.tag.appendChild(container);
|
|
|
|
|
|
|
|
mediaObject.tag.appendChild(text);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var makeDownloadButton = function (cfg, mediaObject, size, cb) {
|
|
|
|
|
|
|
|
var btn = document.createElement('button');
|
|
|
|
|
|
|
|
btn.setAttribute('class', 'btn btn-default');
|
|
|
|
|
|
|
|
btn.innerHTML = '<i class="fa fa-paperclip"></i>' +
|
|
|
|
|
|
|
|
cfg.download.textDl + ' <b>(' + size + 'MB)</b>';
|
|
|
|
|
|
|
|
btn.addEventListener('click', function () {
|
|
|
|
|
|
|
|
makeProgressBar(cfg, mediaObject);
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
mediaObject.tag.innerHTML = '';
|
|
|
|
|
|
|
|
mediaObject.tag.appendChild(btn);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getFileSize = function (src, _cb) {
|
|
|
|
|
|
|
|
var cb = function (e, res) {
|
|
|
|
|
|
|
|
_cb(e, res);
|
|
|
|
|
|
|
|
cb = function () {};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// XXX Cache
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open("HEAD", src);
|
|
|
|
|
|
|
|
xhr.onerror = function () { return void cb("XHR_ERROR"); };
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
|
|
|
|
if (this.readyState === this.DONE) {
|
|
|
|
|
|
|
|
cb(null, Number(xhr.getResponseHeader("Content-Length")));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onload = function () {
|
|
|
|
|
|
|
|
if (/^4/.test('' + this.status)) { return void cb("XHR_ERROR " + this.status); }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Download a blob from href
|
|
|
|
// Download a blob from href
|
|
|
|
var download = function (src, _cb, progressCb) {
|
|
|
|
var download = function (src, _cb, progressCb) {
|
|
|
@ -433,6 +520,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
// End media-tag rendering: display the tag and emit the event
|
|
|
|
// End media-tag rendering: display the tag and emit the event
|
|
|
|
var end = function (decrypted) {
|
|
|
|
var end = function (decrypted) {
|
|
|
|
|
|
|
|
mediaObject.complete = true;
|
|
|
|
process(mediaObject, decrypted, cfg, function (err) {
|
|
|
|
process(mediaObject, decrypted, cfg, function (err) {
|
|
|
|
if (err) { return void emit('error', err); }
|
|
|
|
if (err) { return void emit('error', err); }
|
|
|
|
mediaObject._blob = decrypted;
|
|
|
|
mediaObject._blob = decrypted;
|
|
|
@ -441,11 +529,13 @@
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// If we have the blob in our cache, don't download & decrypt it again, just display
|
|
|
|
// If we have the blob in our cache, don't download & decrypt it again, just display
|
|
|
|
|
|
|
|
// XXX Store in the cache the pending mediaobject: make sure we don't download and decrypt twice the same element at the same time
|
|
|
|
if (cache[uid]) {
|
|
|
|
if (cache[uid]) {
|
|
|
|
end(cache[uid]);
|
|
|
|
end(cache[uid]);
|
|
|
|
return mediaObject;
|
|
|
|
return mediaObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var dl = function () {
|
|
|
|
// Download the encrypted blob
|
|
|
|
// Download the encrypted blob
|
|
|
|
download(src, function (err, u8Encrypted) {
|
|
|
|
download(src, function (err, u8Encrypted) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
@ -472,6 +562,22 @@
|
|
|
|
progress: 0.5*progress
|
|
|
|
progress: 0.5*progress
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (cfg.force) { dl(); return mediaObject; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var maxSize = 5 * 1024 * 1024;
|
|
|
|
|
|
|
|
getFileSize(src, function (err, size) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
if (err === "XHR_ERROR 404") {
|
|
|
|
|
|
|
|
mediaObject.tag.innerHTML = '<img style="width: 100px; height: 100px;" src="/images/broken.png">';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return void emit('error', err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!size || size < maxSize) { return void dl(); }
|
|
|
|
|
|
|
|
var sizeMb = Math.round(10 * size / 1024 / 1024) / 10;
|
|
|
|
|
|
|
|
makeDownloadButton(cfg, mediaObject, sizeMb, dl);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return mediaObject;
|
|
|
|
return mediaObject;
|
|
|
|
};
|
|
|
|
};
|
|
|
|