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