Modified implementation of image support without change to filename

This commit is contained in:
Ludovic Dubost 2020-01-04 10:06:41 +01:00
parent 1065ef5d4c
commit 453082c080
6 changed files with 84 additions and 79 deletions

View File

@ -49,7 +49,8 @@ define([
var APP = window.APP = { var APP = window.APP = {
$: $ $: $
}; };
var mediasContent = {};
var CHECKPOINT_INTERVAL = 50; var CHECKPOINT_INTERVAL = 50;
var DISPLAY_RESTORE_BUTTON = false; var DISPLAY_RESTORE_BUTTON = false;
@ -74,7 +75,8 @@ define([
var config = {}; var config = {};
var content = { var content = {
hashes: {}, hashes: {},
ids: {} ids: {},
mediasSources: {}
}; };
var oldHashes = {}; var oldHashes = {};
var oldIds = {}; var oldIds = {};
@ -83,6 +85,16 @@ define([
var myOOId; var myOOId;
var sessionId = Hash.createChannelId(); var sessionId = Hash.createChannelId();
// This structure is used for caching media data and blob urls for each media cryptpad url
var mediasData = {};
var getMediaSources = function() {
if (!content.mediasSources) {
content.mediasSources = {}
}
return content.mediasSources;
}
var getId = function () { var getId = function () {
return metadataMgr.getNetfluxId() + '-' + privateData.clientId; return metadataMgr.getNetfluxId() + '-' + privateData.clientId;
}; };
@ -730,7 +742,23 @@ define([
if (data.type !== 'file') { console.log('unhandled embed type ' + data.type); return; } if (data.type !== 'file') { console.log('unhandled embed type ' + data.type); return; }
var privateDat = cpNfInner.metadataMgr.getPrivateData(); var privateDat = cpNfInner.metadataMgr.getPrivateData();
var origin = privateDat.fileHost || privateDat.origin; var origin = privateDat.fileHost || privateDat.origin;
var src = data.src; // = origin + data.src; var name = data.name;
// Add image to the list
var mediasSources = getMediaSources();
mediasSources[name] = data;
APP.getImageURL(name, function(url) {
console.log("CRYPTPAD success add " + name);
APP.AddImageSuccessCallback({
name: name,
url: url
});
});
}
});
/*
Util.fetch(data.src, function (err, u8) { Util.fetch(data.src, function (err, u8) {
FileCrypto.decrypt(u8, Nacl.util.decodeBase64(data.key), function (err, res) { FileCrypto.decrypt(u8, Nacl.util.decodeBase64(data.key), function (err, res) {
if (err || !res.content) { console.log("cryptpad decode fail"); return APP.AddImageErrorCallback(err); } if (err || !res.content) { console.log("cryptpad decode fail"); return APP.AddImageErrorCallback(err); }
@ -743,17 +771,11 @@ define([
mediasContent[name] = reader.result; mediasContent[name] = reader.result;
} }
reader.readAsArrayBuffer(res.content); reader.readAsArrayBuffer(res.content);
console.log("CRYPTPAD success add " + name);
APP.AddImageSuccessCallback({
name: name,
metadata: res.metadata,
content: res.content,
url: url + hiddendata
});
}); });
}); });
} }
}); });
*/
APP.AddImage = function(cb1, cb2) { APP.AddImage = function(cb1, cb2) {
APP.AddImageSuccessCallback = cb1; APP.AddImageSuccessCallback = cb1;
@ -765,28 +787,37 @@ define([
} }
APP.getImageURL = function(name, callback) { APP.getImageURL = function(name, callback) {
var data = {}; var mediasSources = getMediaSources();
var hiddendata = name.substring(name.lastIndexOf("#") + 1); var data = mediasSources[name];
var params = hiddendata.split(",");
for (var i in params) { if (typeof data === 'undefined') {
var item = params[i].split("="); console.log("CryptPad - could not find matching media for " + name);
data[item[0]] = decodeURIComponent(item[1]); callback("");
return;
}
var blobUrl = (typeof mediasData[data.src] === 'undefined') ? "" : mediasData[data.src].src;
if (blobUrl != "") {
console.log("CryptPad Image already loaded " + blobUrl);
callback(blobUrl);
return;
} }
Util.fetch(data.src, function (err, u8) { Util.fetch(data.src, function (err, u8) {
FileCrypto.decrypt(u8, Nacl.util.decodeBase64(data.key), function (err, res) { FileCrypto.decrypt(u8, Nacl.util.decodeBase64(data.key), function (err, res) {
if (err || !res.content) { callback(); } if (err || !res.content) { callback(""); }
var url = URL.createObjectURL(res.content) + "#" + hiddendata; var blobUrl = URL.createObjectURL(res.content);
// store media content for potential export // store media blobUrl and content for cache and export
var mediaData = { blobUrl : blobUrl, content : "" };
mediasData[data.src] = mediaData;
var reader = new FileReader(); var reader = new FileReader();
reader.onloadend = (event) => { reader.onloadend = (event) => {
mediasContent[data.name + "#" + hiddendata] = reader.result; mediaData.content = reader.result;
} }
reader.readAsArrayBuffer(res.content); reader.readAsArrayBuffer(res.content);
console.log("Adding CryptPad Image " + data.name + ": " + url); console.log("Adding CryptPad Image " + data.name + ": " + blobUrl);
window.frames[0].AscCommon.g_oDocumentUrls.addImageUrl(data.name + "#" + hiddendata, url); window.frames[0].AscCommon.g_oDocumentUrls.addImageUrl(data.name, blobUrl);
callback(url); callback(blobUrl);
}); });
}); });
} }
@ -832,10 +863,13 @@ define([
// Adding images // Adding images
for (var mediaFileName in window.frames[0].AscCommon.g_oDocumentUrls.urls) { for (var mediaFileName in window.frames[0].AscCommon.g_oDocumentUrls.urls) {
var mediaFileName = mediaFileName.substring(6); var mediaFileName = mediaFileName.substring(6);
if (mediasContent[mediaFileName]) { var mediasSources = getMediaSources();
var mediaSource = mediasSources[mediaFileName];
var mediaData = (typeof mediaSource === 'undefined') ? mediaSource : mediasData[mediaSource.src];
if (typeof mediaData !== 'undefined') {
console.log("Writing media data " + mediaFileName); console.log("Writing media data " + mediaFileName);
console.log("Data"); console.log("Data");
var fileData = mediasContent[mediaFileName]; var fileData = mediaData.content;
console.log(fileData); console.log(fileData);
x2t.FS.writeFile('/working/media/' + mediaFileName, new Uint8Array(fileData)); x2t.FS.writeFile('/working/media/' + mediaFileName, new Uint8Array(fileData));
x2t.FS.writeFile('/working/media/myimage.png', new Uint8Array(fileData)); x2t.FS.writeFile('/working/media/myimage.png', new Uint8Array(fileData));
@ -1220,6 +1254,7 @@ define([
} }
oldHashes = JSON.parse(JSON.stringify(content.hashes)); oldHashes = JSON.parse(JSON.stringify(content.hashes));
} }
if (content.ids) { if (content.ids) {
handleNewIds(oldIds, content.ids); handleNewIds(oldIds, content.ids);
oldIds = JSON.parse(JSON.stringify(content.ids)); oldIds = JSON.parse(JSON.stringify(content.ids));

View File

@ -204703,14 +204703,14 @@ function DecodeBase64(imData, szSrc)
console.log(oImage); console.log(oImage);
// CRYPTPAD: if we find an image URL with #channel= in it // CRYPTPAD: if we find an image URL with #channel= in it
// then we need to ask cryptpad to get the blob // then we need to ask cryptpad to get the blob
if (oImage.src.indexOf("#src=")!=-1) { window.parent.APP.getImageURL(oImage.src, function(url) {
window.parent.APP.getImageURL(oImage.src, function(url) { if (url=="") {
oThis.loadImageByUrl(oImage.Image, url); oThis.loadImageByUrl(oImage.Image, oImage.src);
oThis.map_image_index[url] = oImage; } else {
}); oThis.loadImageByUrl(oImage.Image, url);
} else { oThis.map_image_index[url] = oImage;
oThis.loadImageByUrl(oImage.Image, oImage.src); }
} });
}; };
this.LoadImagesWithCallback = function(arr, loadImageCallBack, loadImageCallBackArgs) this.LoadImagesWithCallback = function(arr, loadImageCallBack, loadImageCallBackArgs)
@ -205468,16 +205468,6 @@ CCollaborativeEditingBase.prototype.Clear_NewImages = function()
CCollaborativeEditingBase.prototype.Add_NewImage = function(Url) CCollaborativeEditingBase.prototype.Add_NewImage = function(Url)
{ {
this.m_aNewImages.push( Url ); this.m_aNewImages.push( Url );
// CryptPad - Modify URL for local loading
/*
console.log("Add Image " + Url);
if (Url.indexOf("#src=")!=-1) {
window.parent.APP.getImageURL(Url, function(url) {
console.log("CryptPad Image " + url);
});
}
*/ 
// CryptPad - End modification
}; };
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// Функции для работы с массивом m_aDC // Функции для работы с массивом m_aDC

View File

@ -21410,16 +21410,6 @@ CCollaborativeEditingBase.prototype.Clear_NewImages = function()
CCollaborativeEditingBase.prototype.Add_NewImage = function(Url) CCollaborativeEditingBase.prototype.Add_NewImage = function(Url)
{ {
this.m_aNewImages.push( Url ); this.m_aNewImages.push( Url );
// CryptPad - Modify URL for local loading
/*
console.log("Add Image " + Url);
if (Url.indexOf("#src=")!=-1) {
window.parent.APP.getImageURL(Url, function(url) {
console.log("CryptPad Image " + url);
});
}
*/ 
// CryptPad - End modification
}; };
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// Функции для работы с массивом m_aDC // Функции для работы с массивом m_aDC

View File

@ -206051,14 +206051,14 @@ function DecodeBase64(imData, szSrc)
console.log(oImage); console.log(oImage);
// CRYPTPAD: if we find an image URL with #channel= in it // CRYPTPAD: if we find an image URL with #channel= in it
// then we need to ask cryptpad to get the blob // then we need to ask cryptpad to get the blob
if (oImage.src.indexOf("#src=")!=-1) { window.parent.APP.getImageURL(oImage.src, function(url) {
window.parent.APP.getImageURL(oImage.src, function(url) { if (url=="") {
oThis.loadImageByUrl(oImage.Image, url); oThis.loadImageByUrl(oImage.Image, oImage.src);
oThis.map_image_index[url] = oImage; } else {
}); oThis.loadImageByUrl(oImage.Image, url);
} else { oThis.map_image_index[url] = oImage;
oThis.loadImageByUrl(oImage.Image, oImage.src); }
} });
}; };
this.LoadImagesWithCallback = function(arr, loadImageCallBack, loadImageCallBackArgs) this.LoadImagesWithCallback = function(arr, loadImageCallBack, loadImageCallBackArgs)

View File

@ -21543,16 +21543,6 @@ CCollaborativeEditingBase.prototype.Clear_NewImages = function()
CCollaborativeEditingBase.prototype.Add_NewImage = function(Url) CCollaborativeEditingBase.prototype.Add_NewImage = function(Url)
{ {
this.m_aNewImages.push( Url ); this.m_aNewImages.push( Url );
// CryptPad - Modify URL for local loading
/*
console.log("Add Image " + Url);
if (Url.indexOf("#src=")!=-1) {
window.parent.APP.getImageURL(Url, function(url) {
console.log("CryptPad Image " + url);
});
}
*/ 
// CryptPad - End modification
}; };
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// Функции для работы с массивом m_aDC // Функции для работы с массивом m_aDC

View File

@ -205206,14 +205206,14 @@ function DecodeBase64(imData, szSrc)
console.log(oImage); console.log(oImage);
// CRYPTPAD: if we find an image URL with #channel= in it // CRYPTPAD: if we find an image URL with #channel= in it
// then we need to ask cryptpad to get the blob // then we need to ask cryptpad to get the blob
if (oImage.src.indexOf("#src=")!=-1) { window.parent.APP.getImageURL(oImage.src, function(url) {
window.parent.APP.getImageURL(oImage.src, function(url) { if (url=="") {
oThis.loadImageByUrl(oImage.Image, url); oThis.loadImageByUrl(oImage.Image, oImage.src);
oThis.map_image_index[url] = oImage; } else {
}); oThis.loadImageByUrl(oImage.Image, url);
} else { oThis.map_image_index[url] = oImage;
oThis.loadImageByUrl(oImage.Image, oImage.src); }
} });
}; };
this.LoadImagesWithCallback = function(arr, loadImageCallBack, loadImageCallBackArgs) this.LoadImagesWithCallback = function(arr, loadImageCallBack, loadImageCallBackArgs)