1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 08:25:16 +00:00

Start to fetch page content

This commit is contained in:
2021-04-08 16:45:29 +02:00
parent d53f0aae7b
commit 74adef5221
5 changed files with 495 additions and 90 deletions

View File

@ -6,7 +6,6 @@ use crate::api_data::post_api::PostAPI;
use crate::api_data::posts_targets_api::PostsTargets;
use crate::api_data::res_create_post::ResCreatePost;
use crate::constants::{PATH_POST_IMAGES, PATH_POST_PDF};
use crate::routes::RequestResult;
use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group::GroupAccessLevel;
@ -15,9 +14,11 @@ use crate::data::new_survey::NewSurvey;
use crate::data::notification::NotifEventType;
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink};
use crate::helpers::{friends_helper, groups_helper, notifications_helper, posts_helper, survey_helper, user_helper};
use crate::routes::RequestResult;
use crate::utils::date_utils::time;
use crate::utils::string_utils::{check_string_before_insert, check_youtube_id};
use crate::utils::user_data_utils::user_data_path;
use crate::utils::webpage_utils::get_post_web_link;
impl PostFile {
/// Initialize a `PostFile` instance based on a file that have just been created
@ -163,14 +164,18 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
let url = r.post_url_opt("url", true)?
.ok_or(ExecError::new("Missing url!"))?;
// For now, for safety, we do not fetch page content
// But this might change in the future
PostKind::POST_KIND_WEBLINK(PostWebLink {
url,
title: None,
description: None,
image: None,
})
match get_post_web_link(&url) {
Ok(info) => PostKind::POST_KIND_WEBLINK(info),
Err(e) => {
eprintln!("Error while fetching page metadata: {}", e);
PostKind::POST_KIND_WEBLINK(PostWebLink {
url,
title: None,
description: None,
image: None,
})
}
}
}
"pdf" => {

View File

@ -10,4 +10,5 @@ pub mod string_utils;
pub mod pdf_utils;
pub mod mp3_utils;
pub mod mp4_utils;
pub mod zip_utils;
pub mod zip_utils;
pub mod webpage_utils;

View File

@ -0,0 +1,32 @@
//! # Webpage utilities
//!
//! @author Pierre Hubert
use std::time::Duration;
use webpage::{Webpage, WebpageOptions};
use crate::data::error::{ExecError, Res};
use crate::data::post::PostWebLink;
/// Attempt to get information about a URL given by a user
pub fn get_post_web_link(url: &str) -> Res<PostWebLink> {
let mut options = WebpageOptions::default();
options.max_redirections = 2;
options.follow_location = true;
options.useragent = "OpenGraph Fetcher v1.0".to_string();
options.timeout = Duration::from_secs(5);
let page = Webpage::from_url(url, options)?;
/*if !page.http.content_type.to_lowercase().starts_with("text/html") {
return Err(ExecError::boxed_string(format!("The page at {} as an invalid content type: {}!", url, page.http.content_type)));
}*/
Ok(PostWebLink {
url: url.to_string(),
title: page.html.opengraph.properties.get("title").map(String::to_string),
description: page.html.opengraph.properties.get("description").map(String::to_string),
image: page.html.opengraph.images.first().map(|m| m.url.to_string()),
})
}