Skip to content

Commit 9a82589

Browse files
committed
fix: rename files of termux uri with hack(#1009)
1 parent b4290f5 commit 9a82589

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/lib/openFolder.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@ function execOperation(type, action, url, $target, name) {
397397
}
398398

399399
async function renameFile() {
400-
if (url.startsWith("content://com.termux.documents/tree/")) {
400+
if (
401+
url.startsWith("content://com.termux.documents/tree/") &&
402+
!helpers.isFile(type)
403+
) {
401404
alert(strings.warning, strings["rename not supported"]);
402405
return;
403406
}
@@ -411,7 +414,22 @@ function execOperation(type, action, url, $target, name) {
411414

412415
startLoading();
413416
const fs = fsOperation(url);
414-
const newUrl = await fs.renameTo(newName);
417+
let newUrl;
418+
419+
if (
420+
url.startsWith("content://com.termux.documents/tree/") &&
421+
helpers.isFile(type)
422+
) {
423+
// Special handling for Termux content files
424+
const newFilePath = Url.join(Url.dirname(url), newName);
425+
const content = await fs.readFile();
426+
await fsOperation(Url.dirname(url)).createFile(newName, content);
427+
await fs.delete();
428+
newUrl = newFilePath;
429+
} else {
430+
newUrl = await fs.renameTo(newName);
431+
}
432+
415433
newName = Url.basename(newUrl);
416434
$target.querySelector(":scope>.text").textContent = newName;
417435
$target.dataset.url = newUrl;

src/pages/fileBrowser/fileBrowser.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,35 @@ function FileBrowserInclude(mode, info, doesOpenLast = true) {
449449

450450
async function renameFile(newname) {
451451
if (url.startsWith("content://com.termux.documents/tree/")) {
452-
alert(strings.warning, strings["rename not supported"]);
453-
return;
452+
if (helpers.isDir(type)) {
453+
alert(strings.warning, strings["rename not supported"]);
454+
return;
455+
} else {
456+
// Special handling for Termux content files
457+
const fs = fsOperation(url);
458+
try {
459+
const content = await fs.readFile();
460+
const newUrl = Url.join(Url.dirname(url), newname);
461+
await fsOperation(Url.dirname(url)).createFile(newname, content);
462+
await fs.delete();
463+
464+
recents.removeFile(url);
465+
recents.addFile(newUrl);
466+
const file = editorManager.getFile(url, "uri");
467+
if (file) {
468+
file.uri = newUrl;
469+
file.filename = newname;
470+
}
471+
openFolder.renameItem(url, newUrl, newname);
472+
toast(strings.success);
473+
reload();
474+
return;
475+
} catch (err) {
476+
window.log("error", err);
477+
helpers.error(err);
478+
return;
479+
}
480+
}
454481
}
455482
const fs = fsOperation(url);
456483
try {

0 commit comments

Comments
 (0)