mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-06 14:45:36 +01:00
89 lines
1.9 KiB
Rust
89 lines
1.9 KiB
Rust
use reqwest::{Response, multipart::{Form, Part}, header};
|
|
use serde::Deserialize;
|
|
|
|
use crate::config::CONFIG;
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct UploadData {
|
|
pub chat_id: i64,
|
|
pub message_id: i64
|
|
}
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct UploadResult {
|
|
pub backend: String,
|
|
pub data: UploadData
|
|
}
|
|
|
|
|
|
pub async fn download_from_telegram_files(
|
|
message_id: i64,
|
|
chat_id: i64
|
|
) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
|
|
let url = format!(
|
|
"{}/api/v1/files/download_by_message/{chat_id}/{message_id}",
|
|
CONFIG.files_url
|
|
);
|
|
|
|
let response = reqwest::Client::new()
|
|
.get(url)
|
|
.header("Authorization", CONFIG.library_api_key.clone())
|
|
.send()
|
|
.await?
|
|
.error_for_status()?;
|
|
|
|
Ok(response)
|
|
}
|
|
|
|
|
|
pub async fn upload_to_telegram_files(
|
|
data_response: Response,
|
|
caption: String
|
|
) -> Result<UploadData, Box<dyn std::error::Error + Send + Sync>> {
|
|
let url = format!(
|
|
"{}/api/v1/files/upload/",
|
|
CONFIG.files_url
|
|
);
|
|
|
|
let headers = data_response.headers();
|
|
|
|
let file_size = headers
|
|
.get(header::CONTENT_LENGTH)
|
|
.unwrap()
|
|
.to_str()
|
|
.unwrap()
|
|
.to_string();
|
|
|
|
let filename = headers
|
|
.get("x-filename-b64-ascii")
|
|
.unwrap()
|
|
.to_str()
|
|
.unwrap()
|
|
.to_string();
|
|
|
|
let part = Part::stream(data_response)
|
|
.file_name(filename);
|
|
|
|
let form = Form::new()
|
|
.text("caption", caption)
|
|
.text("file_size", file_size)
|
|
.part("file", part);
|
|
|
|
let response = reqwest::Client::new()
|
|
.post(url)
|
|
.header("Authorization", CONFIG.library_api_key.clone())
|
|
.multipart(form)
|
|
.send()
|
|
.await?
|
|
.error_for_status()?;
|
|
|
|
match response.json::<UploadResult>().await {
|
|
Ok(v) => Ok(v.data),
|
|
Err(err) => {
|
|
Err(Box::new(err))
|
|
},
|
|
}
|
|
}
|