-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonolithic_context.rs
More file actions
46 lines (38 loc) · 1.23 KB
/
monolithic_context.rs
File metadata and controls
46 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use image::RgbImage;
use crate::contexts::app::App;
use crate::types::{User, UserId};
impl App {
pub async fn get_user(&self, user_id: &UserId) -> anyhow::Result<User> {
let user = sqlx::query_as(
"SELECT name, email, profile_picture_object_id FROM users WHERE id = $1",
)
.bind(user_id.0 as i64)
.fetch_one(&self.database)
.await?;
Ok(user)
}
pub async fn fetch_storage_object(&self, object_id: &str) -> anyhow::Result<Vec<u8>> {
let output = self
.storage_client
.get_object()
.bucket(&self.bucket_id)
.key(object_id)
.send()
.await?;
let data = output.body.collect().await?.into_bytes().to_vec();
Ok(data)
}
pub async fn get_user_profile_picture(
&self,
user_id: &UserId,
) -> anyhow::Result<Option<RgbImage>> {
let user = self.get_user(user_id).await?;
if let Some(object_id) = user.profile_picture_object_id {
let data = self.fetch_storage_object(&object_id).await?;
let image = image::load_from_memory(&data)?.to_rgb8();
Ok(Some(image))
} else {
Ok(None)
}
}
}