Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Simplify CLI download routing

Manav Rathic7d043d045a7
diff --git a/rust/apps/cli/src/api/client.rs b/rust/apps/cli/src/api/client.rs
index 7121764b1d..1de3628c22 100644
--- a/rust/apps/cli/src/api/client.rs
+++ b/rust/apps/cli/src/api/client.rs
@@ -16,9 +16,9 @@ pub struct AppClient {
token: RwLock<Option<String>>,
}
-struct DownloadProxies {
- files: Api,
- thumbnails: Api,
+pub(crate) struct DownloadProxies {
+ pub(crate) files: Api,
+ pub(crate) thumbnails: Api,
}
impl AppClient {
@@ -67,7 +67,11 @@ impl AppClient {
&self.museum
}
- pub(crate) async fn download_file(&self, url: &str) -> Result<Vec<u8>> {
+ pub(crate) fn download_proxies(&self) -> Option<&DownloadProxies> {
+ self.proxies.as_ref()
+ }
+
+ pub(crate) async fn download_url(&self, url: &str) -> Result<Vec<u8>> {
Ok(
http::retry_with_profile(RetryProfile::Background, || async {
self.museum
@@ -82,14 +86,6 @@ impl AppClient {
.await?,
)
}
-
- pub(crate) async fn download_file_from_proxy(&self, file_id: i64) -> Result<Vec<u8>> {
- download_from_proxy(&self.proxies.as_ref().unwrap().files, file_id).await
- }
-
- pub(crate) async fn download_thumbnail_from_proxy(&self, file_id: i64) -> Result<Vec<u8>> {
- download_from_proxy(&self.proxies.as_ref().unwrap().thumbnails, file_id).await
- }
}
fn new_api(http: &Http, origin: &str, client_package: &str) -> Api {
@@ -105,7 +101,7 @@ fn new_api(http: &Http, origin: &str, client_package: &str) -> Api {
)
}
-async fn download_from_proxy(api: &Api, file_id: i64) -> Result<Vec<u8>> {
+pub(crate) async fn download_from_proxy(api: &Api, file_id: i64) -> Result<Vec<u8>> {
Ok(
http::retry_with_profile(RetryProfile::Background, || async {
api.get("/")
@@ -125,8 +121,17 @@ mod tests {
use super::*;
use mockito::{Matcher, Server};
+ #[test]
+ fn selects_download_backend_from_origin() {
+ let production = AppClient::new(None, App::Photos).unwrap();
+ assert!(production.download_proxies().is_some());
+
+ let self_hosted = AppClient::new(Some("https://example.com".into()), App::Photos).unwrap();
+ assert!(self_hosted.download_proxies().is_none());
+ }
+
#[tokio::test]
- async fn app_clients_keep_auth_scoped_to_app() {
+ async fn app_clients_keep_museum_auth_scoped_to_app() {
let mut server = Server::new_async().await;
let auth_download = server
.mock("GET", "/")
diff --git a/rust/apps/cli/src/api/methods.rs b/rust/apps/cli/src/api/methods.rs
index 2028155c2d..5d106d21de 100644
--- a/rust/apps/cli/src/api/methods.rs
+++ b/rust/apps/cli/src/api/methods.rs
@@ -1,13 +1,10 @@
-use crate::api::client::AppClient;
+use crate::api::client::{AppClient, download_from_proxy};
use crate::api::models::{
Collection, File, GetCollectionsResponse, GetDiffResponse, GetFileResponse, GetFileUrlResponse,
GetFilesResponse, GetThumbnailUrlResponse, UserDetails,
};
use crate::models::error::Result;
use ente_core::http;
-use ente_core::urls::{PRODUCTION_API_ORIGIN, file_download_url};
-
-const THUMBNAILS_ORIGIN: &str = "https://thumbnails.ente.com";
pub struct ApiMethods<'a> {
client: &'a AppClient,
@@ -123,10 +120,7 @@ impl<'a> ApiMethods<'a> {
Ok((response.diff, response.has_more))
}
- pub async fn get_file_url(&self, file_id: i64) -> Result<String> {
- if self.client.origin() == PRODUCTION_API_ORIGIN {
- return Ok(file_download_url(self.client.origin(), file_id));
- }
+ async fn get_signed_file_url(&self, file_id: i64) -> Result<String> {
let api = self.client.api();
let response: GetFileUrlResponse = http::retry(|| async {
api.get(&format!("/files/download/v2/{file_id}"))
@@ -141,10 +135,7 @@ impl<'a> ApiMethods<'a> {
Ok(response.url)
}
- pub async fn get_thumbnail_url(&self, file_id: i64) -> Result<String> {
- if self.client.origin() == PRODUCTION_API_ORIGIN {
- return Ok(format!("{THUMBNAILS_ORIGIN}/?fileID={file_id}"));
- }
+ async fn get_signed_thumbnail_url(&self, file_id: i64) -> Result<String> {
let api = self.client.api();
let response: GetThumbnailUrlResponse = http::retry(|| async {
api.get(&format!("/files/preview/v2/{file_id}"))
@@ -160,19 +151,23 @@ impl<'a> ApiMethods<'a> {
}
pub async fn download_file(&self, file_id: i64) -> Result<Vec<u8>> {
- if self.client.origin() == PRODUCTION_API_ORIGIN {
- return self.client.download_file_from_proxy(file_id).await;
+ match self.client.download_proxies() {
+ Some(proxies) => download_from_proxy(&proxies.files, file_id).await,
+ None => {
+ let url = self.get_signed_file_url(file_id).await?;
+ self.client.download_url(&url).await
+ }
}
- let url = self.get_file_url(file_id).await?;
- self.client.download_file(&url).await
}
pub async fn download_thumbnail(&self, file_id: i64) -> Result<Vec<u8>> {
- if self.client.origin() == PRODUCTION_API_ORIGIN {
- return self.client.download_thumbnail_from_proxy(file_id).await;
+ match self.client.download_proxies() {
+ Some(proxies) => download_from_proxy(&proxies.thumbnails, file_id).await,
+ None => {
+ let url = self.get_signed_thumbnail_url(file_id).await?;
+ self.client.download_url(&url).await
+ }
}
- let url = self.get_thumbnail_url(file_id).await?;
- self.client.download_file(&url).await
}
pub async fn get_trash(&self, since_time: i64) -> Result<(Vec<File>, bool)> {
@@ -227,21 +222,6 @@ mod tests {
use super::*;
use mockito::{Matcher, Server};
- #[tokio::test]
- async fn test_production_urls() {
- let api = AppClient::new(None, crate::models::account::App::Photos).unwrap();
- let methods = ApiMethods::new(&api);
-
- assert_eq!(
- methods.get_file_url(12345).await.unwrap(),
- "https://files.ente.com/?fileID=12345"
- );
- assert_eq!(
- methods.get_thumbnail_url(12345).await.unwrap(),
- "https://thumbnails.ente.com/?fileID=12345"
- );
- }
-
#[tokio::test]
async fn test_self_hosted_download_uses_signed_url() {
let mut server = Server::new_async().await;