#![allow(unused)]
use std::path::PathBuf;
use ani_rs::business_error::BusinessError;
use ani_rs::objects::{AniObject, AniRef};
use ani_rs::AniEnv;
use request_client::check::file::DownloadPathError;
use request_client::client::error::CreateTaskError;
use request_client::RequestClient;
use request_core::config::{TaskConfig, Version};
use request_core::info::TaskInfo;
use request_utils::context::{is_stage_context, Context};
use super::bridge::{DownloadConfig, DownloadTask};
use crate::api9::bridge::{DownloadInfo, UploadConfig, UploadTask};
use crate::api9::callback::CallbackManager;
use crate::constant::*;
use crate::seq::TaskSeq;
#[ani_rs::native]
pub fn check_config(
env: &AniEnv,
context: AniRef,
config: UploadConfig,
) -> Result<i64, BusinessError> {
let context = AniObject::from(context);
let seq = TaskSeq::next().0.get();
info!("Check task, seq: {}", seq);
let context = Context::new(env, &context);
let mut config: TaskConfig = config.into();
config.bundle_type = context.get_bundle_type() as u32;
config.bundle = context.get_bundle_name();
match RequestClient::get_instance().check_config(context, seq, config) {
Ok(()) => Ok(seq as i64),
Err(CreateTaskError::DownloadPath(err)) => {
let message = match err {
DownloadPathError::EmptyPath => {
"Invalid file or file system error, File path is empty".to_string()
}
DownloadPathError::TooLongPath => {
"Invalid file or file system error, File path exceeds maximum allowed length".to_string()
}
DownloadPathError::BundleNameNotMap => {
"Invalid file or file system error, File path bundle name does not match the application".to_string()
}
DownloadPathError::AlreadyExists => {
"Invalid file or file system error, File already exists, set overwrite=true to replace".to_string()
}
DownloadPathError::CreateFile(e) => {
format!("Invalid file or file system error, Failed to create file: {}", e)
}
DownloadPathError::SetPermission(e) => {
format!("Invalid file or file system error, Failed to set file permissions: {}", e)
}
DownloadPathError::AclAccess(e) => {
format!("Invalid file or file system error, ACL permission denied: {}", e)
}
_ => "Invalid file or file system error.".to_string(),
};
return Err(BusinessError::new(13400001, message))
}
Err(CreateTaskError::Code(code)) => {
return Err(BusinessError::new(code, "Upload failed.".to_string()))
}
}
}
#[ani_rs::native]
pub fn upload_file(env: &AniEnv, context: AniRef, seq: i64) -> Result<UploadTask, BusinessError> {
let context = AniObject::from(context);
let context = Context::new(env, &context);
let task = match RequestClient::get_instance().create_task(context, seq as u64) {
Ok(task_id) => UploadTask {
task_id: task_id.to_string(),
},
Err(CreateTaskError::DownloadPath(err)) => {
let message = match err {
DownloadPathError::EmptyPath => {
"Invalid file or file system error, File path is empty".to_string()
}
DownloadPathError::TooLongPath => {
"Invalid file or file system error, File path exceeds maximum allowed length".to_string()
}
DownloadPathError::BundleNameNotMap => {
"Invalid file or file system error, File path bundle name does not match the application".to_string()
}
DownloadPathError::AlreadyExists => {
"Invalid file or file system error, File already exists, set overwrite=true to replace".to_string()
}
DownloadPathError::CreateFile(e) => {
format!("Invalid file or file system error, Failed to create file: {}", e)
}
DownloadPathError::SetPermission(e) => {
format!("Invalid file or file system error, Failed to set file permissions: {}", e)
}
DownloadPathError::AclAccess(e) => {
format!("Invalid file or file system error, ACL permission denied: {}", e)
}
_ => "Invalid file or file system error.".to_string(),
};
return Err(BusinessError::new(13400001, message))
}
Err(CreateTaskError::Code(code)) => {
return Err(BusinessError::new(code, "Upload failed.".to_string()))
}
};
let tid = task.task_id.parse().unwrap();
match RequestClient::get_instance().start(tid) {
Ok(_) => {
info!("Api9 upload started successfully, seq: {}", seq);
Ok(task)
}
Err(e) => {
error!("Api9 upload start failed, error: {}", e);
Err(BusinessError::new(
e,
format!("Upload start failed with error code: {}", e),
))
}
}
}
#[ani_rs::native]
pub fn delete(this: UploadTask) -> Result<bool, BusinessError> {
let task_id = this.task_id.parse().unwrap();
match RequestClient::get_instance().remove(task_id) {
Err(e) if e == ExceptionErrorCode::E_PERMISSION as i32 => {
return Err(BusinessError::new(e, "Failed to delete upload task".to_string()));
}
_ => {}
}
CallbackManager::get_instance().remove_task(task_id);
Ok(true)
}