2018-10-10 18:40:39 +00:00
|
|
|
use serde_json::Value;
|
2018-02-14 23:40:34 +00:00
|
|
|
|
|
|
|
use super::Cipher;
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::CONFIG;
|
2018-02-14 23:40:34 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
|
|
|
#[table_name = "attachments"]
|
|
|
|
#[belongs_to(Cipher, foreign_key = "cipher_uuid")]
|
|
|
|
#[primary_key(id)]
|
|
|
|
pub struct Attachment {
|
|
|
|
pub id: String,
|
|
|
|
pub cipher_uuid: String,
|
|
|
|
pub file_name: String,
|
|
|
|
pub file_size: i32,
|
2018-11-27 16:24:12 +00:00
|
|
|
pub key: Option<String>
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Attachment {
|
|
|
|
pub fn new(id: String, cipher_uuid: String, file_name: String, file_size: i32) -> Self {
|
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
cipher_uuid,
|
|
|
|
file_name,
|
|
|
|
file_size,
|
2018-11-27 16:24:12 +00:00
|
|
|
key: None
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_file_path(&self) -> String {
|
|
|
|
format!("{}/{}/{}", CONFIG.attachments_folder, self.cipher_uuid, self.id)
|
|
|
|
}
|
|
|
|
|
2018-10-10 18:40:39 +00:00
|
|
|
pub fn to_json(&self, host: &str) -> Value {
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::util::get_display_size;
|
2018-02-14 23:40:34 +00:00
|
|
|
|
2018-02-15 00:49:36 +00:00
|
|
|
let web_path = format!("{}/attachments/{}/{}", host, self.cipher_uuid, self.id);
|
2018-02-14 23:40:34 +00:00
|
|
|
let display_size = get_display_size(self.file_size);
|
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.id,
|
|
|
|
"Url": web_path,
|
|
|
|
"FileName": self.file_name,
|
|
|
|
"Size": self.file_size.to_string(),
|
|
|
|
"SizeName": display_size,
|
2018-11-27 16:24:12 +00:00
|
|
|
"Key": self.key,
|
2018-02-14 23:40:34 +00:00
|
|
|
"Object": "attachment"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::db::DbConn;
|
|
|
|
use crate::db::schema::attachments;
|
2018-02-14 23:40:34 +00:00
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Attachment {
|
2018-08-29 13:22:19 +00:00
|
|
|
pub fn save(&self, conn: &DbConn) -> QueryResult<()> {
|
|
|
|
diesel::replace_into(attachments::table)
|
2018-02-14 23:40:34 +00:00
|
|
|
.values(self)
|
2018-08-29 13:22:19 +00:00
|
|
|
.execute(&**conn)
|
|
|
|
.and(Ok(()))
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 16:27:53 +00:00
|
|
|
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::util;
|
2018-07-31 14:07:17 +00:00
|
|
|
use std::{thread, time};
|
2018-02-15 18:05:57 +00:00
|
|
|
|
2018-07-31 14:07:17 +00:00
|
|
|
let mut retries = 10;
|
2018-08-29 13:22:19 +00:00
|
|
|
|
2018-07-31 14:07:17 +00:00
|
|
|
loop {
|
|
|
|
match diesel::delete(
|
|
|
|
attachments::table.filter(
|
|
|
|
attachments::id.eq(&self.id)
|
|
|
|
)
|
|
|
|
).execute(&**conn) {
|
|
|
|
Ok(_) => break,
|
|
|
|
Err(err) => {
|
|
|
|
if retries < 1 {
|
2018-12-06 19:35:25 +00:00
|
|
|
error!("Failed with 10 retries");
|
2018-07-31 14:07:17 +00:00
|
|
|
return Err(err)
|
|
|
|
} else {
|
2018-09-13 19:55:23 +00:00
|
|
|
retries -= 1;
|
2018-12-06 19:35:25 +00:00
|
|
|
info!("Had to retry! Retries left: {}", retries);
|
2018-07-31 14:07:17 +00:00
|
|
|
thread::sleep(time::Duration::from_millis(500));
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 18:05:57 +00:00
|
|
|
|
2018-07-31 14:07:17 +00:00
|
|
|
util::delete_file(&self.get_file_path());
|
|
|
|
Ok(())
|
2018-05-15 16:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> QueryResult<()> {
|
2018-11-27 16:24:12 +00:00
|
|
|
for attachment in Attachment::find_by_cipher(&cipher_uuid, &conn) {
|
|
|
|
attachment.delete(&conn)?;
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
2018-05-15 16:27:53 +00:00
|
|
|
Ok(())
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_id(id: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::id.eq(id))
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_cipher(cipher_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::cipher_uuid.eq(cipher_uuid))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading attachments")
|
|
|
|
}
|
2018-08-13 11:35:41 +00:00
|
|
|
|
|
|
|
pub fn find_by_ciphers(cipher_uuids: Vec<String>, conn: &DbConn) -> Vec<Self> {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::cipher_uuid.eq_any(cipher_uuids))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading attachments")
|
|
|
|
}
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|