use std::ffi::OsStr;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::Arc;
use flate2::Compression;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use crate::error::{Aria2Error, Result};
use crate::request::request_group::RequestGroup;
use super::SessionEntry;
use super::deserialization::deserialize;
use super::serialize::serialize_groups_with_results;
use crate::request::request_group::DownloadResult;
pub async fn load_from_file(path: &Path) -> Result<Vec<SessionEntry>> {
let bytes = tokio::fs::read(path).await.map_err(|e| {
Aria2Error::Io(format!(
"Failed to read session file {}: {}",
path.display(),
e
))
})?;
let content = decode_session_content(path, &bytes)?;
deserialize(&content)
}
pub async fn save_to_file(
path: &Path,
groups: &[Arc<std::sync::RwLock<RequestGroup>>],
) -> Result<()> {
save_to_file_with_results(path, groups, &[]).await
}
pub async fn save_to_file_with_results(
path: &Path,
groups: &[Arc<std::sync::RwLock<RequestGroup>>],
results: &[DownloadResult],
) -> Result<()> {
let content = encode_session_content(path, &serialize_groups_with_results(groups, results)?)?;
let tmp_path = path.with_extension("sess.tmp");
tokio::fs::write(&tmp_path, &content).await.map_err(|e| {
Aria2Error::Io(format!(
"Failed to write session temp file {}: {}",
tmp_path.display(),
e
))
})?;
tokio::fs::rename(&tmp_path, path).await.map_err(|e| {
Aria2Error::Io(format!(
"Failed to rename session file {}: {}",
path.display(),
e
))
})
}
pub async fn save_to_file_with_entries(path: &Path, entries: &[SessionEntry]) -> Result<()> {
let mut text = String::new();
for entry in entries {
text.push_str(&entry.serialize());
text.push('\n');
}
let content = encode_session_content(path, &text)?;
let tmp_path = path.with_extension("sess.tmp");
tokio::fs::write(&tmp_path, &content).await.map_err(|e| {
Aria2Error::Io(format!(
"Failed to write session temp file {}: {}",
tmp_path.display(),
e
))
})?;
tokio::fs::rename(&tmp_path, path).await.map_err(|e| {
Aria2Error::Io(format!(
"Failed to rename session file {}: {}",
path.display(),
e
))
})
}
fn is_gzip_path(path: &Path) -> bool {
path.extension() == Some(OsStr::new("gz"))
}
fn decode_session_content(path: &Path, bytes: &[u8]) -> Result<String> {
if !is_gzip_path(path) {
return String::from_utf8(bytes.to_vec()).map_err(|e| {
Aria2Error::Io(format!(
"Failed to decode session file {}: {}",
path.display(),
e
))
});
}
let mut decoder = GzDecoder::new(bytes);
let mut content = String::new();
decoder.read_to_string(&mut content).map_err(|e| {
Aria2Error::Io(format!(
"Failed to decompress session file {}: {}",
path.display(),
e
))
})?;
Ok(content)
}
fn encode_session_content(path: &Path, text: &str) -> Result<Vec<u8>> {
if !is_gzip_path(path) {
return Ok(text.as_bytes().to_vec());
}
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(text.as_bytes()).map_err(|e| {
Aria2Error::Io(format!(
"Failed to compress session file {}: {}",
path.display(),
e
))
})?;
encoder.finish().map_err(|e| {
Aria2Error::Io(format!(
"Failed to finish compressed session file {}: {}",
path.display(),
e
))
})
}