Skip to content

Commit

Permalink
api: fix sl1 processing to account for relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ada-phillips committed Apr 28, 2024
1 parent ecfcad3 commit 41948a3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
25 changes: 7 additions & 18 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,13 @@ use crate::{

#[handler]
async fn start_print(
URLPath((location, file_name)): URLPath<(LocationCategory, String)>,
URLPath((location, file_path)): URLPath<(LocationCategory, String)>,
Data(operation_sender): Data<&mpsc::Sender<Operation>>,
Data(configuration): Data<&ApiConfig>,
) -> Result<()> {
let pathbuf = get_file_path(configuration, &file_name, &location)?;

let path = pathbuf
.into_os_string()
.into_string()
.map_err(|_| NotFoundError)?;
let full_file_path = get_file_path(configuration, &file_path, &location)?;

let file_data = FileData {
name: file_name,
path,
last_modified: None,
location_category: location,
};
let file_data = _get_filedata(full_file_path, &location, configuration)?;

operation_sender
.send(Operation::StartPrint { file_data })
Expand Down Expand Up @@ -282,10 +272,7 @@ fn get_file_path(
}

// Since USB paths are specified as a glob, find all and filter to file_name
fn get_usb_file_path(
configuration: &ApiConfig,
file_name: &str,
) -> Result<PathBuf, NotFoundError> {
fn get_usb_file_path(configuration: &ApiConfig, file_name: &str) -> Result<PathBuf, NotFoundError> {
let paths = glob(&configuration.usb_glob).map_err(|_| NotFoundError)?;

let path_buf = paths
Expand Down Expand Up @@ -327,6 +314,7 @@ fn _get_filedata(
.and_then(|modified| modified.duration_since(UNIX_EPOCH).ok())
.map(|dur| dur.as_millis());

// TODO handle USB _get_filedata
Ok(FileData {
path: target_file
.strip_prefix(configuration.upload_path.as_str())
Expand All @@ -347,6 +335,7 @@ fn _get_filedata(
})?,
last_modified: modified_time,
location_category: location.clone(),
parent_path: configuration.upload_path.clone(),
})
}

Expand Down Expand Up @@ -394,7 +383,7 @@ async fn get_file_metadata(

#[handler]
async fn delete_file(
URLPath((_location, _file_name)): URLPath<(LocationCategory, String)>,
URLPath((_location, _file_path)): URLPath<(LocationCategory, String)>,
Data(_configuration): Data<&ApiConfig>,
) -> Result<Json<FileData>> {
Err(NotImplemented(MethodNotAllowedError))
Expand Down
1 change: 1 addition & 0 deletions src/printfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct FileData {
pub name: String,
pub last_modified: Option<u128>,
pub location_category: LocationCategory,
pub parent_path: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
7 changes: 5 additions & 2 deletions src/sl1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::File, io::Read};
use std::{fs::File, io::Read, path::Path};

use async_trait::async_trait;
use config::{Config, ConfigError, File as ConfigFile, FileFormat};
Expand Down Expand Up @@ -70,7 +70,10 @@ impl PrintFile for Sl1 {
/// Instantiate the Sl1 from the given file
fn from_file(file_data: FileData) -> Sl1 {
log::info!("Loading PrintFile from SL1 {:?}", file_data);
let file = File::open(file_data.path.clone()).unwrap();

let full_path = Path::new(file_data.parent_path.as_str()).join(file_data.path.as_str());

let file = File::open(full_path).unwrap();

let mut archive = ZipArchive::new(file).unwrap();

Expand Down

0 comments on commit 41948a3

Please sign in to comment.