feat(download): simple downloader working without streaming

This commit is contained in:
grimhilt 2024-10-13 22:07:42 +02:00
parent 3853d1fe27
commit 09f1b26a50
9 changed files with 73 additions and 32 deletions

View File

@ -1,14 +1,13 @@
use super::init::{self, InitArgs};
use super::init;
use crate::config::config::Config;
use crate::services::{downloader::Downloader, enumerator::Enumerator, service::Service};
use crate::store::{
nsobject::NsObject,
structs::{self, to_obj_path},
structs,
};
use regex::Regex;
use std::fs;
use std::io::{self, BufRead};
use std::path::PathBuf;
pub struct CloneArgs {
pub remote: String,
@ -35,7 +34,7 @@ pub async fn exec(args: CloneArgs, config: Config) {
let service = Service::from(&url_props);
let Ok((files, folders)) = Enumerator::new(&service)
.set_path(url_props.path.to_string())
.set_path(String::new()) // use base_path of the service
// .set_depth(args.depth)
.get_properties(vec![])
.enumerate()
@ -57,8 +56,14 @@ pub async fn exec(args: CloneArgs, config: Config) {
}
let downloader = Downloader::new(&service)
.set_files(files.into_iter().map(|file| file.href).collect())
.download();
.set_files(
files
.into_iter()
.map(|file| file.abs_path().to_string())
.collect(),
)
.download()
.await;
}
pub struct UrlProps<'a> {

View File

@ -1,7 +1,7 @@
use crate::config::config::Config;
use crate::services::req_props::{Props, ReqProps};
use crate::store::object::Obj;
use crate::store::{indexer::Indexer, structs};
// use crate::services::req_props::{Props, ReqProps};
// use crate::store::object::Obj;
// use crate::store::{indexer::Indexer, structs};
pub struct TestArgs {}

View File

@ -1,27 +1,42 @@
use crate::services::service::{Request, Service};
use reqwest::Error;
use reqwest::{header::HeaderMap, Method, Url};
use reqwest::{Client, ClientBuilder, RequestBuilder};
use crate::store::nsobject::NsObject;
use std::{fs::OpenOptions, io::Write};
pub struct Download<'a> {
request: Request<'a>,
obj_path: String,
}
impl<'a> Download<'a> {
pub fn new(service: &'a Service) -> Self {
Download {
request: Request::new(service),
obj_path: String::new(),
}
}
pub fn set_obj_path(mut self, obj_path: String) -> Self {
self.request.get(obj_path);
self.request.get(obj_path.clone());
self.obj_path = obj_path
.strip_prefix("/")
.unwrap_or(&self.obj_path.clone())
.to_string();
self
}
pub async fn send(&mut self) -> Result<(), reqwest::Error> {
let res = self.request.send().await;
let body = res.unwrap().text().await?;
let body = res.unwrap().bytes().await?;
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(self.obj_path.clone())
.unwrap();
file.write_all(&body.to_vec()).unwrap();
NsObject::from_local_path(&self.obj_path.clone().into())
.save()
.unwrap();
Ok(())
}
}

View File

@ -18,9 +18,9 @@ impl<'a> Downloader<'a> {
self
}
pub fn download(&self) {
pub async fn download(&self) {
for file in self.files.clone() {
Download::new(self.service).set_obj_path(file).send();
Download::new(self.service).set_obj_path(file).send().await;
}
}
}

View File

@ -1,8 +1,5 @@
use crate::services::service::{Request, Service};
use crate::store::{
object::Obj,
structs::{to_obj_path, ObjPath},
};
use crate::store::structs::{to_obj_path, ObjPath};
use crate::utils::path;
use serde::Deserialize;
use serde_xml_rs::from_str;

View File

@ -1,5 +1,5 @@
use crate::commands::clone::UrlProps;
use reqwest::{header::HeaderMap, Method, Url};
use reqwest::{header::HeaderMap, Method};
use reqwest::{Client, ClientBuilder, RequestBuilder};
const USER_AGENT: &str = "Nextsync";
@ -70,6 +70,7 @@ impl Service {
let mut final_url = self.url_base.clone();
final_url.push_str("/");
final_url.push_str(&self.user);
final_url.push_str(&self.base_path);
final_url.push_str(url);
final_url
}

View File

@ -5,7 +5,7 @@ use crate::store::{
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use std::time::UNIX_EPOCH;
type NsObjectChilds = Vec<Box<NsObject>>;

View File

@ -3,12 +3,12 @@ use crate::store::{
structs::{NsObjPath, ObjPath},
};
use crate::utils::path;
use std::env;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::path::PathBuf;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
env, fmt, fs, io,
path::PathBuf,
sync::OnceLock,
time::SystemTime,
};
const MAX_SIZE_TO_USE_HASH: u64 = 12 * 1024 * 1024;
@ -25,6 +25,12 @@ pub enum ObjType {
Tree,
}
impl fmt::Display for ObjType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl From<ObjType> for u8 {
fn from(variant: ObjType) -> Self {
variant as u8
@ -92,10 +98,12 @@ impl Obj {
if !self.obj_path.exists() {
return ObjStatus::Deleted;
}
if *self != nsobj {
return ObjStatus::Modified;
if self.obj_type != ObjType::Tree && nsobj.obj_type != ObjType::Tree {
if *self != nsobj {
return ObjStatus::Modified;
}
}
todo!("Read status");
ObjStatus::Unchanged
})
}
@ -191,7 +199,13 @@ impl Obj {
impl PartialEq<NsObject> for Obj {
fn eq(&self, other: &NsObject) -> bool {
if self.obj_type != other.obj_type {
eprintln!("Trying to compare different obj type");
eprintln!(
"{}",
format!(
"Trying to compare different obj type ({} != {})",
self.obj_type, other.obj_type
)
);
return false;
}

View File

@ -102,6 +102,15 @@ impl Into<ObjPath> for PathBuf {
}
}
impl Into<ObjPath> for String {
fn into(self) -> ObjPath {
ObjPath {
path: self.into(),
abs: OnceLock::new(),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct NsObjPath {
path: PathBuf,