Render the name and the uploaded date for each file in the gallery #80

Merged
savanni merged 4 commits from file-service/show-file-metadata into main 2023-10-25 14:36:00 +00:00
2 changed files with 25 additions and 1 deletions
Showing only changes of commit e96b8087e2 - Show all commits

View File

@ -120,8 +120,13 @@ impl FileHandle {
/// Create a new entry in the database /// Create a new entry in the database
pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> { pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> {
let id = FileId::from(Uuid::new_v4().hyphenated().to_string()); 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() .extension()
.and_then(|s| s.to_str().map(|s| s.to_owned())) .and_then(|s| s.to_str().map(|s| s.to_owned()))
.ok_or(WriteFileError::InvalidPath)?; .ok_or(WriteFileError::InvalidPath)?;
@ -138,6 +143,7 @@ impl FileHandle {
let info = FileInfo { let info = FileInfo {
id: id.clone(), id: id.clone(),
name,
size: 0, size: 0,
created: Utc::now(), created: Utc::now(),
file_type, 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] #[test]
fn it_opens_a_file() { fn it_opens_a_file() {
let tmp = TempDir::new("var").unwrap(); let tmp = TempDir::new("var").unwrap();

View File

@ -11,6 +11,12 @@ use std::{
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FileInfo { pub struct FileInfo {
pub id: FileId, 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 size: usize,
pub created: DateTime<Utc>, pub created: DateTime<Utc>,
pub file_type: String, pub file_type: String,
@ -50,6 +56,7 @@ mod test {
let info = FileInfo { let info = FileInfo {
id: FileId("temp-id".to_owned()), id: FileId("temp-id".to_owned()),
name: "test-image".to_owned(),
size: 23777, size: 23777,
created, created,
file_type: "image/png".to_owned(), file_type: "image/png".to_owned(),