diff --git a/file-service/src/store/filehandle.rs b/file-service/src/store/filehandle.rs index e03974f..c516411 100644 --- a/file-service/src/store/filehandle.rs +++ b/file-service/src/store/filehandle.rs @@ -120,8 +120,13 @@ impl FileHandle { /// Create a new entry in the database pub fn new(filename: String, root: PathBuf) -> Result { let id = FileId::from(Uuid::new_v4().hyphenated().to_string()); + let path = PathBuf::from(filename); - let extension = PathBuf::from(filename) + let name = path + .file_stem() + .and_then(|s| s.to_str().map(|s| s.to_owned())) + .ok_or(WriteFileError::InvalidPath)?; + let extension = path .extension() .and_then(|s| s.to_str().map(|s| s.to_owned())) .ok_or(WriteFileError::InvalidPath)?; @@ -138,6 +143,7 @@ impl FileHandle { let info = FileInfo { id: id.clone(), + name, size: 0, created: Utc::now(), file_type, @@ -233,6 +239,17 @@ mod test { ); } + #[test] + fn it_creates_file_info() { + let tmp = TempDir::new("var").unwrap(); + let handle = + FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed"); + assert_eq!(handle.info.name, Some("rawr".to_owned())); + assert_eq!(handle.info.size, 0); + assert_eq!(handle.info.file_type, "image/png"); + assert_eq!(handle.info.extension, "png"); + } + #[test] fn it_opens_a_file() { let tmp = TempDir::new("var").unwrap(); diff --git a/file-service/src/store/fileinfo.rs b/file-service/src/store/fileinfo.rs index 0574550..bcfbc4e 100644 --- a/file-service/src/store/fileinfo.rs +++ b/file-service/src/store/fileinfo.rs @@ -11,6 +11,12 @@ use std::{ #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FileInfo { pub id: FileId, + + // Early versions of the application didn't support a name field, so it is possible that + // metadata won't contain the name. We can just default to an empty string when loading the + // metadata, as all future versions will require a filename when the file gets uploaded. + #[serde(default)] + pub name: String, pub size: usize, pub created: DateTime, pub file_type: String, @@ -50,6 +56,7 @@ mod test { let info = FileInfo { id: FileId("temp-id".to_owned()), + name: "test-image".to_owned(), size: 23777, created, file_type: "image/png".to_owned(),