diff --git a/crates/bili_sync/src/bilibili/danmaku/ass_writer.rs b/crates/bili_sync/src/bilibili/danmaku/ass_writer.rs index 3adf1ae..b0f069b 100644 --- a/crates/bili_sync/src/bilibili/danmaku/ass_writer.rs +++ b/crates/bili_sync/src/bilibili/danmaku/ass_writer.rs @@ -184,7 +184,7 @@ impl<'a, W: AsyncWrite> AssWriter<'a, W> { } } -fn escape_text(text: &str) -> Cow { +fn escape_text(text: &'_ str) -> Cow<'_, str> { let text = text.trim(); if memchr::memchr(b'\n', text.as_bytes()).is_some() { Cow::from(text.replace('\n', "\\N")) diff --git a/crates/bili_sync/src/bilibili/video.rs b/crates/bili_sync/src/bilibili/video.rs index e8852f5..c92e9cb 100644 --- a/crates/bili_sync/src/bilibili/video.rs +++ b/crates/bili_sync/src/bilibili/video.rs @@ -91,7 +91,7 @@ impl<'a> Video<'a> { .collect()) } - pub async fn get_danmaku_writer(&self, page: &'a PageInfo) -> Result { + pub async fn get_danmaku_writer(&self, page: &'a PageInfo) -> Result> { let tasks = FuturesUnordered::new(); for i in 1..=page.duration.div_ceil(360) { tasks.push(self.get_danmaku_segment(page, i as i64)); diff --git a/crates/bili_sync/src/task/http_server.rs b/crates/bili_sync/src/task/http_server.rs index 307b6ee..dc72f22 100644 --- a/crates/bili_sync/src/task/http_server.rs +++ b/crates/bili_sync/src/task/http_server.rs @@ -26,7 +26,7 @@ pub async fn http_server( log_writer: LogHelper, ) -> Result<()> { let app = router() - .fallback_service(get(frontend_files)) + .fallback_service(get(frontend_files).head(frontend_files)) .layer(Extension(database_connection)) .layer(Extension(bili_client)) .layer(Extension(log_writer)); @@ -48,34 +48,51 @@ async fn frontend_files(request: Request) -> impl IntoResponse { }; let mime_type = content.mime_type(); let content_type = mime_type.as_deref().unwrap_or("application/octet-stream"); - if cfg!(debug_assertions) { - ( - [(header::CONTENT_TYPE, content_type)], - // safety: `RustEmbed` returns uncompressed files directly from the filesystem in debug mode - content.data().unwrap(), - ) - .into_response() - } else { - let accepted_encodings = request - .headers() - .get(header::ACCEPT_ENCODING) - .and_then(|v| v.to_str().ok()) - .map(|s| s.split(',').map(str::trim).collect::>()) - .unwrap_or_default(); - for (encoding, data) in [("br", content.data_br()), ("gzip", content.data_gzip())] { - if accepted_encodings.contains(encoding) { - if let Some(data) = data { - return ( - [ - (header::CONTENT_TYPE, content_type), - (header::CONTENT_ENCODING, encoding), - ], - data, - ) - .into_response(); - } + let default_headers = [ + (header::CONTENT_TYPE, content_type), + (header::CACHE_CONTROL, "no-cache"), + (header::ETAG, &content.hash()), + ]; + if let Some(if_none_match) = request.headers().get(header::IF_NONE_MATCH) { + if let Ok(client_etag) = if_none_match.to_str() { + if client_etag == content.hash() { + return (StatusCode::NOT_MODIFIED, default_headers).into_response(); } } - "Unsupported Encoding".into_response() } + if request.method() == axum::http::Method::HEAD { + return (StatusCode::OK, default_headers).into_response(); + } + if cfg!(debug_assertions) { + // safety: `RustEmbed` returns uncompressed files directly from the filesystem in debug mode + return (StatusCode::OK, default_headers, content.data().unwrap()).into_response(); + } + let accepted_encodings = request + .headers() + .get(header::ACCEPT_ENCODING) + .and_then(|v| v.to_str().ok()) + .map(|s| s.split(',').map(str::trim).collect::>()) + .unwrap_or_default(); + for (encoding, data) in [("br", content.data_br()), ("gzip", content.data_gzip())] { + if accepted_encodings.contains(encoding) { + if let Some(data) = data { + return ( + StatusCode::OK, + [ + (header::CONTENT_TYPE, content_type), + (header::CACHE_CONTROL, "no-cache"), + (header::ETAG, &content.hash()), + (header::CONTENT_ENCODING, encoding), + ], + data, + ) + .into_response(); + } + } + } + ( + StatusCode::NOT_ACCEPTABLE, + "Client must support gzip or brotli compression", + ) + .into_response() } diff --git a/crates/bili_sync/src/utils/task_notifier.rs b/crates/bili_sync/src/utils/task_notifier.rs index c1009d8..4da2788 100644 --- a/crates/bili_sync/src/utils/task_notifier.rs +++ b/crates/bili_sync/src/utils/task_notifier.rs @@ -42,7 +42,7 @@ impl TaskStatusNotifier { } } - pub async fn start_running(&self) -> MutexGuard<()> { + pub async fn start_running(&'_ self) -> MutexGuard<'_, ()> { let lock = self.mutex.lock().await; let _ = self.tx.send(Arc::new(TaskStatus { is_running: true,