From 2f02dd80dc6dd65bf8d5e89299bec82a7403ae82 Mon Sep 17 00:00:00 2001 From: AB Date: Sat, 18 Apr 2020 04:39:06 +0300 Subject: [PATCH] Added Size metadata. Fixing read method. --- .vscode/launch.json | 2 +- .vscode/settings.json | 2 +- src/main.rs | 68 ++++++++++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 2dee575..c83bc40 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ }, "args": [ "mnt", - "https://mus.hexor.ru" + "http://mus_.hexor.ru" ], "cwd": "${workspaceFolder}", "postDebugTask": "Umount FUSE", diff --git a/.vscode/settings.json b/.vscode/settings.json index b263409..4fbbf1e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "lldb.displayFormat": "auto", - "lldb.showDisassembly": "always", + "lldb.showDisassembly": "never", "lldb.dereferencePointers": true } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6b25b6c..45d4395 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,8 @@ use reqwest::blocking::Client; use reqwest::blocking::Response; use reqwest::header::CONTENT_LENGTH; use std::collections::BTreeMap; -use std::ffi::OsStr; use std::env; +use std::ffi::OsStr; use time::Timespec; //use http::Method; @@ -31,6 +31,7 @@ pub struct Track { pub format: Option, pub filetype: Option, pub path: Option, + pub Size: Option, } const CACHE_HEAD: i64 = 1024 * 1024; @@ -95,12 +96,27 @@ impl JsonFilesystem { }; attrs.insert(1, attr); inodes.insert("/".to_string(), 1); + let client = Client::new(); + let mut resp: Response; for (i, track) in tree.iter().enumerate() { let basename = get_basename(track.path.as_ref()).unwrap().to_string(); + /* + let full_url = format!("{}/{}", server, track.path.as_ref().unwrap().to_string()); + resp = client.head(full_url.as_str()).send().unwrap(); + let content_length = resp + .headers() + .get(CONTENT_LENGTH) + .unwrap() + .to_str() + .unwrap() + .parse::() + .unwrap(); + println!("{} len is {}", basename, content_length); + */ let attr = FileAttr { ino: i as u64 + 2, //size: 1024 * 1024 * 1024 as u64, - size: 591646132, + size: track.Size.unwrap() as u64, blocks: 0, atime: ts, mtime: ts, @@ -203,32 +219,29 @@ impl Filesystem for JsonFilesystem { if content_length > offset { print!("Content len {:?} ", content_length); - let mut end_of_chunk = offset - 1 + size as i64; - let range = format!( - "bytes={}-{}", - offset, - if (end_of_chunk) > content_length { - content_length - } else { - end_of_chunk - } - ); + let end_of_chunk = if size - 1 + offset as u32 > content_length as u32 { + content_length + } else { + (size + offset as u32) as i64 + }; + let range = format!("bytes={}-{}", offset, end_of_chunk - 1); // if it's beginning of file... - if (offset - 1 + size as i64) < CACHE_HEAD { + if end_of_chunk < CACHE_HEAD { // cleaning cache before. it should be less than MAX_CACHE_SIZE bytes if self.buffer_head.len() as i64 * CACHE_HEAD > MAX_CACHE_SIZE { let (key, _) = self.buffer_head.iter_mut().next().unwrap(); let key_cpy: String = key.to_string(); - self.buffer_head.remove(&key_cpy); - print!(" *Cache Cleaned* "); + if *key == key_cpy { + self.buffer_head.remove(&key_cpy); + print!(" *Cache Cleaned* "); + } } // looking for CACHE_HEAD bytes file beginning in cache if self.buffer_head.contains_key(id.as_str()) { print!("Hit head cache! "); - chunk = self.buffer_head[id.as_str()] - [offset as usize..(size + offset as u32) as usize] + chunk = self.buffer_head[id.as_str()][offset as usize..end_of_chunk as usize] .to_vec() .clone(); reply.data(&chunk); @@ -241,9 +254,9 @@ impl Filesystem for JsonFilesystem { format!( "bytes=0-{}", if CACHE_HEAD > content_length { - content_length + content_length - 1 } else { - CACHE_HEAD + CACHE_HEAD - 1 } ), ) @@ -251,11 +264,6 @@ impl Filesystem for JsonFilesystem { .unwrap(); let response = resp.bytes().unwrap(); self.buffer_head.insert(id.to_string(), response.to_vec()); - end_of_chunk = if content_length < end_of_chunk { - content_length - } else { - end_of_chunk - }; chunk = response[offset as usize..end_of_chunk as usize].to_vec(); reply.data(&chunk); } @@ -274,7 +282,7 @@ impl Filesystem for JsonFilesystem { " Len: {}, Chunk {} - {}", chunk.len(), offset, - offset - 1 + chunk.len() as i64 + offset + chunk.len() as i64 ); } else { println!( @@ -317,14 +325,20 @@ fn main() { let mountpoint = match env::args().nth(1) { Some(path) => path, None => { - println!("Usage: {} ", env::args().nth(0).unwrap()); + println!( + "Usage: {} ", + env::args().nth(0).unwrap() + ); return; } }; let server = match env::args().nth(2) { Some(server) => server, None => { - println!("Usage: {} ", env::args().nth(0).unwrap()); + println!( + "Usage: {} ", + env::args().nth(0).unwrap() + ); return; } };