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
|
|
|
|
2019-09-12 20:12:22 +00:00
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations, AsChangeset)]
|
2018-02-14 23:40:34 +00:00
|
|
|
#[table_name = "attachments"]
|
2020-04-15 14:49:33 +00:00
|
|
|
#[changeset_options(treat_none_as_null="true")]
|
2018-02-14 23:40:34 +00:00
|
|
|
#[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,
|
2019-05-20 19:12:41 +00:00
|
|
|
pub akey: Option<String>,
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Attachment {
|
2020-05-03 15:24:51 +00:00
|
|
|
pub const fn new(id: String, cipher_uuid: String, file_name: String, file_size: i32) -> Self {
|
2018-02-14 23:40:34 +00:00
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
cipher_uuid,
|
|
|
|
file_name,
|
|
|
|
file_size,
|
2019-05-20 19:12:41 +00:00
|
|
|
akey: None,
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_file_path(&self) -> String {
|
2019-01-25 17:23:51 +00:00
|
|
|
format!("{}/{}/{}", CONFIG.attachments_folder(), self.cipher_uuid, self.id)
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2019-05-20 19:12:41 +00:00
|
|
|
"Key": self.akey,
|
2018-02-14 23:40:34 +00:00
|
|
|
"Object": "attachment"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:56:26 +00:00
|
|
|
use crate::db::schema::{attachments, ciphers};
|
2018-12-30 22:34:31 +00:00
|
|
|
use crate::db::DbConn;
|
2018-02-14 23:40:34 +00:00
|
|
|
use diesel::prelude::*;
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
/// Database methods
|
|
|
|
impl Attachment {
|
2019-09-12 20:12:22 +00:00
|
|
|
#[cfg(feature = "postgresql")]
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn save(&self, conn: &DbConn) -> EmptyResult {
|
2019-09-17 19:05:56 +00:00
|
|
|
diesel::insert_into(attachments::table)
|
2019-09-12 20:12:22 +00:00
|
|
|
.values(self)
|
|
|
|
.on_conflict(attachments::id)
|
|
|
|
.do_update()
|
|
|
|
.set(self)
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error saving attachment")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "postgresql"))]
|
|
|
|
pub fn save(&self, conn: &DbConn) -> EmptyResult {
|
2019-09-17 19:05:56 +00:00
|
|
|
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)
|
2018-12-19 20:52:53 +00:00
|
|
|
.map_res("Error saving attachment")
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
2018-12-12 21:15:54 +00:00
|
|
|
crate::util::retry(
|
2018-12-30 22:34:31 +00:00
|
|
|
|| diesel::delete(attachments::table.filter(attachments::id.eq(&self.id))).execute(&**conn),
|
2018-12-12 21:15:54 +00:00
|
|
|
10,
|
2018-12-19 20:52:53 +00:00
|
|
|
)
|
|
|
|
.map_res("Error deleting attachment")?;
|
2018-12-30 22:34:31 +00:00
|
|
|
|
2018-12-23 21:37:02 +00:00
|
|
|
crate::util::delete_file(&self.get_file_path())?;
|
2018-07-31 14:07:17 +00:00
|
|
|
Ok(())
|
2018-05-15 16:27:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> EmptyResult {
|
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> {
|
2019-01-11 00:35:15 +00:00
|
|
|
let id = id.to_lowercase();
|
2019-01-25 17:23:51 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
attachments::table
|
|
|
|
.filter(attachments::id.eq(id))
|
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_cipher(cipher_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::cipher_uuid.eq(cipher_uuid))
|
2018-12-19 20:52:53 +00:00
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading attachments")
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
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))
|
2018-12-19 20:52:53 +00:00
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading attachments")
|
2018-08-13 11:35:41 +00:00
|
|
|
}
|
2020-02-17 21:56:26 +00:00
|
|
|
|
|
|
|
pub fn size_by_user(user_uuid: &str, conn: &DbConn) -> i64 {
|
|
|
|
let result: Option<i64> = attachments::table
|
|
|
|
.left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid)))
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid))
|
|
|
|
.select(diesel::dsl::sum(attachments::file_size))
|
|
|
|
.first(&**conn)
|
|
|
|
.expect("Error loading user attachment total size");
|
|
|
|
|
|
|
|
result.unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:57:03 +00:00
|
|
|
pub fn count_by_user(user_uuid: &str, conn: &DbConn) -> i64 {
|
|
|
|
attachments::table
|
|
|
|
.left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid)))
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid))
|
|
|
|
.count()
|
|
|
|
.first::<i64>(&**conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:56:26 +00:00
|
|
|
pub fn size_by_org(org_uuid: &str, conn: &DbConn) -> i64 {
|
|
|
|
let result: Option<i64> = attachments::table
|
|
|
|
.left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid)))
|
|
|
|
.filter(ciphers::organization_uuid.eq(org_uuid))
|
|
|
|
.select(diesel::dsl::sum(attachments::file_size))
|
|
|
|
.first(&**conn)
|
|
|
|
.expect("Error loading user attachment total size");
|
|
|
|
|
|
|
|
result.unwrap_or(0)
|
|
|
|
}
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|