same filename bug

This commit is contained in:
EvilMuffinHa 2022-03-02 17:55:50 -05:00
parent 19c2ad1a85
commit 2435a5728f

View File

@ -5,16 +5,17 @@ extern crate rocket;
#[macro_use]
extern crate lazy_static;
use std::path::{PathBuf, Path};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::ffi::OsStr;
use std::collections::HashMap;
use rocket::response::{NamedFile, Redirect};
use std::panic;
use std::path::{PathBuf, Path};
use rocket::http::{Cookie, Cookies, ContentType, Status};
use rocket::response::{NamedFile, Redirect};
use rocket::request::{self, Request, Form, FromRequest};
use rocket_contrib::templates::Template;
use rocket_upload::MultipartDatas;
use rocket_upload::{FilePart, MultipartDatas};
use serde::{Serialize, Deserialize};
lazy_static! {
@ -206,37 +207,42 @@ fn file_get(path: PathBuf, _guard: GenericUser) -> Option<NamedFile> {
rocket::response::NamedFile::open(dir.as_path()).ok()
}
#[post("/upload/<path..>", data = "<data>")]
fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas, _guard: AdminUser) -> Redirect {
let dir = DIR.as_path().join(path.clone());
fn upload(dir: PathBuf, data:MultipartDatas) {
let prev_hook = panic::take_hook();
panic::set_hook(Box::new(|_| {}));
let _ = panic::catch_unwind(||
for f in data.files {
if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() {
f.persist(&dir);
}
else {
for i in 0.. {
if !Path::new(&format!("{}/{}.{}", dir.to_str().unwrap(), f.filename, i)).exists() {
f.persist(&dir);
let file = FilePart {
name: f.name.to_string(),
path: f.path.to_string(),
filename: format!("{}.{}", f.filename, i)
};
file.persist(&dir);
break
}
}
}
});
panic::set_hook(prev_hook);
}
#[post("/upload/<path..>", data = "<data>")]
fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas, _guard: AdminUser) -> Redirect {
let dir = DIR.as_path().join(path.clone());
upload(dir, data);
Redirect::to(uri!(get_dir_entry: path))
}
#[post("/upload", data = "<data>")]
fn upload_root(_content_type: &ContentType, data:MultipartDatas, _guard: AdminUser) -> Redirect {
let dir = DIR.as_path();
for f in data.files {
if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() {
f.persist(dir);
}
for i in 0.. {
if !Path::new(&format!("{}/{}.{}", dir.to_str().unwrap(), f.filename, i)).exists() {
f.persist(dir);
break
}
}
}
let dir = DIR.as_path().to_path_buf();
upload(dir, data);
Redirect::to(uri!(get_root))
}