Skip to content

Commit cbe5b73

Browse files
committed
fix: some tuff
1 parent 9117dc6 commit cbe5b73

7 files changed

+152
-10
lines changed

Cargo.lock

+111-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ data-encoding = "2.4.0"
5656
reqwest-retry = "0.6"
5757
reqwest-middleware = "0.3"
5858
memory-stats = "1.2.0"
59+
graphql_client = { version = "0.14", features = ["reqwest"] }
5960
#format_serde_error = "0.3"
6061

6162
[dev-dependencies]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ run:
3131
REPLEX_DISABLE_LEAF_COUNT=0 \
3232
REPLEX_DISABLE_USER_STATE=0 \
3333
REPLEX_ENABLE_CONSOLE=0 \
34-
REPLEX_CACHE_TTL=0 \
34+
REPLEX_CACHE_TTL=600 \
3535
REPLEX_NTF_WATCHLIST_FORCE=1 \
3636
RUST_LOG="info,replex=info" \
3737
RUSTFLAGS=-Awarnings \

src/cache.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use moka::{future::ConcurrentCacheExt, Expiry};
77
use crate::headers;
88
use once_cell::sync::Lazy;
99
use salvo::cache::CachedBody;
10+
use regex::Regex;
1011
use salvo::cache::MethodSkipper;
1112
use salvo::conn::SocketAddr;
1213
use salvo::handler::Skipper;
@@ -157,6 +158,7 @@ pub struct RequestIssuer {
157158
use_authority: bool,
158159
use_local_addr: bool,
159160
use_path: bool,
161+
path_strip_last_segment: bool,
160162
// TODO: Also make this like headers. So that we can dump unneeded query variables like X-Plex-Client-Identifier and have cross device caching
161163
use_query: bool,
162164
use_method: bool,
@@ -176,6 +178,7 @@ impl RequestIssuer {
176178
use_authority: true,
177179
use_local_addr: false,
178180
use_path: true,
181+
path_strip_last_segment: false,
179182
use_query: true,
180183
use_method: true,
181184
use_mime: false,
@@ -189,6 +192,7 @@ impl RequestIssuer {
189192
use_authority: true,
190193
use_local_addr: false,
191194
use_path: true,
195+
path_strip_last_segment: false,
192196
use_query: true,
193197
use_method: true,
194198
use_mime: false,
@@ -235,6 +239,10 @@ impl RequestIssuer {
235239
self.use_headers = value;
236240
self
237241
}
242+
pub fn path_strip_last_segment(mut self, value: bool) -> Self {
243+
self.path_strip_last_segment = value;
244+
self
245+
}
238246
}
239247

240248
// #[derive(Encode, Decode, PartialEq, Debug)]
@@ -270,7 +278,15 @@ impl CacheIssuer for RequestIssuer {
270278
);
271279
}
272280
if self.use_path {
273-
key.push_str(req.uri().path());
281+
// strip last segment of an path. Used for paths that include tokens
282+
if self.path_strip_last_segment{
283+
// let re = Regex::new(format!(r"{}", self.path_regex.clone().unwrap()).as_str()).unwrap();
284+
// req.uri().path()
285+
//let k = req.uri().path().split('/').iter().join("/");
286+
key.push_str(req.uri().path());
287+
} else {
288+
key.push_str(req.uri().path());
289+
}
274290
}
275291
// TODO: Clean up query. Not everything needs a cache change.
276292
if self.use_query {

src/plex_client.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl PlexClient {
301301
self,
302302
uuid: String,
303303
) -> Option<String> {
304-
let cache_key = format!("{}:cover_art", uuid);
304+
let cache_key = format!("{}:hero_art", uuid);
305305

306306
let cached_result: Option<Option<String>> =
307307
GLOBAL_CACHE.get(cache_key.as_str()).await;
@@ -333,12 +333,14 @@ impl PlexClient {
333333
}
334334
}
335335
}
336+
337+
image.as_ref()?; // dont return and dont cache, let us just retry next time.
336338

337339
let mut cache_expiry = crate::cache::Expiration::Month;
338-
image.as_ref()?; // dont return and dont cache, let us just retry next time.
339340
let _ = GLOBAL_CACHE
340341
.insert(cache_key, image.clone(), cache_expiry)
341342
.await;
343+
342344
image
343345
}
344346

src/routes.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,9 @@ pub fn route() -> Router {
156156
)
157157
.push(
158158
Router::new()
159-
.path("/image/hero/<type>/<uuid>/<token>")
160-
// .path("/image/hero.jpg")
159+
.path("/replex/image/hero/<type>/<uuid>/<token>")
160+
// .hoop(anonymous_cache(60 * 60 * 24 * 30))
161161
.get(hero_image)
162-
//.get(proxy_request),
163162
)
164163
.push(
165164
Router::new()
@@ -384,7 +383,6 @@ pub async fn hero_image(
384383
depot: &mut Depot,
385384
) {
386385
let mut params: PlexContext = req.extract().await.unwrap();
387-
388386
let t = req.param::<String>("type").unwrap();
389387
let uuid = req.param::<String>("uuid").unwrap();
390388
let token = req.param::<String>("token");
@@ -861,6 +859,7 @@ pub fn auto_refresh_cache() -> Cache<MemoryStore<String>, RequestIssuer> {
861859
)
862860
}
863861

862+
// cache that uses plex identifiers
864863
pub fn default_cache() -> Cache<MemoryStore<String>, RequestIssuer> {
865864
let config: Config = Config::figment().extract().unwrap();
866865
let ttl = if config.cache_rows {
@@ -879,6 +878,20 @@ pub fn default_cache() -> Cache<MemoryStore<String>, RequestIssuer> {
879878
)
880879
}
881880

881+
// simple cache that doesnt use plex identifers
882+
pub fn anonymous_cache(ttl: u64) -> Cache<MemoryStore<String>, RequestIssuer> {
883+
//let config: Config = Config::figment().extract().unwrap();
884+
885+
Cache::new(
886+
MemoryStore::with_moka_cache(
887+
MokaCacheSync::builder()
888+
.time_to_live(Duration::from_secs(ttl))
889+
.build(),
890+
),
891+
RequestIssuer::new().use_scheme(false).use_query(false),
892+
)
893+
}
894+
882895
// const RESOLUTIONS: HashMap<&'static str, &'static str> =
883896
// HashMap::from([("1080p", "1920x1080"), ("4k", "4096x2160")]);
884897

src/transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ impl Transform for MediaStyleTransform {
800800
guid = guid.replace("plex://", "");
801801

802802
//let cover_art = item.get_hero_art(plex_client).await;
803-
let cover_art = Some(format!("{}://{}/image/hero/{}/{}",
803+
let cover_art = Some(format!("{}://{}/replex/image/hero/{}/{}",
804804
match options.forwarded_proto {
805805
Some(v) => v,
806806
None => "http".to_string()

0 commit comments

Comments
 (0)