Добавлен фитбек переноса файлов

This commit is contained in:
2026-05-07 14:32:22 +03:00
parent a3fc5e2940
commit ff0652b992
4 changed files with 83 additions and 18 deletions

View File

@@ -121,6 +121,7 @@ pub async fn run_web_server(config: Config) -> anyhow::Result<()> {
.route("/api/videos", get(list_videos))
.route("/api/videos/{filename}", get(stream_video))
.route("/api/videos/{filename}/move", post(move_video))
.route("/api/videos/{filename}/status", get(get_video_status))
.layer(TraceLayer::new_for_http())
.with_state(state);
@@ -361,15 +362,37 @@ async fn move_video(
.source_access
.read_file(&filename)
.await
.map_err(|e| AppError(StatusCode::NOT_FOUND, e.to_string()))?;
.map_err(|e| AppError(StatusCode::NOT_FOUND, format!("File not found: {}", e)))?;
// Определяем целевую подпапку по имени файла
let subfolder = if filename.contains("_Today") {
"Today"
} else if filename.contains("_Tomorrow") {
"Tomorrow"
} else {
"Data"
};
let dest_filename = format!("{}/{}", subfolder, filename);
// Проверяем, существует ли уже файл в целевой папке
if state.dest_access.read_file(&dest_filename).await.is_ok() {
return Ok((
StatusCode::OK,
format!("File {} already exists in {} folder.", filename, subfolder),
));
}
// Запись
state
.dest_access
.write_file(&filename, &data)
.write_file(&dest_filename, &data)
.await
.map_err(|e| AppError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let mut message = format!("File {} moved successfully.", filename);
let mut message = format!(
"File {} moved successfully to {} folder.",
filename, subfolder
);
if remove {
state
@@ -405,6 +428,25 @@ async fn fa_brands_woff2() -> impl IntoResponse {
)
}
async fn get_video_status(
Path(filename): Path<String>,
State(state): State<AppState>,
) -> Result<Json<serde_json::Value>, AppError> {
let subfolder = if filename.contains("_Today") {
"Today"
} else if filename.contains("_Tomorrow") {
"Tomorrow"
} else {
"Data"
};
let dest_filename = format!("{}/{}", subfolder, filename);
let exists = state.dest_access.read_file(&dest_filename).await.is_ok();
Ok(Json(serde_json::json!({
"exists_in_dest": exists,
"subfolder": subfolder
})))
}
struct AppError(StatusCode, String);
impl IntoResponse for AppError {