fixes #6 adding the size of the attached file
This commit is contained in:
parent
0687f44137
commit
39233e9447
@ -18,6 +18,7 @@ actix-web="4"
|
|||||||
actix-files="0.6.0"
|
actix-files="0.6.0"
|
||||||
serde={ version = "1.0", features = ["derive"] }
|
serde={ version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0.80"
|
serde_json = "1.0.80"
|
||||||
|
bytesize = { version = "1.1", features = ["serde"] }
|
||||||
askama="0.10"
|
askama="0.10"
|
||||||
askama-filters={ version = "0.1.3", features = ["chrono"] }
|
askama-filters={ version = "0.1.3", features = ["chrono"] }
|
||||||
chrono="0.4.19"
|
chrono="0.4.19"
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
use crate::dbio::save_to_file;
|
use crate::dbio::save_to_file;
|
||||||
|
use crate::pasta::PastaFile;
|
||||||
use crate::util::animalnumbers::to_animal_names;
|
use crate::util::animalnumbers::to_animal_names;
|
||||||
use crate::util::misc::is_valid_url;
|
use crate::util::misc::is_valid_url;
|
||||||
use crate::{AppState, Pasta, ARGS};
|
use crate::{AppState, Pasta, ARGS};
|
||||||
use actix_multipart::Multipart;
|
use actix_multipart::Multipart;
|
||||||
use actix_web::{get, web, Error, HttpResponse, Responder};
|
use actix_web::{get, web, Error, HttpResponse, Responder};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
use bytesize::ByteSize;
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -46,7 +48,7 @@ pub async fn create(
|
|||||||
let mut new_pasta = Pasta {
|
let mut new_pasta = Pasta {
|
||||||
id: rand::thread_rng().gen::<u16>() as u64,
|
id: rand::thread_rng().gen::<u16>() as u64,
|
||||||
content: String::from("No Text Content"),
|
content: String::from("No Text Content"),
|
||||||
file: String::from("no-file"),
|
file: None,
|
||||||
extension: String::from(""),
|
extension: String::from(""),
|
||||||
private: false,
|
private: false,
|
||||||
editable: false,
|
editable: false,
|
||||||
@ -115,15 +117,18 @@ pub async fn create(
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let filepath = format!("./pasta_data/{}/{}", &new_pasta.id_as_animals(), &filename);
|
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 f = web::block(|| std::fs::File::create(filepath)).await??;
|
||||||
|
|
||||||
|
let mut size = 0;
|
||||||
while let Some(chunk) = field.try_next().await? {
|
while let Some(chunk) = field.try_next().await? {
|
||||||
|
size += chunk.len();
|
||||||
f = web::block(move || f.write_all(&chunk).map(|_| f)).await??;
|
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");
|
new_pasta.pasta_type = String::from("text");
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -2,15 +2,21 @@ use std::fmt;
|
|||||||
|
|
||||||
use chrono::{DateTime, Datelike, NaiveDateTime, Timelike, Utc};
|
use chrono::{DateTime, Datelike, NaiveDateTime, Timelike, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use bytesize::ByteSize;
|
||||||
|
|
||||||
use crate::util::animalnumbers::to_animal_names;
|
use crate::util::animalnumbers::to_animal_names;
|
||||||
use crate::util::syntaxhighlighter::html_highlight;
|
use crate::util::syntaxhighlighter::html_highlight;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct PastaFile {
|
||||||
|
pub name: String, pub size: ByteSize ,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Pasta {
|
pub struct Pasta {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub file: String,
|
pub file: Option<PastaFile>,
|
||||||
pub extension: String,
|
pub extension: String,
|
||||||
pub private: bool,
|
pub private: bool,
|
||||||
pub editable: bool,
|
pub editable: bool,
|
||||||
|
@ -3,7 +3,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||||||
use linkify::{LinkFinder, LinkKind};
|
use linkify::{LinkFinder, LinkKind};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use crate::{dbio, Pasta};
|
use crate::{dbio, pasta::PastaFile, Pasta};
|
||||||
|
|
||||||
pub fn remove_expired(pastas: &mut Vec<Pasta>) {
|
pub fn remove_expired(pastas: &mut Vec<Pasta>) {
|
||||||
// get current time - this will be needed to check which pastas have expired
|
// 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
|
true
|
||||||
} else {
|
} else {
|
||||||
// remove the file itself
|
// remove the file itself
|
||||||
match fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file)) {
|
if let Some(PastaFile { name, .. }) = &p.file {
|
||||||
Ok(_) => {}
|
if fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), name)).is_err()
|
||||||
Err(_) => {
|
{
|
||||||
log::error!("Failed to delete file {}!", p.file)
|
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
|
false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{% include "header.html" %}
|
{% include "header.html" %}
|
||||||
<a style="margin-right: 0.5rem" href="/raw/{{pasta.id_as_animals()}}">Raw Text Content</a>
|
<a style="margin-right: 0.5rem" href="/raw/{{pasta.id_as_animals()}}">Raw Text Content</a>
|
||||||
{% if pasta.file != "no-file" %}
|
{% if pasta.file.is_some() %}
|
||||||
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file}}">Attached file
|
<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}}'</a>
|
'{{pasta.file.as_ref().unwrap().name}}' [{{pasta.file.as_ref().unwrap().size}}]</a>
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{% if pasta.editable %}
|
{% if pasta.editable %}
|
||||||
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
|
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
|
||||||
|
@ -48,8 +48,8 @@
|
|||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a style="margin-right:1rem" href="/raw/{{pasta.id_as_animals()}}">Raw</a>
|
<a style="margin-right:1rem" href="/raw/{{pasta.id_as_animals()}}">Raw</a>
|
||||||
{% if pasta.file != "no-file" %}
|
{% if pasta.file.is_some() %}
|
||||||
<a style="margin-right:1rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file}}">File</a>
|
<a style="margin-right:1rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name}}">File</a>
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{% if pasta.editable %}
|
{% if pasta.editable %}
|
||||||
<a style="margin-right:1rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
|
<a style="margin-right:1rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user