2021-05-26 07:13:04 +02:00
|
|
|
use std::io::ErrorKind;
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
|
|
|
use super::Cipher;
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::CONFIG;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_object! {
|
2021-03-13 22:04:04 +01:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, Associations, AsChangeset)]
|
2020-08-18 17:15:44 +02:00
|
|
|
#[table_name = "attachments"]
|
|
|
|
#[changeset_options(treat_none_as_null="true")]
|
|
|
|
#[belongs_to(super::Cipher, foreign_key = "cipher_uuid")]
|
|
|
|
#[primary_key(id)]
|
|
|
|
pub struct Attachment {
|
|
|
|
pub id: String,
|
|
|
|
pub cipher_uuid: String,
|
2021-05-25 12:48:57 +02:00
|
|
|
pub file_name: String, // encrypted
|
2020-08-18 17:15:44 +02:00
|
|
|
pub file_size: i32,
|
|
|
|
pub akey: Option<String>,
|
|
|
|
}
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Attachment {
|
2021-05-25 12:48:57 +02:00
|
|
|
pub const fn new(id: String, cipher_uuid: String, file_name: String, file_size: i32, akey: Option<String>) -> Self {
|
2018-02-15 00:40:34 +01:00
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
cipher_uuid,
|
|
|
|
file_name,
|
|
|
|
file_size,
|
2021-05-25 12:48:57 +02:00
|
|
|
akey,
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_file_path(&self) -> String {
|
2019-01-25 18:23:51 +01:00
|
|
|
format!("{}/{}/{}", CONFIG.attachments_folder(), self.cipher_uuid, self.id)
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:48:57 +02:00
|
|
|
pub fn get_url(&self, host: &str) -> String {
|
|
|
|
format!("{}/attachments/{}/{}", host, self.cipher_uuid, self.id)
|
|
|
|
}
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2021-05-25 12:48:57 +02:00
|
|
|
pub fn to_json(&self, host: &str) -> Value {
|
2018-02-15 00:40:34 +01:00
|
|
|
json!({
|
|
|
|
"Id": self.id,
|
2021-05-25 12:48:57 +02:00
|
|
|
"Url": self.get_url(host),
|
2018-02-15 00:40:34 +01:00
|
|
|
"FileName": self.file_name,
|
|
|
|
"Size": self.file_size.to_string(),
|
2021-05-25 12:48:57 +02:00
|
|
|
"SizeName": crate::util::get_display_size(self.file_size),
|
2019-05-20 21:12:41 +02:00
|
|
|
"Key": self.akey,
|
2018-02-15 00:40:34 +01:00
|
|
|
"Object": "attachment"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
use crate::db::DbConn;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
/// Database methods
|
|
|
|
impl Attachment {
|
2019-09-12 22:12:22 +02:00
|
|
|
pub fn save(&self, conn: &DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn:
|
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
match diesel::replace_into(attachments::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values(AttachmentDb::to_db(self))
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
|
|
|
|
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
|
|
|
|
diesel::update(attachments::table)
|
|
|
|
.filter(attachments::id.eq(&self.id))
|
|
|
|
.set(AttachmentDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving attachment")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving attachment")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = AttachmentDb::to_db(self);
|
|
|
|
diesel::insert_into(attachments::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(attachments::id)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving attachment")
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:48:57 +02:00
|
|
|
pub fn delete(&self, conn: &DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
crate::util::retry(
|
|
|
|
|| diesel::delete(attachments::table.filter(attachments::id.eq(&self.id))).execute(conn),
|
|
|
|
10,
|
|
|
|
)
|
|
|
|
.map_res("Error deleting attachment")?;
|
|
|
|
|
2021-05-26 07:13:04 +02:00
|
|
|
let file_path = &self.get_file_path();
|
|
|
|
|
|
|
|
match crate::util::delete_file(file_path) {
|
|
|
|
// Ignore "file not found" errors. This can happen when the
|
|
|
|
// upstream caller has already cleaned up the file as part of
|
|
|
|
// its own error handling.
|
|
|
|
Err(e) if e.kind() == ErrorKind::NotFound => {
|
|
|
|
debug!("File '{}' already deleted.", file_path);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
2020-08-18 17:15:44 +02:00
|
|
|
}}
|
2018-05-15 18:27:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2021-06-19 22:02:03 +02:00
|
|
|
for attachment in Attachment::find_by_cipher(cipher_uuid, conn) {
|
|
|
|
attachment.delete(conn)?;
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
2018-05-15 18:27:53 +02:00
|
|
|
Ok(())
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_id(id: &str, conn: &DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::id.eq(id.to_lowercase()))
|
|
|
|
.first::<AttachmentDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_cipher(cipher_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::cipher_uuid.eq(cipher_uuid))
|
|
|
|
.load::<AttachmentDb>(conn)
|
|
|
|
.expect("Error loading attachments")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
2018-08-13 13:35:41 +02:00
|
|
|
|
|
|
|
pub fn find_by_ciphers(cipher_uuids: Vec<String>, conn: &DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
attachments::table
|
|
|
|
.filter(attachments::cipher_uuid.eq_any(cipher_uuids))
|
|
|
|
.load::<AttachmentDb>(conn)
|
|
|
|
.expect("Error loading attachments")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-08-13 13:35:41 +02:00
|
|
|
}
|
2020-02-17 22:56:26 +01:00
|
|
|
|
|
|
|
pub fn size_by_user(user_uuid: &str, conn: &DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
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-02-17 22:56:26 +01:00
|
|
|
}
|
|
|
|
|
2020-06-03 17:57:03 +02:00
|
|
|
pub fn count_by_user(user_uuid: &str, conn: &DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
attachments::table
|
|
|
|
.left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid)))
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid))
|
|
|
|
.count()
|
|
|
|
.first(conn)
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
2020-06-03 17:57:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 22:56:26 +01:00
|
|
|
pub fn size_by_org(org_uuid: &str, conn: &DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
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)
|
|
|
|
}}
|
2020-02-17 22:56:26 +01:00
|
|
|
}
|
2020-06-03 20:37:31 +02:00
|
|
|
|
|
|
|
pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
attachments::table
|
|
|
|
.left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid)))
|
|
|
|
.filter(ciphers::organization_uuid.eq(org_uuid))
|
|
|
|
.count()
|
|
|
|
.first(conn)
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
2020-06-03 20:37:31 +02:00
|
|
|
}
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|