2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
2021-01-30 11:24:16 +01:00
|
|
|
ap_url, db_conn::DbConn, instance::Instance, safe_string::SafeString, schema::medias,
|
|
|
|
users::User, Connection, Error, Result, CONFIG,
|
2020-01-21 07:02:03 +01:00
|
|
|
};
|
2018-10-31 10:40:20 +01:00
|
|
|
use activitypub::object::Image;
|
2022-02-26 02:58:49 +01:00
|
|
|
use activitystreams::{object::Image as Image07, prelude::*};
|
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;
|
2019-04-17 19:31:47 +02:00
|
|
|
use plume_common::{
|
2022-05-02 09:07:08 +02:00
|
|
|
activity_pub::{inbox::FromId, request, Id, ToAsString, ToAsUri},
|
2022-01-06 21:38:17 +01:00
|
|
|
utils::{escape, MediaProcessor},
|
2019-04-17 19:31:47 +02:00
|
|
|
};
|
2021-02-17 17:23:57 +01:00
|
|
|
use std::{
|
2021-02-18 03:08:40 +01:00
|
|
|
fs::{self, DirBuilder},
|
2021-02-25 03:01:47 +01:00
|
|
|
path::{self, Path, PathBuf},
|
2021-02-17 17:23:57 +01:00
|
|
|
};
|
|
|
|
use tracing::warn;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
const REMOTE_MEDIA_DIRECTORY: &str = "remote";
|
2018-09-02 13:34:48 +02:00
|
|
|
|
2021-02-23 17:05:19 +01:00
|
|
|
#[derive(Clone, Identifiable, Queryable, AsChangeset)]
|
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,
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:11:36 +01:00
|
|
|
impl MediaCategory {
|
|
|
|
pub fn to_string(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
MediaCategory::Image => "image",
|
|
|
|
MediaCategory::Audio => "audio",
|
|
|
|
MediaCategory::Video => "video",
|
|
|
|
MediaCategory::Unknown => "unknown",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-02 13:34:48 +02:00
|
|
|
impl Media {
|
|
|
|
insert!(medias, NewMedia);
|
|
|
|
get!(medias);
|
2021-02-23 17:04:31 +01:00
|
|
|
find_by!(medias, find_by_file_path, file_path as &str);
|
2019-08-28 11:37:03 +02:00
|
|
|
|
|
|
|
pub fn for_user(conn: &Connection, owner: i32) -> Result<Vec<Media>> {
|
|
|
|
medias::table
|
|
|
|
.filter(medias::owner_id.eq(owner))
|
|
|
|
.order(medias::id.desc())
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
2018-09-02 22:55:42 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn list_all_medias(conn: &Connection) -> Result<Vec<Media>> {
|
2019-03-20 17:56:17 +01:00
|
|
|
medias::table.load::<Media>(conn).map_err(Error::from)
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn page_for_user(
|
|
|
|
conn: &Connection,
|
|
|
|
user: &User,
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
) -> Result<Vec<Media>> {
|
2019-03-06 14:11:36 +01:00
|
|
|
medias::table
|
|
|
|
.filter(medias::owner_id.eq(user.id))
|
2019-08-28 11:37:03 +02:00
|
|
|
.order(medias::id.desc())
|
2019-03-19 14:37:56 +01:00
|
|
|
.offset(i64::from(min))
|
|
|
|
.limit(i64::from(max - min))
|
2019-03-06 14:11:36 +01:00
|
|
|
.load::<Media>(conn)
|
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count_for_user(conn: &Connection, user: &User) -> Result<i64> {
|
|
|
|
medias::table
|
|
|
|
.filter(medias::owner_id.eq(user.id))
|
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn category(&self) -> MediaCategory {
|
2019-01-27 13:44:09 +01:00
|
|
|
match &*self
|
2018-11-24 12:44:17 +01:00
|
|
|
.file_path
|
2021-11-27 23:53:13 +01:00
|
|
|
.rsplit_once('.')
|
|
|
|
.map(|x| x.1)
|
2018-12-06 18:54:16 +01:00
|
|
|
.expect("Media::category: extension error")
|
2019-01-27 13:44:09 +01:00
|
|
|
.to_lowercase()
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:59:34 +02:00
|
|
|
pub fn html(&self) -> Result<SafeString> {
|
|
|
|
let url = self.url()?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(match self.category() {
|
2019-03-06 14:11:36 +01:00
|
|
|
MediaCategory::Image => SafeString::trusted(&format!(
|
2018-12-06 18:54:16 +01:00
|
|
|
r#"<img src="{}" alt="{}" title="{}">"#,
|
2020-01-19 12:52:32 +01:00
|
|
|
url,
|
|
|
|
escape(&self.alt_text),
|
|
|
|
escape(&self.alt_text)
|
2018-12-06 18:54:16 +01:00
|
|
|
)),
|
2019-03-06 14:11:36 +01:00
|
|
|
MediaCategory::Audio => SafeString::trusted(&format!(
|
|
|
|
r#"<div class="media-preview audio"></div><audio src="{}" title="{}" controls></audio>"#,
|
2020-01-19 12:52:32 +01:00
|
|
|
url,
|
|
|
|
escape(&self.alt_text)
|
2018-12-06 18:54:16 +01:00
|
|
|
)),
|
2019-03-06 14:11:36 +01:00
|
|
|
MediaCategory::Video => SafeString::trusted(&format!(
|
|
|
|
r#"<video src="{}" title="{}" controls></video>"#,
|
2020-01-19 12:52:32 +01:00
|
|
|
url,
|
|
|
|
escape(&self.alt_text)
|
2018-12-06 18:54:16 +01:00
|
|
|
)),
|
2019-03-06 14:11:36 +01:00
|
|
|
MediaCategory::Unknown => SafeString::trusted(&format!(
|
|
|
|
r#"<a href="{}" class="media-preview unknown"></a>"#,
|
|
|
|
url,
|
|
|
|
)),
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
|
|
|
|
2019-05-10 22:59:34 +02:00
|
|
|
pub fn markdown(&self) -> Result<SafeString> {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(match self.category() {
|
2019-03-20 17:56:17 +01:00
|
|
|
MediaCategory::Image => {
|
2019-04-06 19:20:33 +02:00
|
|
|
SafeString::new(&format!("![{}]({})", escape(&self.alt_text), self.id))
|
2019-03-20 17:56:17 +01:00
|
|
|
}
|
2019-05-10 22:59:34 +02:00
|
|
|
MediaCategory::Audio | MediaCategory::Video => self.html()?,
|
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
|
|
|
}
|
|
|
|
|
2019-05-10 22:59:34 +02:00
|
|
|
pub fn url(&self) -> 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 {
|
2021-03-28 19:24:43 +02:00
|
|
|
let file_path = self.file_path.replace(path::MAIN_SEPARATOR, "/").replacen(
|
|
|
|
&CONFIG.media_directory,
|
|
|
|
"static/media",
|
|
|
|
1,
|
|
|
|
); // "static/media" from plume::routs::plume_media_files()
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(ap_url(&format!(
|
2021-02-25 03:01:47 +01:00
|
|
|
"{}/{}",
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()?.public_domain,
|
2021-02-25 03:01:47 +01:00
|
|
|
&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?
|
2021-01-30 11:24:16 +01:00
|
|
|
pub fn from_activity(conn: &DbConn, image: &Image) -> Result<Media> {
|
2021-11-27 23:53:13 +01:00
|
|
|
let remote_url = image
|
|
|
|
.object_props
|
|
|
|
.url_string()
|
|
|
|
.or(Err(Error::MissingApProperty))?;
|
2021-02-17 17:23:57 +01:00
|
|
|
let path = determine_mirror_file_path(&remote_url);
|
2021-11-27 23:53:13 +01:00
|
|
|
let parent = path.parent().ok_or(Error::InvalidValue)?;
|
2021-02-18 03:08:40 +01:00
|
|
|
if !parent.is_dir() {
|
|
|
|
DirBuilder::new().recursive(true).create(parent)?;
|
|
|
|
}
|
2018-10-31 10:40:20 +01:00
|
|
|
|
2021-11-27 23:53:13 +01:00
|
|
|
let mut dest = fs::File::create(path.clone())?;
|
2021-02-18 03:34:35 +01:00
|
|
|
// TODO: conditional GET
|
2021-12-05 11:27:57 +01:00
|
|
|
request::get(
|
|
|
|
remote_url.as_str(),
|
2022-05-02 05:58:01 +02:00
|
|
|
User::get_sender07(),
|
2021-12-05 11:27:57 +01:00
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
)?
|
2021-11-27 23:53:13 +01:00
|
|
|
.copy_to(&mut dest)?;
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2021-11-27 23:53:13 +01:00
|
|
|
Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?)
|
2021-02-23 17:05:19 +01:00
|
|
|
.and_then(|mut media| {
|
|
|
|
let mut updated = false;
|
|
|
|
|
2021-11-27 23:53:13 +01:00
|
|
|
let alt_text = image
|
|
|
|
.object_props
|
|
|
|
.content_string()
|
|
|
|
.or(Err(Error::NotFound))?;
|
2021-02-23 17:05:19 +01:00
|
|
|
let sensitive = image.object_props.summary_string().is_ok();
|
|
|
|
let content_warning = image.object_props.summary_string().ok();
|
|
|
|
if media.alt_text != alt_text {
|
|
|
|
media.alt_text = alt_text;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.is_remote {
|
|
|
|
media.is_remote = false;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.remote_url.is_some() {
|
|
|
|
media.remote_url = None;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.sensitive != sensitive {
|
|
|
|
media.sensitive = sensitive;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.content_warning != content_warning {
|
|
|
|
media.content_warning = content_warning;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
diesel::update(&media).set(&media).execute(&**conn)?;
|
|
|
|
}
|
|
|
|
Ok(media)
|
|
|
|
})
|
|
|
|
.or_else(|_| {
|
|
|
|
Media::insert(
|
2021-01-30 11:24:16 +01:00
|
|
|
conn,
|
2021-02-23 17:05:19 +01:00
|
|
|
NewMedia {
|
2021-11-27 23:53:13 +01:00
|
|
|
file_path: path.to_str().ok_or(Error::InvalidValue)?.to_string(),
|
|
|
|
alt_text: image
|
|
|
|
.object_props
|
|
|
|
.content_string()
|
|
|
|
.or(Err(Error::NotFound))?,
|
2021-02-23 17:05:19 +01:00
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: image.object_props.summary_string().is_ok(),
|
|
|
|
content_warning: image.object_props.summary_string().ok(),
|
2022-05-02 12:24:36 +02:00
|
|
|
owner_id: User::from_id(
|
2021-02-23 17:05:19 +01:00
|
|
|
conn,
|
|
|
|
image
|
|
|
|
.object_props
|
|
|
|
.attributed_to_link_vec::<Id>()
|
2021-11-27 23:53:13 +01:00
|
|
|
.or(Err(Error::NotFound))?
|
2021-02-23 17:05:19 +01:00
|
|
|
.into_iter()
|
2021-11-27 23:53:13 +01:00
|
|
|
.next()
|
|
|
|
.ok_or(Error::NotFound)?
|
2021-02-23 17:05:19 +01:00
|
|
|
.as_ref(),
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?
|
|
|
|
.id,
|
|
|
|
},
|
2019-04-17 19:31:47 +02:00
|
|
|
)
|
2021-02-23 17:05:19 +01:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2019-04-06 19:20:33 +02:00
|
|
|
|
2022-02-26 02:58:49 +01:00
|
|
|
// TODO: merge with save_remote?
|
|
|
|
pub fn from_activity07(conn: &DbConn, image: &Image07) -> Result<Media> {
|
|
|
|
let remote_url = image
|
|
|
|
.url()
|
|
|
|
.and_then(|url| url.to_as_uri())
|
|
|
|
.ok_or(Error::MissingApProperty)?;
|
|
|
|
let path = determine_mirror_file_path(&remote_url);
|
|
|
|
let parent = path.parent().ok_or(Error::InvalidValue)?;
|
|
|
|
if !parent.is_dir() {
|
|
|
|
DirBuilder::new().recursive(true).create(parent)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut dest = fs::File::create(path.clone())?;
|
|
|
|
// TODO: conditional GET
|
|
|
|
request::get(
|
|
|
|
remote_url.as_str(),
|
2022-05-02 05:58:01 +02:00
|
|
|
User::get_sender07(),
|
2022-02-26 02:58:49 +01:00
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
)?
|
|
|
|
.copy_to(&mut dest)?;
|
|
|
|
|
|
|
|
Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?)
|
|
|
|
.and_then(|mut media| {
|
|
|
|
let mut updated = false;
|
|
|
|
|
|
|
|
let alt_text = image
|
|
|
|
.content()
|
|
|
|
.and_then(|content| content.to_as_string())
|
|
|
|
.ok_or(Error::NotFound)?;
|
|
|
|
let summary = image.summary().and_then(|summary| summary.to_as_string());
|
|
|
|
let sensitive = summary.is_some();
|
|
|
|
let content_warning = summary;
|
|
|
|
if media.alt_text != alt_text {
|
|
|
|
media.alt_text = alt_text;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.is_remote {
|
|
|
|
media.is_remote = false;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.sensitive != sensitive {
|
|
|
|
media.sensitive = sensitive;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if media.content_warning != content_warning {
|
|
|
|
media.content_warning = content_warning;
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
diesel::update(&media).set(&media).execute(&**conn)?;
|
|
|
|
}
|
|
|
|
Ok(media)
|
|
|
|
})
|
|
|
|
.or_else(|_| {
|
|
|
|
let summary = image.summary().and_then(|summary| summary.to_as_string());
|
|
|
|
Media::insert(
|
|
|
|
conn,
|
|
|
|
NewMedia {
|
|
|
|
file_path: path.to_str().ok_or(Error::InvalidValue)?.to_string(),
|
|
|
|
alt_text: image
|
|
|
|
.content()
|
|
|
|
.and_then(|content| content.to_as_string())
|
|
|
|
.ok_or(Error::NotFound)?,
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: summary.is_some(),
|
|
|
|
content_warning: summary,
|
2022-05-02 12:24:36 +02:00
|
|
|
owner_id: User::from_id(
|
2022-02-26 02:58:49 +01:00
|
|
|
conn,
|
|
|
|
&image
|
|
|
|
.attributed_to()
|
|
|
|
.and_then(|attributed_to| attributed_to.to_as_uri())
|
|
|
|
.ok_or(Error::MissingApProperty)?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)?
|
|
|
|
.id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-06 19:20:33 +02:00
|
|
|
pub fn get_media_processor<'a>(conn: &'a Connection, user: Vec<&User>) -> MediaProcessor<'a> {
|
|
|
|
let uid = user.iter().map(|u| u.id).collect::<Vec<_>>();
|
|
|
|
Box::new(move |id| {
|
|
|
|
let media = Media::get(conn, id).ok()?;
|
|
|
|
// if owner is user or check is disabled
|
|
|
|
if uid.contains(&media.owner_id) || uid.is_empty() {
|
2019-05-10 22:59:34 +02:00
|
|
|
Some((media.url().ok()?, media.content_warning))
|
2019-04-06 19:20:33 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 17:23:57 +01:00
|
|
|
fn determine_mirror_file_path(url: &str) -> PathBuf {
|
|
|
|
let mut file_path = Path::new(&super::CONFIG.media_directory).join(REMOTE_MEDIA_DIRECTORY);
|
|
|
|
Url::parse(url)
|
|
|
|
.map(|url| {
|
|
|
|
if !url.has_host() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file_path.push(url.host_str().unwrap());
|
|
|
|
for segment in url.path_segments().expect("FIXME") {
|
|
|
|
file_path.push(segment);
|
|
|
|
}
|
|
|
|
// TODO: handle query
|
|
|
|
// HINT: Use characters which must be percent-encoded in path as separator between path and query
|
|
|
|
// HINT: handle extension
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
warn!("Failed to parse url: {} {}", &url, err);
|
|
|
|
let ext = url
|
|
|
|
.rsplit('.')
|
|
|
|
.next()
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
.unwrap_or_else(|| String::from("png"));
|
2021-11-27 23:53:13 +01:00
|
|
|
file_path.push(format!("{}.{}", GUID::rand(), ext));
|
2021-02-17 17:23:57 +01:00
|
|
|
});
|
|
|
|
file_path
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::{tests::db, users::tests as usersTests, Connection as Conn};
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::Connection;
|
|
|
|
use std::env::{current_dir, set_current_dir};
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
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();
|
2019-03-20 17:56:17 +01:00
|
|
|
(
|
|
|
|
users,
|
|
|
|
vec![
|
|
|
|
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())
|
2019-03-20 17:56:17 +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() {
|
2022-01-10 14:04:57 +01:00
|
|
|
if let Some(err) = media.delete(conn).err() {
|
|
|
|
match &err {
|
|
|
|
Error::Io(e) => match e.kind() {
|
|
|
|
std::io::ErrorKind::NotFound => (),
|
|
|
|
_ => panic!("{:?}", err),
|
|
|
|
},
|
|
|
|
_ => panic!("{:?}", err),
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
},
|
2019-03-20 17:56:17 +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,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(Media::for_user(conn, u1.id)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id));
|
|
|
|
assert!(!Media::for_user(conn, u2.id)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id));
|
2018-12-29 09:36:07 +01:00
|
|
|
media.set_owner(conn, u2).unwrap();
|
2019-03-20 17:56:17 +01:00
|
|
|
assert!(!Media::for_user(conn, u1.id)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id));
|
|
|
|
assert!(Media::for_user(conn, u2.id)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|m| m.id == media.id));
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
clean(conn);
|
|
|
|
Ok(())
|
|
|
|
});
|
2018-10-31 10:40:20 +01:00
|
|
|
}
|
2018-09-02 13:34:48 +02:00
|
|
|
}
|