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

View File

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

View File

@ -1,27 +1,42 @@
use crate::services::service::{Request, Service}; use crate::services::service::{Request, Service};
use reqwest::Error; use crate::store::nsobject::NsObject;
use reqwest::{header::HeaderMap, Method, Url}; use std::{fs::OpenOptions, io::Write};
use reqwest::{Client, ClientBuilder, RequestBuilder};
pub struct Download<'a> { pub struct Download<'a> {
request: Request<'a>, request: Request<'a>,
obj_path: String,
} }
impl<'a> Download<'a> { impl<'a> Download<'a> {
pub fn new(service: &'a Service) -> Self { pub fn new(service: &'a Service) -> Self {
Download { Download {
request: Request::new(service), request: Request::new(service),
obj_path: String::new(),
} }
} }
pub fn set_obj_path(mut self, obj_path: String) -> Self { 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 self
} }
pub async fn send(&mut self) -> Result<(), reqwest::Error> { pub async fn send(&mut self) -> Result<(), reqwest::Error> {
let res = self.request.send().await; 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(()) Ok(())
} }
} }

View File

@ -18,9 +18,9 @@ impl<'a> Downloader<'a> {
self self
} }
pub fn download(&self) { pub async fn download(&self) {
for file in self.files.clone() { 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::services::service::{Request, Service};
use crate::store::{ use crate::store::structs::{to_obj_path, ObjPath};
object::Obj,
structs::{to_obj_path, ObjPath},
};
use crate::utils::path; use crate::utils::path;
use serde::Deserialize; use serde::Deserialize;
use serde_xml_rs::from_str; use serde_xml_rs::from_str;

View File

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

View File

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

View File

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