Skip to content

Commit

Permalink
feat: finished display endpoint and added base64 transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Jan 20, 2022
1 parent 49cf5ea commit 1ca624d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ clokwerk = "0.3"
exif = { git = "https://github.com/RouHim/exif-rs", rev = "1334172dc3c3d48a159619c7f1b1b952764ec530" }
rayon = "1.5"
regex = "1.5"
base64 = "0.13"

# Strip debug symbols in release mode
[profile.release]
Expand Down
77 changes: 52 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async fn main() -> std::io::Result<()> {
.service(list_resources)
.service(list_this_week_resources)
.service(get_resource)
.service(get_resource_base64)
.service(get_resource_metadata)
.service(get_resource_metadata_display)
)
Expand Down Expand Up @@ -86,39 +87,65 @@ async fn list_this_week_resources(kv_reader: web::Data<ReadHandle<String, String

#[get("{resource_id}")]
async fn get_resource(resources_id: web::Path<String>, kv_reader: web::Data<ReadHandle<String, String>>, web_dav_client: web::Data<WebDavClient>) -> HttpResponse {
println!("requesting resource with id: {}", resources_id);

let guard = kv_reader.get(resources_id.as_str()).unwrap();
let web_dav_resource: WebDavResource = serde_json::from_str(guard.get_one().unwrap()).unwrap();
let response_data = web_dav_client.request_resource_data(&web_dav_resource).bytes().unwrap();
let resource_data = kv_reader.get_one(resources_id.as_str())
.map(|value| value.to_string())
.and_then(|resource_json_string| serde_json::from_str(resource_json_string.as_str()).ok())
.and_then(|web_dav_resource| web_dav_client.request_resource_data(&web_dav_resource).bytes().ok());

if let Some(resource_data) = resource_data {
HttpResponse::Ok()
.content_type("image/jpeg")
.body(resource_data.to_vec())
} else {
HttpResponse::InternalServerError().finish()
}
}

HttpResponse::Ok()
.content_type(web_dav_resource.content_type)
.body(response_data.to_vec())
#[get("{resource_id}/base64")]
async fn get_resource_base64(resources_id: web::Path<String>, kv_reader: web::Data<ReadHandle<String, String>>, web_dav_client: web::Data<WebDavClient>) -> HttpResponse {
let base64_image = kv_reader.get_one(resources_id.as_str())
.map(|value| value.to_string())
.and_then(|resource_json_string| serde_json::from_str(resource_json_string.as_str()).ok())
.and_then(|web_dav_resource| web_dav_client.request_resource_data(&web_dav_resource).bytes().ok())
.map(|resource_data| base64::encode(&resource_data))
.map(|base64_string| format!("data:image/jpeg;base64,{}", base64_string));

if let Some(base64_image) = base64_image {
HttpResponse::Ok()
.content_type("plain/text")
.body(base64_image)
} else {
HttpResponse::InternalServerError().finish()
}
}

#[get("{resource_id}/metadata")]
async fn get_resource_metadata(resources_id: web::Path<String>, kv_reader: web::Data<ReadHandle<String, String>>) -> HttpResponse {
println!("requesting resource with id: {}", resources_id);

let guard = kv_reader.get(resources_id.as_str()).unwrap();
let data = guard.get_one().unwrap();

HttpResponse::Ok()
.content_type("application/json")
.body(data)
let metadata = kv_reader.get_one(resources_id.as_str())
.map(|value| value.to_string());

if let Some(metadata) = metadata {
HttpResponse::Ok()
.content_type("application/json")
.body(metadata)
} else {
HttpResponse::InternalServerError().finish()
}
}

#[get("{resource_id}/display")]
async fn get_resource_metadata_display(resources_id: web::Path<String>, kv_reader: web::Data<ReadHandle<String, String>>) -> HttpResponse {
println!("requesting resource with id: {}", resources_id);

let guard = kv_reader.get(resources_id.as_str()).unwrap();
let web_dav_resource: WebDavResource = serde_json::from_str(guard.get_one().unwrap()).unwrap();
let display_value = resource_processor::build_display_value(web_dav_resource);

HttpResponse::Ok()
.content_type("plain/text")
.body(display_value)
let display_value = kv_reader.get_one(resources_id.as_str())
.map(|value| value.to_string())
.and_then(|resource_json_string| serde_json::from_str(resource_json_string.as_str()).ok())
.map(resource_processor::build_display_value);

if let Some(display_value) = display_value {
HttpResponse::Ok()
.content_type("plain/text")
.body(display_value)
} else {
HttpResponse::InternalServerError().finish()
}
}

33 changes: 28 additions & 5 deletions src/resource_processor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt::Display;
use std::collections::HashMap;

use evmap::ReadHandle;

use crate::geo_location::GeoLocation;
use crate::WebDavResource;

pub fn md5(string: &str) -> String {
Expand Down Expand Up @@ -28,12 +30,33 @@ pub fn build_display_value(resource: WebDavResource) -> String {
let mut display_value: String = String::new();

if let Some(taken_date) = resource.taken {
display_value.push_str(taken_date.date().format("%Y-%m-%d").to_string().as_str());
display_value.push_str(
taken_date.date().format("%d.%m.%Y").to_string().as_str()
);
}

if let Some(location) = resource.location {
let city_name = resource.location.and_then(resolve_city_name);

if let Some(city_name) = city_name {
if resource.taken.is_some() {
display_value.push_str(", ");
}

display_value.push_str(city_name.as_str());
}

return display_value;
}
display_value
}

fn resolve_city_name(geo_location: GeoLocation) -> Option<String> {
reqwest::blocking::get(format!(
"https://api.bigdatacloud.net/data/reverse-geocode?latitude={}&longitude={}&localityLanguage=de&key={}",
geo_location.latitude,
geo_location.longitude,
"6b8aad17eba7449d9d93c533359b0384",
))
.and_then(|response| response.text()).ok()
.and_then(|json_string| serde_json::from_str::<HashMap<String, serde_json::Value>>(&json_string).ok())
.and_then(|json_data| json_data.get("city").map(|city| city.to_string()))
}

2 changes: 1 addition & 1 deletion static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ window.onload = () => {

function slideshowTick() {
let photoDataRequest = new XMLHttpRequest();
photoDataRequest.open("GET", window.location.href + "api/resources/" + resources[currentIndex]);
photoDataRequest.open("GET", window.location.href + "api/resources/" + resources[currentIndex] + "/base64");
photoDataRequest.send();
photoDataRequest.onload = () => document.getElementById("slideshow-image").src = photoDataRequest.response;

Expand Down

0 comments on commit 1ca624d

Please sign in to comment.