Add filenames to FileInfo and then set those filenames when creating the file
This commit is contained in:
parent
12df1f4b9b
commit
e96b8087e2
|
@ -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();
|
||||||
|
|
|
@ -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(),
|
||||||
|
|
Loading…
Reference in New Issue