Skip to content
This repository was archived by the owner on Jun 12, 2026. It is now read-only.

Commit 4dd4ef6

Browse files
author
yggverse
committed
implement public storage preview
1 parent 1786f99 commit 4dd4ef6

3 files changed

Lines changed: 51 additions & 9 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ regex = "1.11.2"
2727
# development
2828
[patch.crates-io]
2929
btracker-fs = { git = "https://github.com/YGGverse/btracker-fs.git" }
30+
# btracker-fs = { path = "../btracker-fs" }

src/main.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,33 @@ fn response(
157157
use titanite::response::*;
158158
debug!("Incoming request from `{peer}` to `{}`", request.url.path());
159159
send(
160-
&match Route::from_url(&request.url) {
160+
&match Route::from_url(&request.url, public) {
161+
Route::File(ref path) => success::Default {
162+
data: &std::fs::read(path).unwrap(),
163+
meta: success::default::Meta {
164+
mime: match path.extension() {
165+
Some(extension) => {
166+
let e = extension.to_ascii_lowercase();
167+
if e == "jpeg" || e == "jpg" {
168+
"image/jpeg"
169+
} else if e == "gif" {
170+
"image/gif"
171+
} else if e == "png" {
172+
"image/png"
173+
} else if e == "webp" {
174+
"image/webp"
175+
} else if e == "txt" || e == "log" {
176+
"text/plain"
177+
} else {
178+
todo!()
179+
}
180+
}
181+
None => todo!(),
182+
}
183+
.to_string(),
184+
},
185+
}
186+
.into_bytes(),
161187
Route::List { page, keyword } => match list(config, public, keyword.as_deref(), page) {
162188
Ok(data) => success::Default {
163189
data: data.as_bytes(),
@@ -179,7 +205,7 @@ fn response(
179205
})
180206
.into_bytes(),
181207
Route::Info(id) => match public.torrent(id) {
182-
Some(torrent) => match info(config, torrent) {
208+
Some(torrent) => match info(config, public, torrent) {
183209
Ok(data) => success::Default {
184210
data: data.as_bytes(),
185211
meta: success::default::Meta {
@@ -337,7 +363,7 @@ fn list(
337363
Ok(b.join("\n"))
338364
}
339365

340-
fn info(config: &Config, torrent: Torrent) -> Result<String> {
366+
fn info(config: &Config, public: &Public, torrent: Torrent) -> Result<String> {
341367
struct File {
342368
path: Option<PathBuf>,
343369
length: u64,
@@ -396,7 +422,16 @@ fn info(config: &Config, torrent: Torrent) -> Result<String> {
396422
}) {
397423
b.push("## Files\n".into());
398424
for file in files {
399-
b.push(format!("* {} ({})", file.path(), format::size(file.length)));
425+
let p = file.path();
426+
b.push(match public.href(&i.info_hash.as_string(), &p) {
427+
Some(href) => format!(
428+
"=> {} {} ({})",
429+
urlencoding::encode(&href),
430+
p,
431+
format::size(file.length)
432+
),
433+
None => format!("{} ({})", p, format::size(file.length)), // * ?
434+
})
400435
}
401436
}
402437

src/route.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use std::str::FromStr;
2-
1+
use btracker_fs::public::Public;
32
use librqbit_core::Id20;
43
use regex::Regex;
4+
use std::{path::PathBuf, str::FromStr};
55
use url::Url;
66

77
pub enum Route {
8+
File(PathBuf),
89
Info(Id20),
910
List {
1011
keyword: Option<String>,
@@ -15,8 +16,9 @@ pub enum Route {
1516
}
1617

1718
impl Route {
18-
pub fn from_url(url: &Url) -> Self {
19-
let p = url.path().to_lowercase();
19+
pub fn from_url(url: &Url, public: &Public) -> Self {
20+
let p = urlencoding::decode(url.path()).ok().unwrap_or_default();
21+
let t = p.trim_matches('/');
2022
let q = url.query();
2123

2224
if p.is_empty() {
@@ -26,7 +28,11 @@ impl Route {
2628
};
2729
}
2830

29-
if let Ok(id) = Id20::from_str(p.trim_matches('/')) {
31+
if let Some(path) = public.filepath(t) {
32+
return Self::File(path);
33+
}
34+
35+
if let Ok(id) = Id20::from_str(t) {
3036
return Self::Info(id);
3137
}
3238

0 commit comments

Comments
 (0)