Skip to content

Commit

Permalink
telegram2photoprism works with photoprism release 240531
Browse files Browse the repository at this point in the history
  • Loading branch information
dssysolyatin committed Jun 1, 2024
1 parent 32e954f commit 078642c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ If you want to use environment variables or specify tags, telegram bot api serve
| --photoprism-url (env: TELEGRAM2PHOTOPRISM_PHOTOPRISM_URL) | PhotoPrism URL | - |
| --photoprism-username (env: TELEGRAM2PHOTOPRISM_PHOTOPRISM_USERNAME) | PhotoPrism username | - |
| --photoprism-password (env: TELEGRAM2PHOTOPRISM_PHOTOPRISM_PASSWORD) | PhotoPrism password | - |
| --photoprism-session-refresh-sec (env: TELEGRAM2PHOTOPRISM_PHOTOPRISM_SESSION_REFRESH_SEC) | Number of seconds after which the bot should obtain a new X-SESSION-ID using the username and password. Should be less than PHOTOPRISM_SESSION_TIMEOUT ([More Info](https://docs.photoprism.app/getting-started/config-options/)) | 86400 |
| --photoprism-session-refresh-sec (env: TELEGRAM2PHOTOPRISM_PHOTOPRISM_SESSION_REFRESH_SEC) | Number of seconds after which the bot should obtain a new X-Auth-Token using the username and password. Should be less than PHOTOPRISM_SESSION_TIMEOUT ([More Info](https://docs.photoprism.app/getting-started/config-options/)) | 86400 |
| --locale (env: TELEGRAM2PHOTOPRISM_LOCALE) | Locale | en |
| --working-dir (env: TELEGRAM2PHOTOPRISM_WORKING_DIR) | Directory for temporary downloaded files | CURRENT_DIR |
| --disallow-compressed-files (env: TELEGRAM2PHOTOPRISM_DISALLOW_COMPRESSED_FILES) | By default, Telegram compresses videos and images if they are not attached as files. The quality of the files is significantly reduced after compression. This option prohibits the bot from uploading compressed files to the PhotoPrism server | - |
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct Args {
/// PhotoPrism password
#[arg(long, env = "TELEGRAM2PHOTOPRISM_PHOTOPRISM_PASSWORD")]
photoprism_password: String,
/// Number of seconds after which the bot should obtain a new X-SESSION-ID using the username and password.
/// Number of seconds after which the bot should obtain a new X-Auth-Token using the username and password.
/// Should be less than PHOTOPRISM_SESSION_TIMEOUT (https://docs.photoprism.app/getting-started/config-options/)
#[arg(
long,
Expand Down
30 changes: 15 additions & 15 deletions src/photo_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::path::Path;
use std::string::ToString;
use std::sync::Arc;
use std::time::Duration;

Expand All @@ -14,8 +15,8 @@ use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};

use crate::photo_service::PhotoPrismServiceError::{
AddLabelFailed, AuthenticationError, CanNotFindPhotoByHash, IndexingFailed, UploadFailed,
UserIDIsMissing, XSessionHeaderMissing,
AccessTokenIsMissing, AddLabelFailed, AuthenticationError, CanNotFindPhotoByHash,
IndexingFailed, UploadFailed, UserIDIsMissing,
};

#[derive(Debug, Clone)]
Expand All @@ -33,8 +34,8 @@ pub trait PhotoService {
pub enum PhotoPrismServiceError {
#[error("Failed to authenticate at PhotoPrism service {0}.")]
AuthenticationError(String),
#[error("X-Session-ID header is missing.")]
XSessionHeaderMissing,
#[error("access_token is missing.")]
AccessTokenIsMissing,
#[error("user.ID is missing in response json {0}.")]
UserIDIsMissing(String),
#[error("Failed to upload file {file} to PhotoPrism server: {details}.")]
Expand Down Expand Up @@ -72,7 +73,7 @@ pub struct PhotoPrismPhotoService {
}

pub struct PhotoPrismUser {
pub session_id: String,
pub access_token: String,
uid: String,
}

Expand All @@ -86,7 +87,7 @@ impl PhotoPrismPhotoService {
let client = reqwest::Client::new();
let user_cache = Cache::builder()
.max_capacity(1)
// Reload X-Session-ID every N seconds
// Reload X-Auth-Token every N seconds
.time_to_live(Duration::from_secs(session_refresh_sec))
.build();

Expand Down Expand Up @@ -129,23 +130,22 @@ impl PhotoPrismPhotoService {
return Err(AuthenticationError(auth_resp.text().await?));
}

let session_id = auth_resp
.headers()
.get("X-Session-ID")
.map(|header| header.to_str().map_err(|e| anyhow::Error::from(e).into()))
.unwrap_or_else(|| Err(XSessionHeaderMissing))?
.to_owned();

let user_data = auth_resp.json::<serde_json::Value>().await?;

let access_token = user_data
.get("access_token")
.and_then(|v| v.as_str().map(Ok))
.unwrap_or_else(|| Err(AccessTokenIsMissing))?
.to_owned();

let uid = user_data
.get("user")
.and_then(|v| v.get("UID"))
.and_then(|v| v.as_str().map(Ok))
.unwrap_or_else(|| Err(UserIDIsMissing(user_data.to_string())))?
.to_owned();

Ok(PhotoPrismUser { session_id, uid })
Ok(PhotoPrismUser { access_token, uid })
}

async fn calculate_sha1<P: AsRef<Path>>(file_path: P) -> Result<String, anyhow::Error> {
Expand All @@ -167,7 +167,7 @@ impl PhotoPrismPhotoService {
// It's better to have a separate method instead.
let user = self.get_user().await?;
let response = request_builder
.header("X-Session-ID", &user.session_id)
.header("X-Auth-Token", &user.access_token)
.send()
.await?;
Ok(response)
Expand Down
4 changes: 2 additions & 2 deletions tests/photoprism_api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ async fn test_refresh_token() -> Result<(), anyhow::Error> {
sleep(Duration::from_secs(1)).await;
let user_after_reload = photoprism_service.get_user().await?;

if user_before_reload.session_id != user_after_reload.session_id {
if user_before_reload.access_token != user_after_reload.access_token {
debug!(
"old session id: {}, new session id: {}",
user_before_reload.session_id, user_after_reload.session_id
user_before_reload.access_token, user_after_reload.access_token
);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/photoprism_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Image for PhotoPrismContainer {
}

fn tag(&self) -> String {
"latest".to_string()
"240531".to_string()
}

fn ready_conditions(&self) -> Vec<WaitFor> {
Expand Down

0 comments on commit 078642c

Please sign in to comment.