Skip to content

Commit

Permalink
Added option to change data directory
Browse files Browse the repository at this point in the history
Fixes #46
  • Loading branch information
szabodanika committed Jul 8, 2023
1 parent 664c449 commit 668b460
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
9 changes: 6 additions & 3 deletions src/endpoints/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down Expand Up @@ -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()
);
Expand Down
6 changes: 4 additions & 2 deletions src/endpoints/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
))?;

Expand Down Expand Up @@ -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()
);
Expand Down
6 changes: 4 additions & 2 deletions src/endpoints/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
))
Expand All @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
};
Expand Down
11 changes: 8 additions & 3 deletions src/util/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
))
Expand All @@ -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())
}
Expand Down

0 comments on commit 668b460

Please sign in to comment.