fix: support raw HTML upload via FileReader, fix file import flow

This commit is contained in:
2026-04-08 02:43:27 +02:00
parent 90833c5b93
commit 3bc7ad4c7c
2 changed files with 61 additions and 27 deletions

View File

@@ -10,7 +10,8 @@ pub struct AppState {
#[derive(Deserialize)]
pub struct ParseRequest {
pub source: String,
pub source: Option<String>,
pub html: Option<String>,
}
#[derive(Serialize)]
@@ -22,17 +23,26 @@ pub async fn parse_tab(
State(state): State<Arc<AppState>>,
Json(body): Json<ParseRequest>,
) -> Result<Json<domain::Song>, (StatusCode, Json<ErrorResponse>)> {
let source = if body.source.starts_with("file://") {
let path = body.source.trim_start_matches("file://");
TabSource::File(PathBuf::from(path))
let html = if let Some(raw_html) = body.html {
// Raw HTML provided directly (e.g. from browser file upload via FileReader)
raw_html
} else if let Some(source) = body.source {
let tab_source = if source.starts_with("file://") {
let path = source.trim_start_matches("file://");
TabSource::File(PathBuf::from(path))
} else {
TabSource::Url(source)
};
state.fetcher.fetch(tab_source).await.map_err(|e| {
(StatusCode::BAD_GATEWAY, Json(ErrorResponse { error: e.to_string() }))
})?
} else {
TabSource::Url(body.source)
return Err((
StatusCode::BAD_REQUEST,
Json(ErrorResponse { error: "Provide either 'source' or 'html'".into() }),
));
};
let html = state.fetcher.fetch(source).await.map_err(|e| {
(StatusCode::BAD_GATEWAY, Json(ErrorResponse { error: e.to_string() }))
})?;
let song = state.parser.parse(&html).map_err(|e| {
(StatusCode::UNPROCESSABLE_ENTITY, Json(ErrorResponse { error: e.to_string() }))
})?;