Added option to change data directory

Fixes #46
This commit is contained in:
Daniel Szabo 2023-07-08 22:26:26 +03:00
parent 664c4495e0
commit 668b4608ac
7 changed files with 32 additions and 15 deletions

View file

@ -5,7 +5,7 @@ services:
ports:
- "${MICROBIN_PORT}:8080"
volumes:
- ./microbin-data:/app/pasta_data
- ./microbin-data:/app/microbin_data
environment:
MICROBIN_BASIC_AUTH_USERNAME: ${MICROBIN_BASIC_AUTH_USERNAME}
MICROBIN_BASIC_AUTH_PASSWORD: ${MICROBIN_BASIC_AUTH_PASSWORD}
@ -21,6 +21,7 @@ services:
MICROBIN_BIND: ${MICROBIN_BIND}
MICROBIN_PRIVATE: ${MICROBIN_PRIVATE}
MICROBIN_PURE_HTML: ${MICROBIN_PURE_HTML}
MICROBIN_DATA_DIR: ${MICROBIN_DATA_DIR}
MICROBIN_JSON_DB: ${MICROBIN_JSON_DB}
MICROBIN_PUBLIC_PATH: ${MICROBIN_PUBLIC_PATH}
MICROBIN_SHORT_PATH: ${MICROBIN_SHORT_PATH}

View file

@ -102,6 +102,9 @@ pub struct Args {
#[clap(long, env = "MICROBIN_DEFAULT_EXPIRY", default_value = "24hour")]
pub default_expiry: String,
#[clap(long, env = "MICROBIN_DATA_DIR", default_value = "microbin_data")]
pub data_dir: String,
#[clap(short, long, env = "MICROBIN_NO_FILE_UPLOAD")]
pub no_file_upload: bool,

View file

@ -207,13 +207,15 @@ pub async fn create(
};
std::fs::create_dir_all(format!(
"./pasta_data/attachments/{}",
"./{}/attachments/{}",
ARGS.data_dir,
&new_pasta.id_as_animals()
))
.unwrap();
let filepath = format!(
"./pasta_data/attachments/{}/{}",
"./{}/attachments/{}/{}",
ARGS.data_dir,
&new_pasta.id_as_animals(),
&file.name()
);
@ -258,7 +260,8 @@ pub async fn create(
if new_pasta.file.is_some() && new_pasta.encrypt_server && !new_pasta.readonly {
let filepath = format!(
"./pasta_data/attachments/{}/{}",
"./{}/attachments/{}/{}",
ARGS.data_dir,
&new_pasta.id_as_animals(),
&new_pasta.file.as_ref().unwrap().name()
);

View file

@ -52,7 +52,8 @@ pub async fn post_secure_file(
if found {
if let Some(ref pasta_file) = pastas[index].file {
let file = File::open(format!(
"./pasta_data/attachments/{}/data.enc",
"./{}/attachments/{}/data.enc",
ARGS.data_dir,
pastas[index].id_as_animals()
))?;
@ -118,7 +119,8 @@ pub async fn get_file(
// Construct the path to the file
let file_path = format!(
"./pasta_data/attachments/{}/{}",
"./{}/attachments/{}/{}",
ARGS.data_dir,
pastas[index].id_as_animals(),
pasta_file.name()
);

View file

@ -32,7 +32,8 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
// remove the file itself
if let Some(PastaFile { name, .. }) = &pasta.file {
if fs::remove_file(format!(
"./pasta_data/attachments/{}/{}",
"./{}/attachments/{}/{}",
ARGS.data_dir,
pasta.id_as_animals(),
name
))
@ -43,7 +44,8 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
// and remove the containing directory
if fs::remove_dir(format!(
"./pasta_data/attachments/{}/",
"./{}/attachments/{}/",
ARGS.data_dir,
pasta.id_as_animals()
))
.is_err()

View file

@ -72,16 +72,17 @@ async fn main() -> std::io::Result<()> {
ARGS.port.to_string()
);
match fs::create_dir_all("./pasta_data/public") {
match fs::create_dir_all(format!("./{}/public", ARGS.data_dir)) {
Ok(dir) => dir,
Err(error) => {
log::error!(
"Couldn't create data directory ./pasta_data/attachments/: {:?}",
"Couldn't create data directory ./{}/attachments/: {:?}",
ARGS.data_dir,
error
);
panic!(
"Couldn't create data directory ./pasta_data/attachments/: {:?}",
error
"Couldn't create data directory ./{}/attachments/: {:?}",
ARGS.data_dir, error
);
}
};

View file

@ -41,7 +41,8 @@ pub fn remove_expired(pastas: &mut Vec<Pasta>) {
// remove the file itself
if let Some(file) = &p.file {
if fs::remove_file(format!(
"./pasta_data/attachments/{}/{}",
"./{}/attachments/{}/{}",
ARGS.data_dir,
p.id_as_animals(),
file.name()
))
@ -51,8 +52,12 @@ pub fn remove_expired(pastas: &mut Vec<Pasta>) {
}
// and remove the containing directory
if fs::remove_dir(format!("./pasta_data/attachments/{}/", p.id_as_animals()))
.is_err()
if fs::remove_dir(format!(
"./{}/attachments/{}/",
ARGS.data_dir,
p.id_as_animals()
))
.is_err()
{
log::error!("Failed to delete directory {}!", file.name())
}