ComunicWeb/assets/js/components/account/export/worker.js

272 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2018-05-13 14:52:19 +00:00
/**
* Account data export worker
*
* @author Pierre HUBERT
*/
ComunicWeb.components.account.export.worker = {
/**
* Start account export
*
* @param {String} password The password of the user
*/
start: function(password){
//Get all user text data from the interface
ComunicWeb.components.account.interface.exportData(password, function(result){
//Check for errors
if(result.error){
return ComunicWeb.components.account.export.ui.exportFatalError("Could not get text data! Please check your password...");
}
//Update progress
ComunicWeb.components.account.export.ui.updateMessage("Got text data");
2018-05-13 14:52:19 +00:00
ComunicWeb.components.account.export.ui.updateProgress(10);
2019-01-20 17:04:09 +00:00
//Get explorer
ComunicWeb.components.account.export.worker.getExplorer(result);
2018-05-13 14:52:19 +00:00
});
},
/**
2019-01-20 17:04:09 +00:00
* Second step for export : Get and open personnal data explorer
2018-05-13 14:52:19 +00:00
*
2019-01-20 17:04:09 +00:00
* @param {Object} data Text data about the account (data not modified at this stage)
*/
getExplorer: function(data){
ComunicWeb.components.account.export.ui.updateMessage("Getting data explorer");
ComunicWeb.components.account.export.ui.updateProgress(15);
JSZipUtils.getBinaryContent(ComunicWeb.__config.assetsURL+"zip/personnal-data-export-navigator.zip", function(err, file){
if(err != null){
ComunicWeb.debug.logMessage("Could not get personnal data export navigator!");
ComunicWeb.components.account.export.ui.exportFatalError(e);
return;
}
JSZip.loadAsync(file).then(function(zip){
//Ready to parse data
ComunicWeb.components.account.export.worker.parse(data, zip);
2021-02-13 11:00:49 +00:00
}).catch(e => {
2019-01-20 17:04:09 +00:00
ComunicWeb.debug.logMessage("Could not parse personnal data export navigator!");
ComunicWeb.components.account.export.ui.exportFatalError(e);
return;
});
});
},
/**
* Parse account text data into ZIP file
*
2018-05-13 14:52:19 +00:00
* @param {Object} data Text data about the account
2019-01-20 17:04:09 +00:00
* @param {ZIP} zip The ZIP object to fill
2018-05-13 14:52:19 +00:00
*/
2019-01-20 17:04:09 +00:00
parse: function(data, zip){
2020-04-29 15:01:59 +00:00
//Get UI shortcut
var ui = ComunicWeb.components.account.export.ui;
var Promise = window.Promise;
if (!Promise) {
Promise = JSZip.external.Promise;
}
/**
* Fetch the content and return the associated promise.
* @param {String} url the url of the content to fetch.
* @return {Promise} the promise containing the data.
*/
function urlToPromise(url) {
return new Promise(function(resolve, reject) {
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
/**
* Transform an URL into a path in the archive
*
* @param {String} url The URL to transform
* @return {String} Generated file path
*/
function urlToPath(url) {
var path = url.replace("://", "/");
return "files/" + path;
}
//Determine the list of files to download
var files_list = this._generate_files_list(data);
//Add raw json file
zip.file("source.json", JSON.stringify(data));
2018-05-14 14:53:11 +00:00
//Add the files to download
2018-05-14 14:53:11 +00:00
files_list.forEach(function(url){
var path = urlToPath(url);
zip.file(path, urlToPromise(url), {binary:true});
2018-05-14 14:53:11 +00:00
});
//Generated zip archive
zip.generateAsync({type:"blob"}, function updateCallback(metadata) {
var msg = "progression : " + metadata.percent.toFixed(2) + " %";
if(metadata.currentFile) {
msg += ", current file = " + metadata.currentFile;
}
ui.updateMessage(msg);
ui.updateProgress(metadata.percent.toFixed(2));
})
//Trigger download
.then(function callback(blob) {
//Download file
saveAs(blob, "accountData.zip");
//Update progress
ui.updateProgress(100);
ui.updateMessage("Done !");
}, function (e) {
//In case of error
ComunicWeb.components.account.export.ui.exportFatalError(e);
//Update progress
ui.updateProgress(100);
ui.updateMessage("Error !");
});
},
/**
* Determine the list of files to download
*
* @param {Object} data Dataset to parse
* @return {Array} Generated dataset
*/
_generate_files_list: function(data){
2021-03-08 15:31:47 +00:00
var files = new Set();
2018-05-27 10:10:42 +00:00
/**
* Parse user information to find potential files to download
*
2021-03-08 15:31:47 +00:00
* @param {Object} info Information about the user to parse
2018-05-27 10:10:42 +00:00
*/
var parseUserInfo = function(info){
if(info.accountImage != null)
2021-03-08 15:31:47 +00:00
files.add(info.accountImage);
2018-05-27 10:10:42 +00:00
}
/**
* Parse a comment to find potential files to download
*
* @param {Object} info Information about the comment to parse
*/
var parseComment = function(info){
if(info.img_url != null)
2021-03-08 15:31:47 +00:00
files.add(info.img_url);
}
/**
* Parse a post to find potential files to download
*
* @param {Object} info Information about the post to parse
*/
var parsePost = function(post){
2018-05-14 14:53:11 +00:00
if(post.kind != "youtube"){
if(post.file_path_url != null){
2021-03-08 15:31:47 +00:00
files.add(post.file_path_url);
2018-05-14 14:53:11 +00:00
}
}
//Parse comments
post.comments.forEach(parseComment);
}
2021-03-08 15:43:37 +00:00
/**
* Parse a conversation to find potential files to download
*
* @param {Conversation} info Information about the conversation message to parse
*/
const parseConversation = function(info){
if(info.logo != null)
files.add(info.logo);
}
/**
* Parse a conversation message to find potential files to download
*
2021-03-08 15:31:47 +00:00
* @param {ConversationMessage} info Information about the conversation message to parse
*/
2021-03-08 15:43:37 +00:00
const parseConversationMessage = function(info){
2021-03-08 15:31:47 +00:00
if(info.file != null)
{
files.add(info.file.url);
if (info.file.thumbnail)
files.add(info.file.thumbnail)
}
}
2021-03-08 16:24:27 +00:00
/**
* Parse group information
*
* @param {Group} g Information about the target group
*/
const parseGroupInfo = (g) => {
files.add(g.icon_url)
}
//Main account information
2021-03-08 15:31:47 +00:00
files.add(data.advanced_info.accountImage);
//Posts
data.posts.forEach(parsePost);
//Comments
data.comments.forEach(parseComment);
2021-03-08 15:43:37 +00:00
// Conversations list
data.conversations_list.forEach(parseConversation);
//Conversation message
2018-05-27 10:10:42 +00:00
//* All from users
data.all_conversation_messages.forEach(parseConversationMessage);
//* All from conversations
2018-07-03 09:38:43 +00:00
for (var num in data.conversations_messages) {
2018-05-27 10:10:42 +00:00
if (data.conversations_messages.hasOwnProperty(num)) {
2018-07-03 09:38:43 +00:00
var conversation = data.conversations_messages[num];
2018-05-27 10:10:42 +00:00
conversation.forEach(parseConversationMessage);
}
}
2021-03-08 16:24:27 +00:00
// Groups
data.groups.forEach(parseGroupInfo)
2018-05-27 10:10:42 +00:00
//Related user information
2018-07-03 09:38:43 +00:00
for (var key in data.users_info) {
2018-05-27 10:10:42 +00:00
if (data.users_info.hasOwnProperty(key)) {
parseUserInfo(data.users_info[key]);
}
}
return files;
2018-05-13 14:52:19 +00:00
}
}