Merge pull request #31 from dvdsk/file-size

Adds file size to pasta with an attachment
This commit is contained in:
Dániel Szabó 2022-07-25 13:39:00 +01:00 committed by GitHub
commit 9053211904
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 23 deletions

View file

@ -1,10 +1,12 @@
use crate::dbio::save_to_file;
use crate::pasta::PastaFile;
use crate::util::animalnumbers::to_animal_names;
use crate::util::misc::is_valid_url;
use crate::{AppState, Pasta, ARGS};
use actix_multipart::Multipart;
use actix_web::{get, web, Error, HttpResponse, Responder};
use askama::Template;
use bytesize::ByteSize;
use futures::TryStreamExt;
use rand::Rng;
use std::io::Write;
@ -46,7 +48,7 @@ pub async fn create(
let mut new_pasta = Pasta {
id: rand::thread_rng().gen::<u16>() as u64,
content: String::from("No Text Content"),
file: String::from("no-file"),
file: None,
extension: String::from(""),
private: false,
editable: false,
@ -115,15 +117,18 @@ pub async fn create(
.unwrap();
let filepath = format!("./pasta_data/{}/{}", &new_pasta.id_as_animals(), &filename);
new_pasta.file = filename;
let mut f = web::block(|| std::fs::File::create(filepath)).await??;
let mut size = 0;
while let Some(chunk) = field.try_next().await? {
size += chunk.len();
f = web::block(move || f.write_all(&chunk).map(|_| f)).await??;
}
new_pasta.file = Some(PastaFile {
name: filename,
size: ByteSize::b(size as u64),
});
new_pasta.pasta_type = String::from("text");
}
_ => {}

View file

@ -2,15 +2,21 @@ use std::fmt;
use chrono::{Datelike, Timelike, Local, TimeZone};
use serde::{Deserialize, Serialize};
use bytesize::ByteSize;
use crate::util::animalnumbers::to_animal_names;
use crate::util::syntaxhighlighter::html_highlight;
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub struct PastaFile {
pub name: String, pub size: ByteSize ,
}
#[derive(Serialize, Deserialize)]
pub struct Pasta {
pub id: u64,
pub content: String,
pub file: String,
pub file: Option<PastaFile>,
pub extension: String,
pub private: bool,
pub editable: bool,

View file

@ -3,7 +3,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use linkify::{LinkFinder, LinkKind};
use std::fs;
use crate::{dbio, Pasta};
use crate::{dbio, pasta::PastaFile, Pasta};
pub fn remove_expired(pastas: &mut Vec<Pasta>) {
// get current time - this will be needed to check which pastas have expired
@ -22,20 +22,17 @@ pub fn remove_expired(pastas: &mut Vec<Pasta>) {
true
} else {
// remove the file itself
match fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file)) {
Ok(_) => {}
Err(_) => {
log::error!("Failed to delete file {}!", p.file)
if let Some(PastaFile { name, .. }) = &p.file {
if fs::remove_file(format!("./pasta_data/{}/{}", p.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/{}/", p.id_as_animals())).is_err() {
log::error!("Failed to delete directory {}!", name)
}
}
// and remove the containing directory
match fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals())) {
Ok(_) => {}
Err(_) => {
log::error!("Failed to delete directory {}!", p.file)
}
}
// remove
false
}
});