Skip to content

Commit 94f186a

Browse files
committed
Add clickable links to open file. FIX: browser security doesn't allow to open file directly
1 parent 70eaeb9 commit 94f186a

4 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ async function search(prompt) {
1616
body: prompt
1717
});
1818

19-
for ([path, rank] of await response.json()) {
20-
if (response.json === null) {
21-
let item = document.createElement("span");
22-
item.appendChild(document.createTextNode("No such token found"));
23-
item.appendChild(document.createElement("br"));
24-
}
25-
19+
const data = await response.json();
20+
if (data.length === 0) {
2621
let item = document.createElement("span");
27-
item.appendChild(document.createTextNode(path));
28-
item.appendChild(document.createElement("br"));
22+
item.textContent = "No results found.";
2923
results.appendChild(item);
24+
} else {
25+
for (let [path, rank] of data) {
26+
let link = document.createElement("a");
27+
link.href = path;
28+
link.target = "_blank"; // open in new tab
29+
link.textContent = path;
30+
results.appendChild(link);
31+
results.appendChild(document.createElement("br"));
32+
}
3033
}
3134
}
3235

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ fn append_folder_to_model(dir_path: &Path, model: Arc<Mutex<InMemoryModel>>, pro
176176
continue 'step;
177177
}
178178

179+
// Get absolute file path
180+
let file_path = fs::canonicalize(&file_path).map_err(|err| {
181+
eprintln!("{}: Could not canonicalize path {} as {}", "ERROR".red().bold(),
182+
file_path.display().to_string().bright_blue(),
183+
err.to_string().red());
184+
})?;
185+
186+
// Main
179187
let mut model = model.lock().unwrap();
180188
if model.requires_reindexing(&file_path, last_modified)? {
181189
println!("{}: Indexing {} ...", "INFO".cyan(), file_path_str.bright_cyan());
@@ -299,7 +307,7 @@ fn entry() -> Result<(), ()> {
299307
// Default address
300308
let address = args.next().unwrap_or("127.0.0.1:6969".to_string());
301309

302-
// TODO: Figure out a better way to append extension ".docsense.json"
310+
// IDEATE: Is it fine to place the index file in the folder itself or place in a root dir?
303311
let mut index_path = Path::new(&dir_path).to_path_buf();
304312
index_path.push(".docsense.json");
305313

@@ -351,7 +359,6 @@ fn main() -> io::Result<()> {
351359
Ok(())
352360
}
353361

354-
// TODO: Search result must consist of clickable links to open that file or file section
355362
// TODO: Synonym terms
356363
// TODO: Add levenstein distance or cosine similarity
357364
// TODO: Add better document ranker specifically "Okapi BM-25"

src/model.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
path::{Path, PathBuf}, time::SystemTime
2+
fs, path::{Path, PathBuf}, time::SystemTime
33
};
44

55
use std::collections::HashMap;
@@ -282,6 +282,8 @@ impl Model for InMemoryModel {
282282
for term in ft.keys() {
283283
self.gtf.entry(term.to_owned()).and_modify(|x| *x += 1).or_insert(1);
284284
}
285+
286+
// Update the Docs table
285287
self.docs.insert(file_path, Doc { count: term_count, ft: ft , last_modified: last_modified});
286288
Ok(())
287289
}

src/server.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ pub fn serve_404(request: Request) -> io::Result<()> {
1212

1313

1414
pub fn serve_500(request: Request) -> io::Result<()> {
15-
return request.respond(Response::from_string("404").with_status_code(StatusCode(500)));
15+
return request.respond(Response::from_string("500").with_status_code(StatusCode(500)));
1616
}
1717

1818

1919
pub fn serve_400(request: Request, message: &str) -> io::Result<()> {
20-
return request.respond(Response::from_string(format!("400: {message}")).with_status_code(StatusCode(500)));
20+
return request.respond(Response::from_string(format!("400: {message}")).with_status_code(StatusCode(400)));
2121
}
2222

2323

24-
pub fn serve_static_file(request: Request, file_path: &str) -> io::Result<()> {
24+
pub fn serve_static_file(request: Request, file_path: &str, content_type: &str) -> io::Result<()> {
2525
let html_file = match File::open(Path::new(file_path)) {
2626
Ok(file) => file,
2727
Err(err) => {
@@ -32,8 +32,8 @@ pub fn serve_static_file(request: Request, file_path: &str) -> io::Result<()> {
3232
return serve_500(request);
3333
}
3434
};
35-
36-
return request.respond(Response::from_file(html_file));
35+
let header = Header::from_bytes("Content-Type", content_type).expect("Should be a valid Content-Type while passing the header.");
36+
return request.respond(Response::from_file(html_file).with_header(header));
3737
}
3838

3939

@@ -127,11 +127,11 @@ pub fn serve_request(request: Request, model: Arc<Mutex<InMemoryModel>>) -> io::
127127
match (&request.method(), request.url()) {
128128

129129
(Method::Get, "/") | (Method::Get, "/index.html") => {
130-
serve_static_file(request, "src/index.html")?
130+
serve_static_file(request, "src/index.html", "text/html; charset=utf-8")?
131131
}
132132

133133
(Method::Get, "/index.js") => {
134-
serve_static_file(request, "src/index.js")?
134+
serve_static_file(request, "src/index.js", "text/javascript; charset=utf-8")?
135135
}
136136

137137
(Method::Post, "/api/search") => {

0 commit comments

Comments
 (0)