2018-09-26 17:22:42 +02:00
|
|
|
use diesel::{self, QueryDsl, ExpressionMethods, RunQueryDsl};
|
2018-09-02 22:55:42 +02:00
|
|
|
use serde_json;
|
2018-09-02 23:10:15 +02:00
|
|
|
use std::fs;
|
2018-09-02 22:55:42 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
use {ap_url, Connection};
|
2018-09-02 22:55:42 +02:00
|
|
|
use instance::Instance;
|
2018-09-02 13:34:48 +02:00
|
|
|
use schema::medias;
|
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
#[derive(Clone, Identifiable, Queryable, Serialize)]
|
2018-09-02 13:34:48 +02:00
|
|
|
pub struct Media {
|
|
|
|
pub id: i32,
|
|
|
|
pub file_path: String,
|
|
|
|
pub alt_text: String,
|
|
|
|
pub is_remote: bool,
|
|
|
|
pub remote_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
2018-09-02 22:55:42 +02:00
|
|
|
pub content_warning: Option<String>,
|
|
|
|
pub owner_id: i32
|
2018-09-02 13:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "medias"]
|
|
|
|
pub struct NewMedia {
|
|
|
|
pub file_path: String,
|
|
|
|
pub alt_text: String,
|
|
|
|
pub is_remote: bool,
|
|
|
|
pub remote_url: Option<String>,
|
|
|
|
pub sensitive: bool,
|
2018-09-02 22:55:42 +02:00
|
|
|
pub content_warning: Option<String>,
|
|
|
|
pub owner_id: i32
|
2018-09-02 13:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Media {
|
|
|
|
insert!(medias, NewMedia);
|
|
|
|
get!(medias);
|
2018-09-02 22:55:42 +02:00
|
|
|
list_by!(medias, for_user, owner_id as i32);
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn to_json(&self, conn: &Connection) -> serde_json::Value {
|
2018-10-20 08:44:33 +02:00
|
|
|
let mut json = serde_json::to_value(self).expect("Media::to_json: serialization error");
|
2018-09-03 12:16:07 +02:00
|
|
|
let url = self.url(conn);
|
2018-10-20 08:44:33 +02:00
|
|
|
let (preview, html, md) = match self.file_path.rsplitn(2, '.').next().expect("Media::to_json: extension error") {
|
2018-09-03 11:22:14 +02:00
|
|
|
"png" | "jpg" | "jpeg" | "gif" | "svg" => (
|
2018-09-03 12:16:07 +02:00
|
|
|
format!("<img src=\"{}\" alt=\"{}\" title=\"{}\" class=\"preview\">", url, self.alt_text, self.alt_text),
|
|
|
|
format!("<img src=\"{}\" alt=\"{}\" title=\"{}\">", url, self.alt_text, self.alt_text),
|
|
|
|
format!("![{}]({})", self.alt_text, url),
|
2018-09-02 22:55:42 +02:00
|
|
|
),
|
|
|
|
"mp3" | "wav" | "flac" => (
|
2018-09-03 12:16:07 +02:00
|
|
|
format!("<audio src=\"{}\" title=\"{}\" class=\"preview\"></audio>", url, self.alt_text),
|
|
|
|
format!("<audio src=\"{}\" title=\"{}\"></audio>", url, self.alt_text),
|
|
|
|
format!("<audio src=\"{}\" title=\"{}\"></audio>", url, self.alt_text),
|
2018-09-02 22:55:42 +02:00
|
|
|
),
|
|
|
|
"mp4" | "avi" | "webm" | "mov" => (
|
2018-09-03 12:16:07 +02:00
|
|
|
format!("<video src=\"{}\" title=\"{}\" class=\"preview\"></video>", url, self.alt_text),
|
|
|
|
format!("<video src=\"{}\" title=\"{}\"></video>", url, self.alt_text),
|
|
|
|
format!("<video src=\"{}\" title=\"{}\"></video>", url, self.alt_text),
|
2018-09-02 22:55:42 +02:00
|
|
|
),
|
2018-09-03 12:16:07 +02:00
|
|
|
_ => (String::new(), String::new(), String::new())
|
2018-09-02 22:55:42 +02:00
|
|
|
};
|
|
|
|
json["html_preview"] = json!(preview);
|
|
|
|
json["html"] = json!(html);
|
2018-09-03 12:16:07 +02:00
|
|
|
json["url"] = json!(url);
|
|
|
|
json["md"] = json!(md);
|
2018-09-02 22:55:42 +02:00
|
|
|
json
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn url(&self, conn: &Connection) -> String {
|
2018-09-03 13:17:59 +02:00
|
|
|
if self.is_remote {
|
|
|
|
self.remote_url.clone().unwrap_or(String::new())
|
|
|
|
} else {
|
2018-10-27 19:21:50 +02:00
|
|
|
ap_url(format!("{}/{}", Instance::get_local(conn).expect("Media::url: local instance not found error").public_domain, self.file_path))
|
2018-09-03 13:17:59 +02:00
|
|
|
}
|
2018-09-02 22:55:42 +02:00
|
|
|
}
|
2018-09-02 23:10:15 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn delete(&self, conn: &Connection) {
|
2018-10-20 08:44:33 +02:00
|
|
|
fs::remove_file(self.file_path.as_str()).expect("Media::delete: file deletion error");
|
|
|
|
diesel::delete(self).execute(conn).expect("Media::delete: database entry deletion error");
|
2018-09-02 23:10:15 +02:00
|
|
|
}
|
2018-09-03 13:17:59 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn save_remote(conn: &Connection, url: String) -> Media {
|
2018-09-03 13:17:59 +02:00
|
|
|
Media::insert(conn, NewMedia {
|
|
|
|
file_path: String::new(),
|
|
|
|
alt_text: String::new(),
|
|
|
|
is_remote: true,
|
|
|
|
remote_url: Some(url),
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: 1 // It will be owned by the admin during an instant, but set_owner will be called just after
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn set_owner(&self, conn: &Connection, id: i32) {
|
2018-09-03 13:17:59 +02:00
|
|
|
diesel::update(self)
|
|
|
|
.set(medias::owner_id.eq(id))
|
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Media::set_owner: owner update error");
|
2018-09-03 13:17:59 +02:00
|
|
|
}
|
2018-09-02 13:34:48 +02:00
|
|
|
}
|