fixes #6 adding the size of the attached file

This commit is contained in:
dvdsk 2022-07-13 23:50:10 +02:00
parent 0687f44137
commit 39233e9447
No known key found for this signature in database
GPG key ID: 6CF9D20C5709A836
6 changed files with 32 additions and 23 deletions

View file

@ -18,6 +18,7 @@ actix-web="4"
actix-files="0.6.0"
serde={ version = "1.0", features = ["derive"] }
serde_json = "1.0.80"
bytesize = { version = "1.1", features = ["serde"] }
askama="0.10"
askama-filters={ version = "0.1.3", features = ["chrono"] }
chrono="0.4.19"

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::{DateTime, Datelike, NaiveDateTime, Timelike, Utc};
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
}
});

View file

@ -1,8 +1,8 @@
{% include "header.html" %}
<a style="margin-right: 0.5rem" href="/raw/{{pasta.id_as_animals()}}">Raw Text Content</a>
{% if pasta.file != "no-file" %}
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file}}">Attached file
'{{pasta.file}}'</a>
{% if pasta.file.is_some() %}
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name}}">Attached file
'{{pasta.file.as_ref().unwrap().name}}' [{{pasta.file.as_ref().unwrap().size}}]</a>
{%- endif %}
{% if pasta.editable %}
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>

View file

@ -48,8 +48,8 @@
</td>
<td>
<a style="margin-right:1rem" href="/raw/{{pasta.id_as_animals()}}">Raw</a>
{% if pasta.file != "no-file" %}
<a style="margin-right:1rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file}}">File</a>
{% if pasta.file.is_some() %}
<a style="margin-right:1rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name}}">File</a>
{%- endif %}
{% if pasta.editable %}
<a style="margin-right:1rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>