Compare commits
No commits in common. "49d83c29006cc2f0e2e508c8dcfb6df1253813e2" and "418ca68745a2d5fc3615be92b3982f72b98da60d" have entirely different histories.
49d83c2900
...
418ca68745
@ -23,19 +23,18 @@ pub fn add(files: Values<'_>) {
|
|||||||
for file in file_vec {
|
for file in file_vec {
|
||||||
let path = Path::new(file);
|
let path = Path::new(file);
|
||||||
println!("{}", file);
|
println!("{}", file);
|
||||||
match path.exists() {
|
match path.try_exists() {
|
||||||
true => {
|
Ok(true) => {
|
||||||
match writeln!(index_file, "{}", path.display()) {
|
match writeln!(index_file, "{}", path.display()) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(err) => eprintln!("{}", err),
|
Err(err) => eprintln!("{}", err),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
false => {
|
Ok(false) => {
|
||||||
match writeln!(index_file, "{}", path.display()) {
|
|
||||||
Ok(()) => (),
|
|
||||||
Err(err) => eprintln!("{}", err),
|
|
||||||
}
|
|
||||||
// todo can be regex
|
// todo can be regex
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,7 @@ use crate::commands::{status, config};
|
|||||||
use crate::services::req_props::ReqProps;
|
use crate::services::req_props::ReqProps;
|
||||||
use crate::services::api::ApiError;
|
use crate::services::api::ApiError;
|
||||||
use crate::services::upload_file::UploadFile;
|
use crate::services::upload_file::UploadFile;
|
||||||
use crate::services::delete_path::DeletePath;
|
|
||||||
use crate::commands::status::{State, Obj};
|
use crate::commands::status::{State, Obj};
|
||||||
use crate::store::object::{add_blob, rm_blob};
|
|
||||||
use crate::store::index;
|
|
||||||
|
|
||||||
pub fn push() {
|
pub fn push() {
|
||||||
dbg!(status::get_all_staged());
|
dbg!(status::get_all_staged());
|
||||||
@ -27,7 +24,6 @@ pub fn push() {
|
|||||||
let push_factory = PushFactory.new(obj.clone());
|
let push_factory = PushFactory.new(obj.clone());
|
||||||
match push_factory.can_push() {
|
match push_factory.can_push() {
|
||||||
PushState::Valid => push_factory.push(),
|
PushState::Valid => push_factory.push(),
|
||||||
PushState::Done => (),
|
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +35,6 @@ pub fn push() {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum PushState {
|
enum PushState {
|
||||||
Done,
|
|
||||||
Valid,
|
Valid,
|
||||||
Conflict,
|
Conflict,
|
||||||
Error,
|
Error,
|
||||||
@ -82,7 +77,7 @@ impl PushChange for New {
|
|||||||
// file doesn't exist on remote
|
// file doesn't exist on remote
|
||||||
PushState::Valid
|
PushState::Valid
|
||||||
} else {
|
} else {
|
||||||
// todo check date
|
// check date
|
||||||
PushState::Conflict
|
PushState::Conflict
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -110,82 +105,9 @@ impl PushChange for New {
|
|||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
// todo manage err
|
||||||
|
// todo remove index
|
||||||
});
|
});
|
||||||
|
|
||||||
// update tree
|
|
||||||
add_blob(&obj.path.clone(), "todo_date");
|
|
||||||
// remove index
|
|
||||||
index::rm_line(obj.path.to_str().unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct Deleted {
|
|
||||||
obj: Obj,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PushChange for Deleted {
|
|
||||||
fn can_push(&self) -> PushState {
|
|
||||||
// check if exist on server
|
|
||||||
let file_infos = tokio::runtime::Runtime::new().unwrap().block_on(async {
|
|
||||||
let res = ReqProps::new()
|
|
||||||
.set_url(&self.obj.path.to_str().unwrap())
|
|
||||||
.getlastmodified()
|
|
||||||
.send_with_err()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match res {
|
|
||||||
Ok(data) => Ok(data),
|
|
||||||
Err(ApiError::IncorrectRequest(err)) => {
|
|
||||||
if err.status() == 404 {
|
|
||||||
Ok(vec![])
|
|
||||||
} else {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(_) => Err(()),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Ok(infos) = file_infos {
|
|
||||||
if infos.len() == 0 {
|
|
||||||
// file doesn't exist on remote
|
|
||||||
PushState::Done
|
|
||||||
} else {
|
|
||||||
// todo check date
|
|
||||||
//PushState::Conflict
|
|
||||||
PushState::Valid
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
PushState::Error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn push(&self) {
|
|
||||||
let obj = &self.obj;
|
|
||||||
tokio::runtime::Runtime::new().unwrap().block_on(async {
|
|
||||||
let res = DeletePath::new()
|
|
||||||
.set_url(obj.path.to_str().unwrap())
|
|
||||||
.send_with_err()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match res {
|
|
||||||
Err(ApiError::IncorrectRequest(err)) => {
|
|
||||||
eprintln!("fatal: error deleting file {}: {}", obj.name, err.status());
|
|
||||||
std::process::exit(1);
|
|
||||||
},
|
|
||||||
Err(ApiError::RequestError(_)) => {
|
|
||||||
eprintln!("fatal: request error deleting file {}", obj.name);
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// update tree
|
|
||||||
rm_blob(&obj.path.clone());
|
|
||||||
// remove index
|
|
||||||
index::rm_line(obj.path.to_str().unwrap());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +119,7 @@ impl PushFactory {
|
|||||||
State::New => Box::new(New { obj: obj.clone() }),
|
State::New => Box::new(New { obj: obj.clone() }),
|
||||||
State::Renamed => todo!(),
|
State::Renamed => todo!(),
|
||||||
State::Modified => todo!(),
|
State::Modified => todo!(),
|
||||||
State::Deleted => Box::new(Deleted { obj: obj.clone() }),
|
State::Deleted => todo!(),
|
||||||
State::Default => todo!(),
|
State::Default => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,4 +3,3 @@ pub mod list_folders;
|
|||||||
pub mod download_files;
|
pub mod download_files;
|
||||||
pub mod req_props;
|
pub mod req_props;
|
||||||
pub mod upload_file;
|
pub mod upload_file;
|
||||||
pub mod delete_path;
|
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
use crate::services::api::{ApiBuilder, ApiError};
|
|
||||||
use reqwest::{Method, Response, Error};
|
|
||||||
|
|
||||||
pub struct DeletePath {
|
|
||||||
api_builder: ApiBuilder,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DeletePath {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
DeletePath {
|
|
||||||
api_builder: ApiBuilder::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_url(&mut self, url: &str) -> &mut DeletePath {
|
|
||||||
self.api_builder.build_request(Method::DELETE, url);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn send(&mut self) -> Result<Response, Error> {
|
|
||||||
self.api_builder.send().await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
|
|
||||||
let res = self.send().await.map_err(ApiError::RequestError)?;
|
|
||||||
if res.status().is_success() {
|
|
||||||
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
|
||||||
Ok(body)
|
|
||||||
} else {
|
|
||||||
Err(ApiError::IncorrectRequest(res))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
50
src/services/details.rs
Normal file
50
src/services/details.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
async fn send_propfind_request() -> Result<(), Box<dyn Error>> {
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
|
let mut api_endpoint = env::var("HOST").unwrap().to_owned();
|
||||||
|
api_endpoint.push_str("/remote.php/dav/files/");
|
||||||
|
let username = env::var("USERNAME").unwrap();
|
||||||
|
api_endpoint.push_str(&username);
|
||||||
|
api_endpoint.push_str("/test");
|
||||||
|
let password = env::var("PASSWORD").unwrap();
|
||||||
|
|
||||||
|
// Create a reqwest client
|
||||||
|
let client = Client::new();
|
||||||
|
|
||||||
|
// Create the XML payload
|
||||||
|
let xml_payload = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
|
||||||
|
<d:prop>
|
||||||
|
<d:getlastmodified/>
|
||||||
|
<d:getcontentlength/>
|
||||||
|
<d:getcontenttype/>
|
||||||
|
<oc:permissions/>
|
||||||
|
<d:resourcetype/>
|
||||||
|
<d:getetag/>
|
||||||
|
) </d:prop>
|
||||||
|
</d:propfind>"#;
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/xml"));
|
||||||
|
|
||||||
|
|
||||||
|
// Send the request
|
||||||
|
let response = client
|
||||||
|
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), api_endpoint)
|
||||||
|
.basic_auth(username, Some(password))
|
||||||
|
.headers(headers)
|
||||||
|
.body(xml_payload)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Handle the response
|
||||||
|
if response.status().is_success() {
|
||||||
|
let body = response.text().await?;
|
||||||
|
println!("Response body: {}", body);
|
||||||
|
} else {
|
||||||
|
println!("Request failed with status code: {}", response.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
use crate::services::api::{ApiBuilder, ApiError};
|
use crate::services::api::{ApiBuilder, ApiError};
|
||||||
use xml::reader::{EventReader, XmlEvent};
|
use xml::reader::{EventReader, XmlEvent};
|
||||||
use std::io::Cursor;
|
use std::io::{self, Cursor};
|
||||||
use reqwest::{Method, Response, Error};
|
use reqwest::{Method, IntoUrl, Response, Error};
|
||||||
|
|
||||||
pub struct ReqProps {
|
pub struct ReqProps {
|
||||||
api_builder: ApiBuilder,
|
api_builder: ApiBuilder,
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
|
use xml::reader::{EventReader, XmlEvent};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use crate::services::api::{ApiBuilder, ApiError};
|
use crate::services::api::{ApiBuilder, ApiError};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::io::{Read};
|
use std::io::{self, Read};
|
||||||
use reqwest::{Method, Response, Error};
|
use reqwest::{Method, IntoUrl, Response, Error};
|
||||||
|
|
||||||
pub struct UploadFile {
|
pub struct UploadFile {
|
||||||
api_builder: ApiBuilder,
|
api_builder: ApiBuilder,
|
||||||
|
@ -43,15 +43,3 @@ pub fn add_line(line: String) -> io::Result<()> {
|
|||||||
writeln!(file, "{}", line)?;
|
writeln!(file, "{}", line)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rm_line(line: &str) -> io::Result<()> {
|
|
||||||
let mut root = match path::nextsync_root() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => todo!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
root.push(".nextsync");
|
|
||||||
root.push("HEAD");
|
|
||||||
read::rm_line(root, line)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use crate::utils::{read, path};
|
use crate::utils::read;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub fn _read_only(mut path: PathBuf) -> File {
|
pub fn _read_only(mut path: PathBuf) -> File {
|
||||||
@ -25,15 +25,3 @@ pub fn read_line(mut path: PathBuf) -> io::Result<io::Lines<io::BufReader<File>>
|
|||||||
path.push("index");
|
path.push("index");
|
||||||
read::read_lines(path)
|
read::read_lines(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rm_line(line: &str) -> io::Result<()> {
|
|
||||||
let mut root = match path::nextsync_root() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => todo!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
root.push(".nextsync");
|
|
||||||
root.push("index");
|
|
||||||
read::rm_line(root, line)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
@ -19,7 +19,7 @@ fn parse_path(path: &Path, is_blob: bool) -> (String, String, String) {
|
|||||||
hasher.input_str(path.clone().to_str().unwrap());
|
hasher.input_str(path.clone().to_str().unwrap());
|
||||||
let hash = hasher.result_str();
|
let hash = hasher.result_str();
|
||||||
|
|
||||||
let mut line = String::from(if is_blob { "blob" } else { "tree" });
|
let mut line = String::from(if is_blob { "tree" } else { "blob" });
|
||||||
line.push_str(" ");
|
line.push_str(" ");
|
||||||
line.push_str(&hash);
|
line.push_str(&hash);
|
||||||
line.push_str(" ");
|
line.push_str(" ");
|
||||||
@ -56,36 +56,10 @@ pub fn add_tree(path: &Path) -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rm_blob(path: &Path) -> io::Result<()> {
|
|
||||||
let (line, hash, name) = parse_path(path.clone(), true);
|
|
||||||
|
|
||||||
// remove blob reference to parent
|
|
||||||
if path.iter().count() == 1 {
|
|
||||||
head::rm_line(&line)?;
|
|
||||||
} else {
|
|
||||||
rm_node(path.parent().unwrap(), &line)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove blob object
|
|
||||||
let mut root = match path::objects() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => todo!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let c = hash.clone();
|
|
||||||
let (dir, rest) = c.split_at(2);
|
|
||||||
root.push(dir);
|
|
||||||
root.push(rest);
|
|
||||||
fs::remove_file(root)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_blob(path: &Path, date: &str) -> io::Result<()> {
|
pub fn add_blob(path: &Path, date: &str) -> io::Result<()> {
|
||||||
let (line, hash, name) = parse_path(path.clone(), true);
|
let (line, hash, name) = parse_path(path.clone(), true);
|
||||||
|
|
||||||
// add blob reference to parent
|
// add tree reference to parent
|
||||||
if path.iter().count() == 1 {
|
if path.iter().count() == 1 {
|
||||||
head::add_line(line)?;
|
head::add_line(line)?;
|
||||||
} else {
|
} else {
|
||||||
@ -151,21 +125,6 @@ pub fn read_tree(tree: String) -> Option<(String, io::Lines<io::BufReader<File>>
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rm_node(path: &Path, node: &str) -> io::Result<()> {
|
|
||||||
let mut root = match path::objects() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => todo!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let (dir, rest) = hash_obj(path.clone().to_str().unwrap());
|
|
||||||
|
|
||||||
root.push(dir);
|
|
||||||
root.push(rest);
|
|
||||||
|
|
||||||
read::rm_line(root, node)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_node(path: &Path, node: &str) -> io::Result<()> {
|
fn add_node(path: &Path, node: &str) -> io::Result<()> {
|
||||||
let mut root = match path::objects() {
|
let mut root = match path::objects() {
|
||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::io::{self, BufRead, BufReader, Write};
|
use std::io::{self, BufRead};
|
||||||
use std::fs::{self, File, OpenOptions};
|
use std::fs::{self, File};
|
||||||
|
|
||||||
|
// The output is wrapped in a Result to allow matching on errors
|
||||||
|
// Returns an Iterator to the Reader of the lines of the file.
|
||||||
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||||
where P: AsRef<Path>, {
|
where P: AsRef<Path>, {
|
||||||
let file = File::open(filename)?;
|
let file = File::open(filename)?;
|
||||||
@ -16,28 +18,3 @@ pub fn read_folder(path: PathBuf) -> io::Result<Vec<PathBuf>> {
|
|||||||
entries.sort();
|
entries.sort();
|
||||||
Ok(entries)
|
Ok(entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn rm_line(path: PathBuf, line_to_del: &str) -> io::Result<()> {
|
|
||||||
let file = File::open(path.clone())?;
|
|
||||||
let reader = BufReader::new(&file);
|
|
||||||
let mut temp_file = OpenOptions::new()
|
|
||||||
.write(true)
|
|
||||||
.create(true)
|
|
||||||
.truncate(true)
|
|
||||||
.open(format!("{}_temp", path.display()))?;
|
|
||||||
|
|
||||||
for line in reader.lines() {
|
|
||||||
let l = line?;
|
|
||||||
if l.trim() != line_to_del.trim() {
|
|
||||||
writeln!(temp_file, "{}", l)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
drop(file);
|
|
||||||
drop(temp_file);
|
|
||||||
|
|
||||||
fs::remove_file(path.clone())?;
|
|
||||||
fs::rename(format!("{}_temp", path.display()), path)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user