Allow 'edit anyway' option for non-text files

fixes #50
This commit is contained in:
joshuaboud 2022-10-26 17:37:56 -03:00
parent d70d66f650
commit f91fd9aea0
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E

View File

@ -52,7 +52,7 @@ export class NavFile extends NavEntry {
} else { // single click } else { // single click
this.double_click = true; this.double_click = true;
if (this.timeout) if (this.timeout)
clearTimeout(this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(() => { this.timeout = setTimeout(() => {
this.double_click = false; this.double_click = false;
}, 500); }, 500);
@ -89,18 +89,19 @@ export class NavFile extends NavEntry {
} }
async open() { async open() {
var proc_output = await cockpit.spawn(["file", "--mime-type", this.path_str()], {superuser: "try"}); async function isEditable(path, fileSize) {
var fields = proc_output.split(/:(?=[^:]+$)/); // ensure it's the last : with lookahead if (fileSize === 0)
var type = fields[1].trim(); return true; // empty file always editable
const encoding = (await cockpit.spawn(["file", "-bL", "--mime-encoding", path], { superuser: "try" })).trim();
if (/^text/.test(type) || /^inode\/x-empty$/.test(type) || this.stat["size"] === 0 || (/^application\/octet-stream/.test(type) && this.stat["size"] === 1)) { if (['us-ascii', 'utf-8'].includes(encoding))
this.show_edit_file_contents(); return true;
} else { if (fileSize === 1 && ['\n', '\t', ' '].includes(await cockpit.file(path).read()))
console.log("Unknown mimetype: " + type); return true; // special case for empty file with newline, shows as `application/octet-stream; charset=binary`
if (await this.nav_window_ref.modal_prompt.confirm("Can't open " + this.filename + " for editing.", "Download it instead?")) { return false;
var download = new NavDownloader(this);
download.download();
} }
if (await isEditable(this.path_str(), this.stat['size']) || await this.nav_window_ref.modal_prompt.confirm(`'${this.filename}' is not a text file. Open it anyway?`, "WARNING: this may lead to file corruption.", true)) {
this.show_edit_file_contents();
} }
} }