Skip to content

Commit

Permalink
feat(smb): read single resource from smb client
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Oct 16, 2022
1 parent 025ccd0 commit 31ecad5
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN cargo build --release
# # # # # # # # # # # # # # # # # # # #
# Run image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
FROM alpine as runtime
FROM alpine:3 as runtime

ENV CACHE_DIR "/cache"
ENV RESOURCE_PATHS "/resources"
Expand Down
10 changes: 4 additions & 6 deletions src/exif_reader.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use chrono::{NaiveDate, NaiveDateTime};
use exif::{Exif, In, Tag};


use crate::{geo_location};
use crate::geo_location;
use crate::geo_location::GeoLocation;
use crate::image_processor::ImageOrientation;


/// Reads the exif date from a given exif data entry
/// Primarily the exif date is used to determine the date the image was taken
/// If the exif date is not available, the gps date is used
Expand Down Expand Up @@ -145,9 +143,9 @@ fn parse_from_str(shard: &str) -> Option<NaiveDate> {
"%Y%m%d", // 20010708
"signal-%Y-%m-%d-%Z",
]
.iter()
.filter_map(|format| NaiveDate::parse_from_str(shard, format).ok())
.collect();
.iter()
.filter_map(|format| NaiveDate::parse_from_str(shard, format).ok())
.collect();

if parse_results.is_empty() {
None
Expand Down
6 changes: 2 additions & 4 deletions src/filesystem_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use core::option::Option::None;
use std::fs;
use std::path::{Path, PathBuf};

use exif::Exif;
use log::error;

use crate::{filesystem_client, resource_processor, resource_reader, samba_client, utils};
use crate::resource_reader::{RemoteResource, RemoteResourceType};
use crate::samba_client::create_smb_client;
use crate::{resource_processor, resource_reader, utils};

/// Reads all files of a folder and returns all found resources
/// The folder is recursively searched
Expand Down Expand Up @@ -83,4 +81,4 @@ pub fn fill_exif_data(resource: &RemoteResource) -> RemoteResource {
let maybe_exif_data = exif_reader.read_from_container(&mut bufreader).ok();

resource_reader::fill_exif_data(resource, maybe_exif_data)
}
}
29 changes: 13 additions & 16 deletions src/integration_test_config_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::{env, fs};
Expand All @@ -21,12 +20,10 @@ async fn test_get_slideshow_interval() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
.await;

// AND slideshow interval is set
let slideshow_interval: String = rand::thread_rng().gen::<u16>().to_string();
Expand All @@ -40,10 +37,10 @@ async fn test_get_slideshow_interval() {
.uri("/api/config/interval/slideshow")
.to_request(),
)
.await
.to_vec(),
.await
.to_vec(),
)
.unwrap();
.unwrap();

// THEN the response should contain the correct interval
assert_that!(response).is_equal_to(&slideshow_interval);
Expand All @@ -60,7 +57,7 @@ async fn test_get_refresh_interval() {
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
.await;

// AND refresh interval is set
let refresh_interval: String = rand::thread_rng().gen::<u16>().to_string();
Expand All @@ -74,10 +71,10 @@ async fn test_get_refresh_interval() {
.uri("/api/config/interval/refresh")
.to_request(),
)
.await
.to_vec(),
.await
.to_vec(),
)
.unwrap();
.unwrap();

// THEN the response should contain the correct interval
assert_that!(response).is_equal_to(&refresh_interval);
Expand All @@ -90,10 +87,10 @@ fn build_app(
) -> App<
impl ServiceFactory<
ServiceRequest,
Config=(),
Response=ServiceResponse,
Error=Error,
InitError=(),
Config = (),
Response = ServiceResponse,
Error = Error,
InitError = (),
>,
> {
scheduler::init();
Expand Down
46 changes: 8 additions & 38 deletions src/integration_test_resources_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
Expand All @@ -13,6 +12,7 @@ use evmap::{ReadHandle, WriteHandle};
use rand::Rng;

use crate::geo_location::GeoLocation;
use crate::resource_reader::RemoteResource;
use crate::{
kv_store, resource_endpoint, resource_processor, resource_reader, scheduler, ResourceReader,
};
Expand Down Expand Up @@ -46,12 +46,7 @@ async fn test_get_all_resources() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -91,12 +86,7 @@ async fn test_this_week_in_past_resources() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -129,12 +119,7 @@ async fn test_get_random_resources() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -168,12 +153,7 @@ async fn test_get_resource_by_id_and_resolution() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -208,12 +188,7 @@ async fn test_get_resource_metadata_by_id() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -264,12 +239,7 @@ async fn test_get_resource_description_by_id() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -308,7 +278,7 @@ fn build_app(
>,
> {
scheduler::init();
scheduler::fetch_resources( resource_reader.clone(), kv_writer_mutex.clone());
scheduler::fetch_resources(resource_reader.clone(), kv_writer_mutex.clone());
let geo_location_cache = kv_store::new();
App::new()
.app_data(web::Data::new(kv_reader))
Expand Down
18 changes: 3 additions & 15 deletions src/integration_test_weather_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::{env, fs};
Expand All @@ -9,7 +8,6 @@ use assertor::{assert_that, StringAssertion};
use evmap::{ReadHandle, WriteHandle};
use rand::Rng;

use crate::resource_reader::ResourceReader;
use crate::{resource_reader, scheduler, weather_endpoint, ResourceReader};

const TEST_FOLDER_NAME: &str = "integration_test_weather_api";
Expand All @@ -22,12 +20,7 @@ async fn test_get_weather_current() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -57,12 +50,7 @@ async fn test_get_is_weather_enabled() {
let kv_writer_mutex = Arc::new(Mutex::new(kv_writer));
let app_server = test::init_service(build_app(
kv_reader,
resource_reader::new(
&mut ResourceReader {
samba_connection_urls: HashMap::new(),
},
base_test_dir.to_str().unwrap(),
),
resource_reader::new(base_test_dir.to_str().unwrap()),
kv_writer_mutex.clone(),
))
.await;
Expand Down Expand Up @@ -99,7 +87,7 @@ fn build_app(
>,
> {
scheduler::init();
scheduler::fetch_resources( resource_reader.clone(), kv_writer_mutex);
scheduler::fetch_resources(resource_reader.clone(), kv_writer_mutex);
App::new()
.app_data(web::Data::new(kv_reader))
.app_data(web::Data::new(resource_reader))
Expand Down
16 changes: 6 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
extern crate core;


use std::env;
use std::sync::{Arc, Mutex};

use actix_files::Files;
use actix_web::{App, HttpResponse, HttpServer, middleware, web};


use actix_web::{middleware, web, App, HttpResponse, HttpServer};

mod config_endpoint;
mod exif_reader;
Expand Down Expand Up @@ -60,11 +57,10 @@ async fn main() -> std::io::Result<()> {

// Start scheduler to run at midnight
scheduler::init();
let scheduler_handle =
scheduler::schedule_indexer(app_config.clone(), kv_writer_mutex.clone());
let scheduler_handle = scheduler::schedule_indexer(app_config.clone(), kv_writer_mutex.clone());

// Fetch resources for the first time
scheduler::fetch_resources( app_config.clone(), kv_writer_mutex.clone());
scheduler::fetch_resources(app_config.clone(), kv_writer_mutex.clone());

// Initialize geo location cache
let geo_location_cache = kv_store::new();
Expand Down Expand Up @@ -102,9 +98,9 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/api/health").route(web::get().to(HttpResponse::Ok)))
.service(Files::new("/", "./static/").index_file("index.html"))
})
.bind("0.0.0.0:8080")?
.run()
.await;
.bind("0.0.0.0:8080")?
.run()
.await;

// If the http server is terminated

Expand Down
12 changes: 7 additions & 5 deletions src/resource_endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use actix_web::get;
use actix_web::HttpResponse;
use actix_web::web;
use actix_web::HttpResponse;
use evmap::ReadHandle;

use crate::{ResourceReader, image_processor, resource_processor, resource_reader};
use crate::kv_store::KvStore;
use crate::resource_reader::RemoteResource;
use crate::{image_processor, resource_processor, resource_reader, ResourceReader};

#[get("")]
pub async fn list_all_resources(kv_reader: web::Data<ReadHandle<String, String>>) -> HttpResponse {
Expand Down Expand Up @@ -73,7 +73,9 @@ pub async fn get_resource_by_id_and_resolution(
.and_then(|remote_resource: RemoteResource| remote_resource.orientation);

let resource_data = remote_resource
.map(|remote_resource| resource_reader::read_resource_data(app_config.as_ref(), &remote_resource))
.map(|remote_resource| {
resource_reader::read_resource_data(app_config.as_ref(), &remote_resource)
})
.map(|resource_data| {
image_processor::optimize_image(
resource_data,
Expand All @@ -89,8 +91,8 @@ pub async fn get_resource_by_id_and_resolution(
format!("{resource_id}_{display_width}_{display_height}"),
&resource_data,
)
.await
.expect("writing to cache failed");
.await
.expect("writing to cache failed");

HttpResponse::Ok()
.content_type("image/png")
Expand Down
Loading

0 comments on commit 31ecad5

Please sign in to comment.