Skip to content

Commit

Permalink
Isolate pasta uploads from database.json by moving them into pasta_da…
Browse files Browse the repository at this point in the history
…ta/public/
  • Loading branch information
szabodanika committed Jul 31, 2022
1 parent 7b4cd7c commit 05941f0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/endpoints/create.rs
Expand Up @@ -122,11 +122,11 @@ pub async fn create(
}
};

std::fs::create_dir_all(format!("./pasta_data/{}", &new_pasta.id_as_animals()))
std::fs::create_dir_all(format!("./pasta_data/public/{}", &new_pasta.id_as_animals()))
.unwrap();

let filepath = format!(
"./pasta_data/{}/{}",
"./pasta_data/public/{}/{}",
&new_pasta.id_as_animals(),
&file.name()
);
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/remove.rs
Expand Up @@ -25,14 +25,14 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
if pasta.id == id {
// remove the file itself
if let Some(PastaFile { name, .. }) = &pasta.file {
if fs::remove_file(format!("./pasta_data/{}/{}", pasta.id_as_animals(), name))
if fs::remove_file(format!("./pasta_data/public/{}/{}", pasta.id_as_animals(), name))
.is_err()
{
log::error!("Failed to delete file {}!", name)
}

// and remove the containing directory
if fs::remove_dir(format!("./pasta_data/{}/", pasta.id_as_animals())).is_err() {
if fs::remove_dir(format!("./pasta_data/public/{}/", pasta.id_as_animals())).is_err() {
log::error!("Failed to delete directory {}!", name)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Expand Up @@ -63,11 +63,11 @@ async fn main() -> std::io::Result<()> {
ARGS.port.to_string()
);

match fs::create_dir_all("./pasta_data") {
match fs::create_dir_all("./pasta_data/public") {
Ok(dir) => dir,
Err(error) => {
log::error!("Couldn't create data directory ./pasta_data: {:?}", error);
panic!("Couldn't create data directory ./pasta_data: {:?}", error);
log::error!("Couldn't create data directory ./pasta_data/public/: {:?}", error);
panic!("Couldn't create data directory ./pasta_data/public/: {:?}", error);
}
};

Expand All @@ -87,7 +87,7 @@ async fn main() -> std::io::Result<()> {
.service(edit::get_edit)
.service(edit::post_edit)
.service(static_resources::static_resources)
.service(actix_files::Files::new("/file", "./pasta_data"))
.service(actix_files::Files::new("/file", "./pasta_data/public/"))
.service(web::resource("/upload").route(web::post().to(create::create)))
.default_service(web::route().to(errors::not_found))
.wrap(middleware::Logger::default())
Expand Down
4 changes: 2 additions & 2 deletions src/util/misc.rs
Expand Up @@ -24,7 +24,7 @@ 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/{}/{}",
"./pasta_data/public/{}/{}",
p.id_as_animals(),
file.name()
))
Expand All @@ -34,7 +34,7 @@ pub fn remove_expired(pastas: &mut Vec<Pasta>) {
}

// and remove the containing directory
if fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals())).is_err() {
if fs::remove_dir(format!("./pasta_data/public/{}/", p.id_as_animals())).is_err() {
log::error!("Failed to delete directory {}!", file.name())
}
}
Expand Down

0 comments on commit 05941f0

Please sign in to comment.