2018-10-31 10:40:20 +01:00
|
|
|
use activitypub::object::Image;
|
2018-12-06 18:54:16 +01:00
|
|
|
use askama_escape::escape;
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-10-31 10:40:20 +01:00
|
|
|
use guid_create::GUID;
|
|
|
|
use reqwest;
|
|
|
|
use std::{fs, path::Path};
|
|
|
|
|
|
|
|
use plume_common::activity_pub::Id;
|
2018-09-02 22:55:42 +02:00
|
|
|
|
|
|
|
use instance::Instance;
|
2018-12-06 18:54:16 +01:00
|
|
|
use safe_string::SafeString;
|
2018-09-02 13:34:48 +02:00
|
|
|
use schema::medias;
|
2018-11-24 12:44:17 +01:00
|
|
|
use users::User;
|
2018-12-29 09:36:07 +01:00
|
|
|
use {ap_url, Connection, Error, Result};
|
2018-09-02 13:34:48 +02:00
|
|
|
|
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>,
|
2018-11-24 12:44:17 +01:00
|
|
|
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>,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub owner_id: i32,
|
2018-09-02 13:34:48 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum MediaCategory {
|
|
|
|
Image,
|
|
|
|
Audio,
|
|
|
|
Video,
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
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-12-29 09:36:07 +01:00
|
|
|
pub fn list_all_medias(conn: &Connection) -> Result<Vec<Media>> {
|
2018-11-24 12:44:17 +01:00
|
|
|
medias::table
|
|
|
|
.load::<Media>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn category(&self) -> MediaCategory {
|
|
|
|
match self
|
2018-11-24 12:44:17 +01:00
|
|
|
.file_path
|
|
|
|
.rsplitn(2, '.')
|
|
|
|
.next()
|
2018-12-06 18:54:16 +01:00
|
|
|
.expect("Media::category: extension error")
|
2018-11-24 12:44:17 +01:00
|
|
|
{
|
2018-12-06 18:54:16 +01:00
|
|
|
"png" | "jpg" | "jpeg" | "gif" | "svg" => MediaCategory::Image,
|
|
|
|
"mp3" | "wav" | "flac" => MediaCategory::Audio,
|
|
|
|
"mp4" | "avi" | "webm" | "mov" => MediaCategory::Video,
|
|
|
|
_ => MediaCategory::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn preview_html(&self, conn: &Connection) -> Result<SafeString> {
|
|
|
|
let url = self.url(conn)?;
|
|
|
|
Ok(match self.category() {
|
2018-12-06 18:54:16 +01:00
|
|
|
MediaCategory::Image => SafeString::new(&format!(
|
|
|
|
r#"<img src="{}" alt="{}" title="{}" class=\"preview\">"#,
|
|
|
|
url, escape(&self.alt_text), escape(&self.alt_text)
|
|
|
|
)),
|
|
|
|
MediaCategory::Audio => SafeString::new(&format!(
|
|
|
|
r#"<audio src="{}" title="{}" class="preview"></audio>"#,
|
|
|
|
url, escape(&self.alt_text)
|
|
|
|
)),
|
|
|
|
MediaCategory::Video => SafeString::new(&format!(
|
|
|
|
r#"<video src="{}" title="{}" class="preview"></video>"#,
|
|
|
|
url, escape(&self.alt_text)
|
|
|
|
)),
|
|
|
|
MediaCategory::Unknown => SafeString::new(""),
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn html(&self, conn: &Connection) -> Result<SafeString> {
|
|
|
|
let url = self.url(conn)?;
|
|
|
|
Ok(match self.category() {
|
2018-12-06 18:54:16 +01:00
|
|
|
MediaCategory::Image => SafeString::new(&format!(
|
|
|
|
r#"<img src="{}" alt="{}" title="{}">"#,
|
|
|
|
url, escape(&self.alt_text), escape(&self.alt_text)
|
|
|
|
)),
|
|
|
|
MediaCategory::Audio => SafeString::new(&format!(
|
|
|
|
r#"<audio src="{}" title="{}"></audio>"#,
|
|
|
|
url, escape(&self.alt_text)
|
|
|
|
)),
|
|
|
|
MediaCategory::Video => SafeString::new(&format!(
|
|
|
|
r#"<video src="{}" title="{}"></video>"#,
|
|
|
|
url, escape(&self.alt_text)
|
|
|
|
)),
|
|
|
|
MediaCategory::Unknown => SafeString::new(""),
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn markdown(&self, conn: &Connection) -> Result<SafeString> {
|
|
|
|
let url = self.url(conn)?;
|
|
|
|
Ok(match self.category() {
|
2018-12-06 18:54:16 +01:00
|
|
|
MediaCategory::Image => SafeString::new(&format!("![{}]({})", escape(&self.alt_text), url)),
|
2018-12-29 09:36:07 +01:00
|
|
|
MediaCategory::Audio | MediaCategory::Video => self.html(conn)?,
|
2018-12-06 18:54:16 +01:00
|
|
|
MediaCategory::Unknown => SafeString::new(""),
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-09-02 22:55:42 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn url(&self, conn: &Connection) -> Result<String> {
|
2018-09-03 13:17:59 +02:00
|
|
|
if self.is_remote {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(self.remote_url.clone().unwrap_or_default())
|
2018-09-03 13:17:59 +02:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(ap_url(&format!(
|
2018-11-24 12:44:17 +01:00
|
|
|
"{}/{}",
|
2018-12-29 09:36:07 +01:00
|
|
|
Instance::get_local(conn)?.public_domain,
|
2018-11-24 12:44:17 +01:00
|
|
|
self.file_path
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
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-12-29 09:36:07 +01:00
|
|
|
pub fn delete(&self, conn: &Connection) -> Result<()> {
|
2018-11-24 12:44:17 +01:00
|
|
|
if !self.is_remote {
|
2018-12-29 09:36:07 +01:00
|
|
|
fs::remove_file(self.file_path.as_str())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::from)
|
2018-09-02 23:10:15 +02:00
|
|
|
}
|
2018-09-03 13:17:59 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn save_remote(conn: &Connection, url: String, user: &User) -> Result<Media> {
|
2018-12-02 19:07:36 +01:00
|
|
|
if url.contains(&['<', '>', '"'][..]) {
|
2018-12-29 09:36:07 +01:00
|
|
|
Err(Error::Url)
|
2018-12-02 19:07:36 +01:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
Media::insert(
|
2018-12-02 19:07:36 +01:00
|
|
|
conn,
|
|
|
|
NewMedia {
|
|
|
|
file_path: String::new(),
|
|
|
|
alt_text: String::new(),
|
|
|
|
is_remote: true,
|
|
|
|
remote_url: Some(url),
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: user.id,
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)
|
2018-12-02 19:07:36 +01:00
|
|
|
}
|
2018-09-03 13:17:59 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn set_owner(&self, conn: &Connection, user: &User) -> Result<()> {
|
2018-09-03 13:17:59 +02:00
|
|
|
diesel::update(self)
|
2018-11-24 12:44:17 +01:00
|
|
|
.set(medias::owner_id.eq(user.id))
|
2018-09-03 13:17:59 +02:00
|
|
|
.execute(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::from)
|
2018-09-03 13:17:59 +02:00
|
|
|
}
|
2018-10-31 10:40:20 +01:00
|
|
|
|
|
|
|
// TODO: merge with save_remote?
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn from_activity(conn: &Connection, image: &Image) -> Result<Media> {
|
2018-10-31 10:40:20 +01:00
|
|
|
let remote_url = image.object_props.url_string().ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
let ext = remote_url
|
|
|
|
.rsplit('.')
|
|
|
|
.next()
|
|
|
|
.map(|ext| ext.to_owned())
|
2018-11-26 10:21:52 +01:00
|
|
|
.unwrap_or_else(|| String::from("png"));
|
2018-11-24 12:44:17 +01:00
|
|
|
let path =
|
|
|
|
Path::new("static")
|
|
|
|
.join("media")
|
|
|
|
.join(format!("{}.{}", GUID::rand().to_string(), ext));
|
2018-10-31 10:40:20 +01:00
|
|
|
|
|
|
|
let mut dest = fs::File::create(path.clone()).ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
reqwest::get(remote_url.as_str())
|
|
|
|
.ok()?
|
|
|
|
.copy_to(&mut dest)
|
|
|
|
.ok()?;
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Media::insert(
|
2018-11-24 12:44:17 +01:00
|
|
|
conn,
|
|
|
|
NewMedia {
|
|
|
|
file_path: path.to_str()?.to_string(),
|
|
|
|
alt_text: image.object_props.content_string().ok()?,
|
2018-12-02 19:07:36 +01:00
|
|
|
is_remote: false,
|
2018-11-24 12:44:17 +01:00
|
|
|
remote_url: None,
|
|
|
|
sensitive: image.object_props.summary_string().is_ok(),
|
|
|
|
content_warning: image.object_props.summary_string().ok(),
|
|
|
|
owner_id: User::from_url(
|
|
|
|
conn,
|
|
|
|
image
|
|
|
|
.object_props
|
|
|
|
.attributed_to_link_vec::<Id>()
|
|
|
|
.ok()?
|
|
|
|
.into_iter()
|
|
|
|
.next()?
|
2018-11-26 10:21:52 +01:00
|
|
|
.as_ref(),
|
2018-11-24 12:44:17 +01:00
|
|
|
)?.id,
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
|
|
|
use diesel::Connection;
|
|
|
|
use std::env::{current_dir, set_current_dir};
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
use tests::db;
|
|
|
|
use users::tests as usersTests;
|
|
|
|
use Connection as Conn;
|
|
|
|
|
2018-12-09 18:44:26 +01:00
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Media>) {
|
2018-11-24 12:44:17 +01:00
|
|
|
let mut wd = current_dir().unwrap().to_path_buf();
|
|
|
|
while wd.pop() {
|
|
|
|
if wd.join(".git").exists() {
|
|
|
|
set_current_dir(wd).unwrap();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let users = usersTests::fill_database(conn);
|
|
|
|
let user_one = users[0].id;
|
|
|
|
let user_two = users[1].id;
|
|
|
|
let f1 = "static/media/1.png".to_owned();
|
|
|
|
let f2 = "static/media/2.mp3".to_owned();
|
|
|
|
fs::write(f1.clone(), []).unwrap();
|
|
|
|
fs::write(f2.clone(), []).unwrap();
|
2018-12-09 18:44:26 +01:00
|
|
|
(users, vec![
|
2018-11-24 12:44:17 +01:00
|
|
|
NewMedia {
|
|
|
|
file_path: f1,
|
|
|
|
alt_text: "some alt".to_owned(),
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: user_one,
|
|
|
|
},
|
|
|
|
NewMedia {
|
|
|
|
file_path: f2,
|
|
|
|
alt_text: "alt message".to_owned(),
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: true,
|
|
|
|
content_warning: Some("Content warning".to_owned()),
|
|
|
|
owner_id: user_one,
|
|
|
|
},
|
|
|
|
NewMedia {
|
|
|
|
file_path: "".to_owned(),
|
|
|
|
alt_text: "another alt".to_owned(),
|
|
|
|
is_remote: true,
|
|
|
|
remote_url: Some("https://example.com/".to_owned()),
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: user_two,
|
|
|
|
},
|
|
|
|
].into_iter()
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|nm| Media::insert(conn, nm).unwrap())
|
2018-12-09 18:44:26 +01:00
|
|
|
.collect())
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn clean(conn: &Conn) {
|
|
|
|
//used to remove files generated by tests
|
2018-12-29 09:36:07 +01:00
|
|
|
for media in Media::list_all_medias(conn).unwrap() {
|
|
|
|
media.delete(conn).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//set_owner
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2018-12-09 18:44:26 +01:00
|
|
|
let user = fill_database(conn).0[0].id;
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
let path = "static/media/test_deletion".to_owned();
|
|
|
|
fs::write(path.clone(), []).unwrap();
|
|
|
|
|
|
|
|
let media = Media::insert(
|
|
|
|
conn,
|
|
|
|
NewMedia {
|
|
|
|
file_path: path.clone(),
|
|
|
|
alt_text: "alt message".to_owned(),
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: user,
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
assert!(Path::new(&path).exists());
|
2018-12-29 09:36:07 +01:00
|
|
|
media.delete(conn).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
assert!(!Path::new(&path).exists());
|
|
|
|
|
|
|
|
clean(conn);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn set_owner() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2018-12-09 18:44:26 +01:00
|
|
|
let (users, _) = fill_database(conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
let u1 = &users[0];
|
|
|
|
let u2 = &users[1];
|
|
|
|
|
|
|
|
let path = "static/media/test_set_owner".to_owned();
|
|
|
|
fs::write(path.clone(), []).unwrap();
|
|
|
|
|
|
|
|
let media = Media::insert(
|
|
|
|
conn,
|
|
|
|
NewMedia {
|
|
|
|
file_path: path.clone(),
|
|
|
|
alt_text: "alt message".to_owned(),
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: u1.id,
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
assert!(
|
2018-12-29 09:36:07 +01:00
|
|
|
Media::for_user(conn, u1.id).unwrap()
|
2018-11-24 12:44:17 +01:00
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id)
|
|
|
|
);
|
|
|
|
assert!(
|
2018-12-29 09:36:07 +01:00
|
|
|
!Media::for_user(conn, u2.id).unwrap()
|
2018-11-24 12:44:17 +01:00
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id)
|
|
|
|
);
|
2018-12-29 09:36:07 +01:00
|
|
|
media.set_owner(conn, u2).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
assert!(
|
2018-12-29 09:36:07 +01:00
|
|
|
!Media::for_user(conn, u1.id).unwrap()
|
2018-11-24 12:44:17 +01:00
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id)
|
|
|
|
);
|
|
|
|
assert!(
|
2018-12-29 09:36:07 +01:00
|
|
|
Media::for_user(conn, u2.id).unwrap()
|
2018-11-24 12:44:17 +01:00
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id)
|
|
|
|
);
|
|
|
|
|
|
|
|
clean(conn);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
2018-10-31 10:40:20 +01:00
|
|
|
}
|
2018-09-02 13:34:48 +02:00
|
|
|
}
|