Skip to content

Commit 2d66637

Browse files
feat. working ftp
1 parent a480c2a commit 2d66637

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

src/lib/run.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,9 @@ async function run(
177177
const reqId = req.requestId;
178178
let reqPath = req.path.substring(1);
179179

180-
console.log(req)
181-
182180
//if (!reqPath || reqPath.endsWith("/")) {}
183181

184-
if (!reqPath) {
185-
reqPath = "/"+filename
186-
}
187-
182+
//todo : redirect to current file if path is empty
188183
if (!reqPath) {
189184
console.error("Request path is empty");
190185
webServer?.send(reqId, {
@@ -261,12 +256,31 @@ async function run(
261256
}
262257

263258
let url = activeFile.uri;
259+
260+
console.log("activeFile.uri", activeFile.uri);
261+
264262
let file = activeFile.SAFMode === "single" ? activeFile : null;
265263

266264
if (pathName) {
267-
url = Url.join(addedFolder[0].url, reqPath);
265+
let rootFolder = addedFolder[0].url;
266+
const query = url.split("?")[1];
267+
268+
//remove the query string if present this is needs to be removed because the url is not valid
269+
if (rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) {
270+
if (rootFolder.includes("?")) {
271+
rootFolder = rootFolder.split("?")[0];
272+
}
273+
}
274+
275+
url = Url.join(rootFolder, reqPath);
276+
277+
if (query) {
278+
url = `${url}?${query}`;
279+
}
280+
281+
console.log("url", url);
282+
268283
file = editorManager.getFile(url, "uri");
269-
270284
} else if (!activeFile.uri) {
271285
file = activeFile;
272286
}
@@ -496,12 +510,7 @@ async function run(
496510
}
497511

498512
function removePrefix(str, prefix) {
499-
console.log("-----------------")
500-
console.log("removing " + prefix + " from " + str)
501-
const result = str.startsWith(prefix) ? str.slice(prefix.length) : str;
502-
console.log("result : "+result)
503-
console.log("-----------------")
504-
return result
513+
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
505514
}
506515

507516
/**
@@ -511,17 +520,33 @@ async function run(
511520
//get the project url
512521
let rootFolder = addedFolder[0].url
513522

514-
//append path data
523+
//parent of the file
524+
let filePath = pathName
525+
526+
//make the uri absolute if necessary
515527
if (rootFolder === "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome") {
516528
rootFolder = "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome::/data/data/com.termux/files/home/"
517529
}
518530

519-
const path = removePrefix(Url.join(editorManager.activeFile.location,filename),rootFolder)
531+
//remove the query string if present this is needs to be removed because the url is not valid
532+
if (rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) {
533+
if (rootFolder.includes("?")) {
534+
rootFolder = rootFolder.split("?")[0];
535+
}
536+
}
537+
538+
//remove the query string if present this is needs to be removed because the url is not valid
539+
if (filePath.startsWith("ftp:") || rootFolder.startsWith("sftp:")) {
540+
if (filePath.includes("?")) {
541+
filePath = filePath.split("?")[0];
542+
}
543+
}
544+
545+
//subtract the root folder from the file path to get relative path
546+
const path = removePrefix(Url.join(filePath,filename),rootFolder)
520547

521548
const src = `http://localhost:${port}/${path}`;
522549

523-
524-
console.log("Src", src);
525550
if (target === "browser") {
526551
system.openInBrowser(src);
527552
return;

0 commit comments

Comments
 (0)