Skip to content

Commit 580879e

Browse files
Improve New API's Params Consistency
1 parent b9d6033 commit 580879e

File tree

4 files changed

+90
-62
lines changed

4 files changed

+90
-62
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
## API Schemas
55
* Static Image `/${season}/${episode}/${frame}.${format}`
6-
* `season`: String (`mygo`, `ave`, `ave-mujica`) | u32 (`1`, `2`)
7-
* `episode`: String (`1-3`, `4`, `5`, ..., `13`)
6+
* `season`: u32 (`1`, `2`)
7+
* `episode`: u32 (`1`, `2`, `3`, ..., `13`)
88
* `frame`: u32
99
* `format`: String (`jpg`, `jpeg`, `png`, `webp`)
1010
* Animated Image `/${season}/${episode}/${startFrame}-${endFrame}.${format}`
11-
* `season`: String (`mygo`, `ave`, `ave-mujica`) | u32 (`1`, `2`)
12-
* `episode`: String (`1-3`, `4`, `5`, ..., `13`)
11+
* `season`: u32 (`1`, `2`)
12+
* `episode`: u32 (`1`, `2`, `3`, ..., `13`)
1313
* `startFrame`: u32
1414
* `endFrame`: u32 (`endFrame` must be greater than `startFrame`)
1515
* `format`: String (`gif`)
1616
* `gif`: Limited in 3600 frames (150 seconds)
1717
* (deprecated) Legacy Static Image `/${season}-${episode}_${frame}.${format}`
18-
* `season`: String (`mygo`, `ave`, `ave-mujica`)
18+
* `season`: Option\<String\> (`ave-`)
1919
* `episode`: String (`1-3`, `4`, `5`, ..., `13`)
2020
* `frame`: u32
2121
* `format`: String (`jpg`, `jpeg`, `png`, `webp`)

src/endpoints/image.rs

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,30 @@ use crate::{
2424
};
2525

2626

27+
static EPISODE_SPLIT_FRAME: [u32; 3] = [0, 34288, 68334];
28+
29+
2730
pub(crate) async fn handler(
28-
Path((season, episode, target)): Path<(String, String, String)>,
31+
Path((season, episode, target)): Path<(u8, String, String)>,
2932
State(scheduler): State<Arc<Scheduler>>,
3033
) -> impl IntoResponse {
31-
let season = season.to_lowercase();
32-
let episode = episode.to_lowercase();
34+
let season = match season {
35+
1 => "",
36+
2 => "ave-",
37+
_ => ""
38+
};
39+
40+
let mut episode = episode.to_lowercase();
3341
let target = target.to_lowercase();
3442

43+
let env_config = match ENV_CONFIG.get() {
44+
Some(env) => env,
45+
None => return (
46+
StatusCode::INTERNAL_SERVER_ERROR,
47+
"Failed to load server env."
48+
).into_response()
49+
};
50+
3551
let (
3652
target_frame,
3753
target_format
@@ -42,8 +58,6 @@ pub(crate) async fn handler(
4258
"Failed to parse target file."
4359
).into_response(),
4460
};
45-
46-
let animated_frame = target_frame.split_once("-");
4761

4862
let target_format = match target_format {
4963
"png" => ImageFormat::Png,
@@ -52,6 +66,14 @@ pub(crate) async fn handler(
5266
"jpg" | "jpeg" => ImageFormat::Jpeg,
5367
_ => return StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response()
5468
};
69+
70+
let mut animated_frame: Option<(u32, u32)> = target_frame
71+
.split_once("-")
72+
.and_then(
73+
|r| r.0.parse().ok().zip(r.1.parse().ok())
74+
);
75+
76+
let mut frame = target_frame.parse().ok();
5577

5678
if animated_frame.is_some() && target_format != ImageFormat::Gif {
5779
return (
@@ -60,56 +82,62 @@ pub(crate) async fn handler(
6082
).into_response();
6183
}
6284

63-
let env_config = match ENV_CONFIG.get() {
64-
Some(env) => env,
65-
None => return (
66-
StatusCode::INTERNAL_SERVER_ERROR,
67-
"Failed to parse target file."
68-
).into_response()
69-
};
70-
71-
let season_name = match season.as_str() {
72-
"1" | "mygo" => "",
73-
"2" | "ave" | "ave-mujica" => "ave-",
74-
_ => ""
75-
};
76-
77-
if let Some(animated_frame) = animated_frame {
85+
if season.is_empty() {
86+
let offset = match episode.as_str() {
87+
"1" => Some(EPISODE_SPLIT_FRAME[0]),
88+
"2" => Some(EPISODE_SPLIT_FRAME[1]),
89+
"3" => Some(EPISODE_SPLIT_FRAME[2]),
90+
_ => None
91+
};
92+
93+
if let Some(offset) = offset {
94+
match (frame, animated_frame) {
95+
(Some(f), None) => {
96+
frame = Some(f + offset)
97+
},
98+
(None, Some(a_f)) => {
99+
animated_frame = Some((a_f.0 + offset, a_f.1 + offset))
100+
},
101+
_ => return (
102+
StatusCode::BAD_REQUEST,
103+
"Failed to request file with target frame."
104+
).into_response()
105+
}
106+
episode = String::from("1-3");
107+
}
108+
}
109+
110+
match (frame, animated_frame) {
111+
(Some(frame), None) => return handle_static_image(
112+
env_config,
113+
season,
114+
&episode,
115+
frame,
116+
target_format
117+
).await,
118+
(None, Some(animated_frame)) =>
78119
return handle_animated_image(
79120
env_config,
80-
&season_name,
121+
season,
81122
&episode,
82123
animated_frame,
83124
scheduler
84-
).await;
85-
} else {
86-
return handle_static_image(
87-
env_config,
88-
&season_name,
89-
&episode,
90-
target_frame,
91-
target_format
92-
).await;
125+
).await,
126+
_ => return (
127+
StatusCode::BAD_REQUEST,
128+
"Failed to request file with target frame."
129+
).into_response()
93130
}
94131
}
95132

96133
async fn handle_animated_image(
97134
env_config: &EnvConfig,
98-
season_name: &str,
135+
season: &str,
99136
episode: &str,
100-
animated_frame: (&str, &str),
137+
animated_frame: (u32, u32),
101138
scheduler: Arc<Scheduler>
102139
) -> Response<Body> {
103-
let u32_frame: Option<(u32, u32)> = animated_frame.0.parse().ok()
104-
.zip(animated_frame.1.parse().ok());
105-
106-
let (start_frame, end_frame) = match u32_frame {
107-
Some(r) => r,
108-
None => return (
109-
StatusCode::BAD_REQUEST,
110-
"Failed to convert frame range to u32."
111-
).into_response(),
112-
};
140+
let (start_frame, end_frame) = animated_frame;
113141

114142
if start_frame >= end_frame || start_frame <= 0 {
115143
return (
@@ -133,25 +161,25 @@ async fn handle_animated_image(
133161
env_config,
134162
start_frame,
135163
frames,
136-
&season_name,
164+
season,
137165
&episode,
138166
scheduler
139167
).await
140168
}
141169

142170
async fn handle_static_image(
143171
env_config: &EnvConfig,
144-
season_name: &str,
172+
season: &str,
145173
episode: &str,
146-
target_frame: &str,
147-
target_format: ImageFormat
174+
frame: u32,
175+
format: ImageFormat
148176
) -> Response<Body> {
149177
let source_file_path = format!(
150178
"{}/{}{}_{}.webp",
151179
env_config.image_source_path,
152-
season_name,
180+
season,
153181
episode,
154-
target_frame
182+
frame
155183
);
156184

157185
if let Ok(exists) = fs::try_exists(&source_file_path).await {
@@ -176,5 +204,5 @@ async fn handle_static_image(
176204
).into_response(),
177205
};
178206

179-
convert_static_image(reader, target_format).await
207+
convert_static_image(reader, format).await
180208
}

src/endpoints/legacy_image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(crate) async fn handler(
5858
Some(env) => env,
5959
None => return (
6060
StatusCode::INTERNAL_SERVER_ERROR,
61-
"Failed to read server env."
61+
"Failed to load server env."
6262
).into_response(),
6363
};
6464

src/utils/convert.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ static SOURCE_FORMAT: ImageFormat = ImageFormat::WebP;
2323

2424
pub(crate) async fn convert_static_image(
2525
reader: Cursor<Vec<u8>>,
26-
target_format: ImageFormat
26+
format: ImageFormat
2727
) -> Response<Body> {
28-
if target_format == SOURCE_FORMAT {
28+
if format == SOURCE_FORMAT {
2929
return (
3030
StatusCode::OK,
3131
Body::from_stream(
@@ -46,7 +46,7 @@ pub(crate) async fn convert_static_image(
4646
).into_response(),
4747
};
4848

49-
match decoded_img.write_to(&mut buf, target_format) {
49+
match decoded_img.write_to(&mut buf, format) {
5050
Ok(_) => {
5151
buf.set_position(0);
5252

@@ -70,22 +70,22 @@ pub(crate) async fn convert_animated_image(
7070
env_config: &EnvConfig,
7171
start_frame: u32,
7272
frames: u32,
73-
season_name: &str,
73+
season: &str,
7474
episode: &str,
7575
scheduler: Arc<Scheduler>
7676
) -> Response<Body> {
7777
let start_fram_file_path = format!(
7878
"{}/{}{}_{}.webp",
7979
env_config.image_source_path,
80-
season_name,
80+
season,
8181
episode,
8282
start_frame
8383
);
8484

8585
let end_fram_file_path = format!(
8686
"{}/{}{}_{}.webp",
8787
env_config.image_source_path,
88-
season_name,
88+
season,
8989
episode,
9090
start_frame + frames
9191
);
@@ -116,7 +116,7 @@ pub(crate) async fn convert_animated_image(
116116
let file_pattern = format!(
117117
"{}/{}{}_%d.webp",
118118
env_config.image_source_path,
119-
season_name,
119+
season,
120120
episode
121121
);
122122

0 commit comments

Comments
 (0)