-
Notifications
You must be signed in to change notification settings - Fork 4
[Added] Now a filebrowser is accessible and attachements can be linked. #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
saschagrunert
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for the contribution. We need some rework to this pull request.
src/lib.rs
Outdated
| /// Gather new content | ||
| let md_path = PathBuf::from(&directory).join("**").join("*.md"); | ||
| if !Path::new(&directory).is_dir() { | ||
| if !Path::new(&directory).is_dir () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this change :)
src/lib.rs
Outdated
| info!("Creating directory for file input: {}.", file_directory); | ||
| fs::create_dir(file_directory); | ||
| } | ||
| for path in self.paths.clone(){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clone can be avoided by iterating over the references: for path in &self.paths
src/lib.rs
Outdated
| let mut count = 0; | ||
|
|
||
| //create the path for the html site or just find it | ||
| let page_files_path = format!("{}/{}", file_directory, path.to_str().unwrap()).replace(".md",""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend to use the join based approach here.
src/lib.rs
Outdated
| //create the path for the html site or just find it | ||
| let page_files_path = format!("{}/{}", file_directory, path.to_str().unwrap()).replace(".md",""); | ||
| if !Path::new(page_files_path.as_str()).exists(){ | ||
| info!("Creating directory for {}'s files: {}.", path.to_str().unwrap(), page_files_path.as_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unwraps can lead into panics, please avoid these by using the display method.
src/lib.rs
Outdated
| //add the path | ||
| let file_path = Path::new(page_files_path.as_str()); | ||
| //read all the files | ||
| let files = file_path.read_dir().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add error handling here as well.
src/lib.rs
Outdated
| pub fn serve(&self, output_directory: &str) -> Result<()> { | ||
| // Create a default listening address | ||
| let addr = "localhost:5000"; | ||
| let addr = "localhost:3000"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend to use a higher ranged port here. The address should be a static string somewhere in the lib, since we have no configuration options.
src/lib.rs
Outdated
| let output_directory_string = output_directory.to_owned(); | ||
|
|
||
| //maybe this shoul be stored in a separated file? | ||
| let pdf_mime: Mime = "application/pdf".parse().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid "live" unwrapping, this can be done before main in a static string or by using the lazy static crate.
src/lib.rs
Outdated
| if !path.exists() { | ||
| return Ok(Response::with(status::InternalServerError)); | ||
| return Ok(Response::with((ContentType::html().0, | ||
| status::NotFound, html_err))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong indent my dear 🐻
src/lib.rs
Outdated
| } | ||
|
|
||
| let mut f = match File::open(path) { | ||
| let mut f = match File::open(path.clone()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can also open a reference here to avoid cloning.
src/lib.rs
Outdated
| status::NotFound, html_err))), | ||
| }; | ||
|
|
||
| match path.clone().to_str(){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid this clone too.
Drogglbecher
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All in all there are many unwraps in waiting to be purged ;)
src/lib.rs
Outdated
| "\n<a href='http://localhost:3000/files/{}/{}'>{}</a><br>\n", | ||
| path.to_str().unwrap().replace(".md", ""), | ||
| current_entry.file_name().into_string().unwrap().replace("\n","."), | ||
| current_entry.file_name().into_string().unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can try to replace these 3 unwraps above with ok_or_else
src/lib.rs
Outdated
|
|
||
| let mut html_file = OpenOptions::new().read(true).write(true).create(true). | ||
| open(format!("output/{}", path.to_str().unwrap(). | ||
| replace(".md", ".html"))).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These unwraps can be handled too
src/lib.rs
Outdated
| } | ||
| //check if there are no files attached | ||
| if count == 0 { | ||
| info!("No files found so far for {}. Simply add your files to the folders in 'files'.", path.to_str().unwrap()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better reading some line breaks would be good. In l. 186 too.
src/lib.rs
Outdated
|
|
||
| Ok(()) | ||
| } | ||
| /// read all the files from current paths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This description not really describes what this function actually does (and to be honest, i have no idea too)
| help: Enable integrated HTTP server to serve contents from output directory. | ||
| long: www | ||
| short: w | ||
| - file_directory: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since input and output directory we have now additionally a file directory and as a user I would have no idea wherefore this is and the description is not really helpful.
…mited. Better doc comments and minor fixes.
saschagrunert
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now, what do you think @Drogglbecher
|
Yes, thanks. Please merge in the current master and resolve the failing check, after that I will have a look, it's a little bit hard to read it this way. There are some "coding style issues" too but we can resolve them later ;) @mbu93 |
Conflicts: Cargo.lock Cargo.toml src/lib.rs src/main.rs
Drogglbecher
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do cargo update, I think this should resolve the problems with nightly build ;)
After these changes the rest looks ok so far. Thanks ;)
| for entry in files { | ||
| let current_entry = match entry { | ||
| Ok(entry) => entry, | ||
| _ => panic!("Couldn't read the path!"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better return an error with a meaningful description here.
|
|
||
| //create the path for the html site or just find it | ||
| let page_files_path = file_directory.join(&path.path).to_str().ok_or_else(|| | ||
| "Couldn't create Path to html file!")?.replace(".md",""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message is a bit weird. It would be also better to break the lines here for better reading.
file_directory.join(&path.path)
.to_str()
.ok_or_else(|| "Couldn't create Path to html file!")?
.replace(".md","");
| "Couldn't create Path to html file!")?.replace(".md",""); | ||
|
|
||
| if !PathBuf::from(page_files_path.as_str()).exists(){ | ||
| info!("Creating directory for {}'s files: {}.", path.path.to_str().ok_or_else(|| "Path not found!")?, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good when specifying which path can't be found. For this we should rework the error messages for the whole project, there are much things in like this ^^
| let files = file_path.read_dir()?; | ||
|
|
||
| //now attach the files to the html sites folder | ||
| for entry in files { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you can get rid of the extra var but write
for entry in file_path.read_dir()? { ...
| html_file.read_to_string(&mut buffer); | ||
| buffer += &link.as_str(); | ||
| info!("Creating link to {:?} for {:?}", current_entry.file_name(), path.path.to_str() | ||
| .ok_or_else(|| "Entry is corrupted!")?.replace(".md", ".html")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
info!("Creating link to {:?} for {:?}",
current_entry.file_name(),
path.path.to_str()
.ok_or_else(|| "Entry is corrupted!")?
.replace(".md", ".html"));
|
|
||
| else {Ok(Response::with((status::Ok, f)))} | ||
| }, | ||
| _ => panic!("Invalid Path."), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here also return an Error.
The wiki now automatically creates links to the files in the file-folder of the generated site. If there is no file-folder found it will initially create one and add the neccessary paths(same as the html paths). To add a link files can simply be dropped. In order to do so I extended the Server Responses for a few filetypes.