mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-27 15:44:22 +02:00
parent
d70d66f650
commit
f91fd9aea0
@ -52,7 +52,7 @@ export class NavFile extends NavEntry {
|
||||
} else { // single click
|
||||
this.double_click = true;
|
||||
if (this.timeout)
|
||||
clearTimeout(this.timeout)
|
||||
clearTimeout(this.timeout);
|
||||
this.timeout = setTimeout(() => {
|
||||
this.double_click = false;
|
||||
}, 500);
|
||||
@ -89,18 +89,19 @@ export class NavFile extends NavEntry {
|
||||
}
|
||||
|
||||
async open() {
|
||||
var proc_output = await cockpit.spawn(["file", "--mime-type", this.path_str()], {superuser: "try"});
|
||||
var fields = proc_output.split(/:(?=[^:]+$)/); // ensure it's the last : with lookahead
|
||||
var type = fields[1].trim();
|
||||
|
||||
if (/^text/.test(type) || /^inode\/x-empty$/.test(type) || this.stat["size"] === 0 || (/^application\/octet-stream/.test(type) && this.stat["size"] === 1)) {
|
||||
this.show_edit_file_contents();
|
||||
} else {
|
||||
console.log("Unknown mimetype: " + type);
|
||||
if (await this.nav_window_ref.modal_prompt.confirm("Can't open " + this.filename + " for editing.", "Download it instead?")) {
|
||||
var download = new NavDownloader(this);
|
||||
download.download();
|
||||
async function isEditable(path, fileSize) {
|
||||
if (fileSize === 0)
|
||||
return true; // empty file always editable
|
||||
const encoding = (await cockpit.spawn(["file", "-bL", "--mime-encoding", path], { superuser: "try" })).trim();
|
||||
if (['us-ascii', 'utf-8'].includes(encoding))
|
||||
return true;
|
||||
if (fileSize === 1 && ['\n', '\t', ' '].includes(await cockpit.file(path).read()))
|
||||
return true; // special case for empty file with newline, shows as `application/octet-stream; charset=binary`
|
||||
return false;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user