Files
minisrv/zefie_wtvp_minisrv/includes/ServiceVault/wtv-disk/sync.js
2025-08-12 23:45:04 -04:00

327 lines
18 KiB
JavaScript

const minisrv_service_file = true;
const diskmap = request_headers.query[wtvshared.getCaseInsensitiveKey("DiskMap", request_headers.query)];
const wtvdl = new WTVDisk(minisrv_config, service_name);
const force_update = (request_headers.query.force == "true") ? true : false;
const no_delete = (request_headers.query.dont_delete_files == "true") ? true : false;
const content_dir = "content/"
const diskmap_dir = content_dir + "diskmaps/";
if (request_headers['wtv-request-type'] == 'download') {
function generateDownloadList(diskmap_group_name, update_list, diskmap_group_data) {
wtvdl.reset();
let files_to_send = 0;
Object.keys(update_list).forEach(function (k) {
if (update_list[k].checksum_match && !force_update) return;
if (!update_list[k].invalid && !force_update) return;
files_to_send++;
});
// create WebTV Download List
if (diskmap_group_data.execute_start) {
wtvdl.execute(diskmap_group_data.execute_start);
}
// delete groups if force, or group is invalid
if (diskmap_group_data.client_group_data) {
if (force_update || diskmap_group_data.client_group_data.state === "invalid" || typeof diskmap_group_data.client_group_data.state === 'undefined') {
wtvdl.deleteGroupUpdate(diskmap_group_data.client_group_data.group, diskmap_group_data.client_group_data.path);
}
// delete partition/path if force, and not no_delete
if (force_update && !no_delete) {
// don't delete Browser partition, ever, but allow deleting of Browser partition subdirs
if (!diskmap_group_data.base.match(/disk\/browser(\/)?$/i)) {
if (diskmap_group_data.client_group_data.path.toLowerCase() == diskmap_group_data.base.toLowerCase()) {
wtvdl.delete(diskmap_group_data.base, null);
} else {
wtvdl.delete(diskmap_group_data.base, diskmap_group_data.client_group_data.group);
}
}
}
}
if (diskmap_group_name.display) wtvdl.display(diskmap_group_name.display);
if (files_to_send > 0) {
if (diskmap_group_data.partition_size) {
wtvdl.createPartition(diskmap_group_data.base, diskmap_group_data.partition_size);
}
if (!diskmap_group_data.nogroup) {
// only send group commands if group mode is enable
// useful to disable for PUT
wtvdl.createUpdateGroup(diskmap_group_name, diskmap_group_data.base, "invalid", (diskmap_group_data.service_owned || false));
}
Object.keys(update_list).forEach(function (k) {
// file { "action": "delete" }
// Useful to purge files we no longer want on the client
if (update_list[k].action != "DELETE" && update_list[k].action != "DELETEONLY") {
// skip deleting valid files if we aren't specifically requesting their deletion
if (update_list[k].checksum_match && !force_update) return;
if (!update_list[k].invalid && !force_update) return;
}
let delete_file = update_list[k].file.replace(diskmap_group_data.base, "")
if (!diskmap_group_data.files[k].dont_extract_filename) {
delete_file = delete_file.replace(".gz","");
}
wtvdl.delete(delete_file, diskmap_group_name);
});
Object.keys(update_list).forEach(function (k) {
if (update_list[k].checksum_match && !force_update) return;
if (!update_list[k].invalid && !force_update) return;
if (update_list[k].action == "DELETEONLY") return;
if (update_list[k].display) wtvdl.display(update_list[k].display);
switch (update_list[k].action) {
case "PUT":
wtvdl.put(update_list[k].file.replace(diskmap_group_data.base, ""), service_name + ":/" + update_list[k].location, update_list[k].display);
break;
case "GET":
let get_url = service_name + ":/" + update_list[k].location + "?";
if (update_list[k].compress === false) get_url += "dont_compress=true&";
if (update_list[k].type) get_url += "content_type=" + encodeURIComponent(update_list[k].type) + "&";
wtvdl.get(update_list[k].file.replace(diskmap_group_data.base, ""), update_list[k].file, get_url, diskmap_group_name, update_list[k].checksum, update_list[k].uncompressed_size || null, update_list[k].original_filename)
break;
}
});
if (!diskmap_group_data.nogroup) {
wtvdl.createGroup(diskmap_group_name, diskmap_group_data.base, "invalid", (diskmap_group_data.service_owned || false));
// this rename loop is a part of the group system
Object.keys(update_list).forEach(function (k) {
if (update_list[k].checksum_match && !force_update) return;
if (!update_list[k].invalid && !force_update) return;
if (update_list[k].action == "DELETEONLY") return;
wtvdl.rename(update_list[k].file.replace(diskmap_group_data.base, ""), update_list[k].file.replace(diskmap_group_data.base, ""), diskmap_group_name, diskmap_group_name, update_list[k].rename || update_list[k].original_filename || null);
});
wtvdl.setGroup(diskmap_group_name, 'ok', diskmap_group_data.version);
}
}
if (diskmap_group_data.execute_end) {
wtvdl.execute(diskmap_group_data.execute_end);
}
if (files_to_send > 0) {
if (!diskmap_group_data.nogroup) {
wtvdl.deleteGroupUpdate(diskmap_group_name, diskmap_group_data.base);
}
}
const download_list = wtvdl.getDownloadList();
if (minisrv_config.config.show_diskmap) console.log(download_list);
return download_list;
}
function processGroup(diskmap_primary_group, diskmap_group_data, diskmap_subgroup = null, version = 0) {
// parse webtv post
let output_data = '';
let post_data = [];
let client_group_data = [];
if (request_headers.post_data) {
post_data = request_headers.post_data.toString(CryptoJS.enc.Latin1).split("\n");
client_group_data = wtvdl.getGroupDataFromClientPost(request_headers.post_data.toString(CryptoJS.enc.Latin1));
}
if (minisrv_config.config.show_diskmap) console.log("Client POST Data:", client_group_data)
let post_data_current_directory = '';
let post_data_current_file = false;
let post_data_current_group = '';
let post_data_last_modified = false;
let post_data_content_length = false;
let post_data_current_group_state = false;
const post_data_fileinfo = [];
let entry_type = false;
let post_data_current_version = false;
let post_data_current_checksum = false;
let post_data_last_checkup_time = 0;
Object.keys(post_data).forEach(function (k) {
if (post_data[k].slice(0, 7) == "file://") {
entry_type = "folder";
post_data_current_file = false;
post_data_current_version = false;
post_data_last_checkup_time = 0;
post_data_current_group = '';
post_data_current_group_state = false;
post_data_last_modified = false;
post_data_content_length = false;
post_data_current_checksum = false;
post_data_current_directory = post_data[k];
} else {
if (post_data[k].indexOf(":") > 0) {
const post_data_line = post_data[k].split(": ")
const post_data_line_name = post_data_line[0];
post_data_line.shift();
const post_data_line_data = post_data_line.join(": ");
switch (post_data_line_name.toLowerCase()) {
case "last-modified":
post_data_last_modified = Math.floor(Date.parse(post_data_line_data) / 1000);
break;
case "content-length":
post_data_content_length = parseInt(post_data_line_data);
break;
case "version":
post_data_current_version = parseInt(post_data_line_data);
break;
case "group":
post_data_current_group = post_data_line_data;
break;
case "state":
post_data_current_group_state = post_data_line_data;
break;
case "wtv-checksum":
post_data_current_checksum = post_data_line_data;
break;
case "last-checkup-time":
post_data_last_checkup_time = Math.floor(Date.parse(post_data_line_data) / 1000);
break;
}
} else {
if (!entry_type && post_data[k] != "") {
entry_type = "file";
post_data_current_file = post_data[k];
}
if (post_data[k] == "" && entry_type) {
const post_data_current_path = ((entry_type == "file") ? (post_data_current_directory + post_data_current_file) : post_data_current_directory);
const index = post_data_current_path.replace(/[\:\/]/g, "_").toLowerCase() + "_" + post_data_current_group;
if (index.match(/\/$/)) entry_type = "folder";
if (!post_data_fileinfo[index]) post_data_fileinfo[index] = {};
post_data_fileinfo[index].entry_type = entry_type;
post_data_fileinfo[index].file = post_data_current_path;
post_data_fileinfo[index].group = post_data_current_group;
post_data_fileinfo[index].version = post_data_current_version || 0;
if (post_data_current_checksum) post_data_fileinfo[index].checksum = post_data_current_checksum;
if (post_data_current_group_state) post_data_fileinfo[index].state = post_data_current_group_state;
if (post_data_last_checkup_time) post_data_fileinfo[index].last_checkup = post_data_last_checkup_time;
if (post_data_last_modified) post_data_fileinfo[index].last_modified = post_data_last_modified;
if (post_data_content_length) post_data_fileinfo[index].content_length = post_data_content_length;
entry_type = false;
}
}
}
});
const wtv_download_list = [];
let newest_file_epoch = version;
Object.keys(diskmap_group_data.files).forEach(function (k) {
if (!diskmap_group_data.files[k].location) diskmap_group_data.files[k].location = wtvshared.makeSafePath(diskmap_group_data.location,diskmap_group_data.files[k].file.replace(diskmap_group_data.base, ""), true);
let diskmap_data_file = null;
Object.keys(service_vaults).forEach(function (g) {
if (diskmap_data_file != null) return;
diskmap_data_file = service_vaults[g] + "/" + service_name + "/" + diskmap_group_data.files[k].location;
if (!fs.existsSync(diskmap_data_file) || !fs.lstatSync(diskmap_data_file).isFile()) diskmap_data_file = null;
});
if (diskmap_data_file) {
const diskmap_file_stat = fs.lstatSync(diskmap_data_file);
const diskmap_file_data = Buffer.from(fs.readFileSync(diskmap_data_file, {
encoding: null,
flags: 'r'
}));
diskmap_group_data.files[k].base = diskmap_group_data.base;
diskmap_group_data.files[k].last_modified = Math.floor(diskmap_file_stat.mtime.getTime() / 1000);
diskmap_group_data.files[k].content_length = diskmap_file_stat.size;
diskmap_group_data.files[k].action = (diskmap_group_data.files[k].action) ? diskmap_group_data.files[k].action.toUpperCase() : "GET";
// we need the checksum of the uncompressed data
if (wtvshared.getFileExt(diskmap_data_file).toLowerCase() == "gz") {
const diskmap_data_filename = path.basename(diskmap_data_file);
const gunzipped = zlib.gunzipSync(diskmap_file_data);
diskmap_group_data.files[k].checksum = CryptoJS.MD5(CryptoJS.lib.WordArray.create(gunzipped)).toString(CryptoJS.enc.Hex).toLowerCase();
const gzip_fn_end = diskmap_file_data.indexOf("\0", 10);
if (!diskmap_group_data.files[k].dont_extract_filename) {
diskmap_group_data.files[k].original_filename = diskmap_group_data.files[k].file.replace(diskmap_group_data.base,"").replace(diskmap_data_filename, diskmap_file_data.toString('utf8', 10, gzip_fn_end));
}
diskmap_group_data.files[k].uncompressed_size = gunzipped.byteLength;
} else {
diskmap_group_data.files[k].checksum = CryptoJS.MD5(CryptoJS.lib.WordArray.create(diskmap_file_data)).toString(CryptoJS.enc.Hex).toLowerCase();
}
if (parseInt(diskmap_group_data.files[k].last_modified) > newest_file_epoch) newest_file_epoch = parseInt(diskmap_group_data.files[k].last_modified);
diskmap_group_data.files[k].invalid = true;
wtv_download_list.push(diskmap_group_data.files[k]);
}
});
// check to see if client says they have this version
diskmap_group_data.version = newest_file_epoch;
Object.keys(wtv_download_list).forEach(function (k) {
wtv_download_list[k].version = newest_file_epoch;
Object.keys(post_data_fileinfo).forEach(function (g) {
if (post_data_fileinfo[g].file == wtv_download_list[k].file || post_data_fileinfo[g].file == wtv_download_list[k].base) {
diskmap_group_data.group_exists = true;
if (wtv_download_list[k].checksum && wtv_download_list[k].checksum.toLowerCase() == post_data_fileinfo[g].checksum) wtv_download_list[k].invalid = false;
else if (post_data_fileinfo[g].version == wtv_download_list[k].version && post_data_fileinfo[g].state !== "invalid") wtv_download_list[k].invalid = false;
}
});
});
const diskmap_group_name = (diskmap_subgroup == null) ? diskmap_primary_group : diskmap_primary_group + "-" + diskmap_subgroup;
diskmap_group_data.client_group_data = client_group_data[diskmap_group_name] || null;
output_data = generateDownloadList(diskmap_group_name, wtv_download_list, diskmap_group_data);
return output_data;
}
if (diskmap && request_headers.query.group) {
let diskmap_json_file = null;
Object.keys(service_vaults).forEach(function (g) {
if (diskmap_json_file != null) return;
diskmap_json_file = service_vaults[g] + "/" + service_name + "/" + diskmap_dir + diskmap + ".json";
if (!fs.existsSync(diskmap_json_file)) diskmap_json_file = null;
});
if (diskmap_json_file != null) {
try {
// read diskmap
const json_stats = fs.lstatSync(diskmap_json_file);
let diskmap_data = JSON.parse(fs.readFileSync(diskmap_json_file).toString());
if (!diskmap_data[request_headers.query.group]) {
throw ("Invalid diskmap data (group does not match)");
}
data = '';
diskmap_data = diskmap_data[request_headers.query.group];
if (!diskmap_data.location) {
Object.keys(diskmap_data).forEach(function (k) {
if (diskmap_data[k]) {
diskmap_data[k].version = Math.floor(json_stats.mtime.getTime() / 1000);
data += processGroup(request_headers.query.group, diskmap_data[k], k, diskmap_data.version);
}
});
} else {
diskmap_data.version = Math.floor(json_stats.mtime.getTime() / 1000);
data = processGroup(request_headers.query.group, diskmap_data, null, diskmap_data.version);
}
headers = "200 OK\nContent-Type: wtv/download-list";
} catch (e) {
const errpage = wtvshared.doErrorPage(400);
headers = errpage[0];
data = errpage[1];
console.error(" # " + service_name+":/sync error", e);
}
} else {
const errpage = wtvshared.doErrorPage(404, "The requested DiskMap does not exist.");
headers = errpage[0];
data = errpage[1];
if (minisrv_config.config.debug_flags.debug) console.error(" # " + service_name +":/sync error", "could not find diskmap");
}
} else {
const errpage = wtvshared.doErrorPage(400);
headers = errpage[0];
data = errpage[1];
if (minisrv_config.config.debug_flags.debug) console.error(" # " + service_name + ":/sync error", "missing query arguments");
}
} else {
const queryString = Object.keys(request_headers.query)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(request_headers.query[key]))
.join('&');
headers = "302 Found\nLocation: wtv-disk:/content/DownloadScreen.tmpl" + (queryString ? ("?" + queryString) : "");
data = "";
}