Compare commits

..

No commits in common. "master" and "v0.1.0" have entirely different histories.

4 changed files with 29 additions and 59 deletions

View File

@ -1,21 +0,0 @@
FROM rust:1.59 as builder
WORKDIR /build
COPY ./src/ /build/src
COPY ./Cargo.toml /build/Cargo.toml
RUN rustup update nightly && rustup default nightly && \
cargo clean && cargo build --release
FROM debian:buster-slim
WORKDIR /app
COPY --from=builder /build/target/release/fastcloud /app/fastcloud
RUN mkdir /app/service
COPY ./templates/ /app/templates
ENV FASTCLOUD_DIR /app/service
CMD [ "/app/fastcloud" ]

View File

@ -5,17 +5,16 @@ extern crate rocket;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
use std::collections::HashMap; use std::path::{PathBuf, Path};
use std::env; use std::env;
use std::fs; use std::fs;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::panic; use std::collections::HashMap;
use std::path::{PathBuf, Path};
use rocket::http::{Cookie, Cookies, ContentType, Status};
use rocket::response::{NamedFile, Redirect}; use rocket::response::{NamedFile, Redirect};
use rocket::http::{Cookie, Cookies, ContentType, Status};
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::{FilePart, MultipartDatas}; use rocket_upload::MultipartDatas;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
lazy_static! { lazy_static! {
@ -169,11 +168,8 @@ fn delete(path: PathBuf, _guard: AdminUser) -> Redirect {
fn directory_structure(path: String) -> HashMap<&'static str, HashMap<i32, DirPath>> { fn directory_structure(path: String) -> HashMap<&'static str, HashMap<i32, DirPath>> {
let dir = Path::new(path.as_str()); let dir = Path::new(path.as_str());
let mut sort = fs::read_dir(dir).unwrap() let paths = fs::read_dir(dir).unwrap()
.map(|x| x.unwrap().path().into_os_string().into_string().unwrap()) .map(|x| x.unwrap().path().into_os_string().into_string().unwrap())
.collect::<Vec<String>>();
sort.sort_by_key(|a| a.to_lowercase());
let paths = sort.into_iter()
.enumerate() .enumerate()
.map(|x| (x.0 as i32, DirPath { .map(|x| (x.0 as i32, DirPath {
is_file: Path::new(&x.1).is_file(), is_file: Path::new(&x.1).is_file(),
@ -210,42 +206,37 @@ 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()
} }
fn upload(dir: PathBuf, data:MultipartDatas) { #[post("/upload/<path..>", data = "<data>")]
let prev_hook = panic::take_hook(); fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas, _guard: AdminUser) -> Redirect {
panic::set_hook(Box::new(|_| {})); let dir = DIR.as_path().join(path.clone());
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);
} }
else {
for i in 0.. { for i in 0.. {
if !Path::new(&format!("{}/{}.{}", dir.to_str().unwrap(), f.filename, i)).exists() { if !Path::new(&format!("{}/{}.{}", dir.to_str().unwrap(), f.filename, i)).exists() {
let file = FilePart { f.persist(&dir);
name: f.name.to_string(),
path: f.path.to_string(),
filename: format!("{}.{}", f.filename, i)
};
file.persist(&dir);
break 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().to_path_buf(); let dir = DIR.as_path();
upload(dir, data); 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
}
}
}
Redirect::to(uri!(get_root)) Redirect::to(uri!(get_root))
} }

View File

@ -25,12 +25,12 @@
{{#each ctx}} {{#each ctx}}
<div class="d-flex flex-row"> <div class="d-flex flex-row">
{{#if is_file}} {{#if is_file}}
<a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/file/{{path}}" role="button" style="overflow: hidden;"> <a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/file/{{path}}" role="button">
<i class="bi bi-file-earmark"></i> <i class="bi bi-file-earmark"></i>
{{name}} {{name}}
</a> </a>
{{else}} {{else}}
<a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/serve/{{path}}" role="button" style="overflow: hidden;"> <a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/serve/{{path}}" role="button">
<i class="bi bi-folder-fill"></i> <i class="bi bi-folder-fill"></i>
{{name}} {{name}}
</a> </a>

View File

@ -19,12 +19,12 @@
{{#each ctx}} {{#each ctx}}
<div class="d-flex flex-row"> <div class="d-flex flex-row">
{{#if is_file}} {{#if is_file}}
<a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/file/{{path}}" role="button" style="overflow: hidden;"> <a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/file/{{path}}" role="button">
<i class="bi bi-file-earmark"></i> <i class="bi bi-file-earmark"></i>
{{name}} {{name}}
</a> </a>
{{else}} {{else}}
<a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/serve/{{path}}" role="button" style="overflow: hidden;"> <a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/serve/{{path}}" role="button">
<i class="bi bi-folder-fill"></i> <i class="bi bi-folder-fill"></i>
{{name}} {{name}}
</a> </a>