diff options
author | Vika <vika@fireburn.ru> | 2022-07-19 06:16:27 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2022-07-19 06:16:27 +0300 |
commit | 164dbe76c6842245c34ea67b084b2a99aa056b85 (patch) | |
tree | aa7e286bcb3fd187a51758dd6fed6660d4d1a99e /kittybox-rs | |
parent | 89ed681225e8287cb7ca8d5e92b8ec62c1dd990f (diff) | |
download | kittybox-164dbe76c6842245c34ea67b084b2a99aa056b85.tar.zst |
media: fix small files not being saved to disk properly
It turns out that BufWriter requires calling `flush()` manually and doesn't do it on `drop()`. I forgot about that.
Diffstat (limited to 'kittybox-rs')
-rw-r--r-- | kittybox-rs/Cargo.lock | 24 | ||||
-rw-r--r-- | kittybox-rs/Cargo.toml | 1 | ||||
-rw-r--r-- | kittybox-rs/src/media/storage/file.rs | 32 |
3 files changed, 46 insertions, 11 deletions
diff --git a/kittybox-rs/Cargo.lock b/kittybox-rs/Cargo.lock index 716b10d..e325b54 100644 --- a/kittybox-rs/Cargo.lock +++ b/kittybox-rs/Cargo.lock @@ -1536,6 +1536,7 @@ dependencies = [ "tracing", "tracing-log", "tracing-subscriber", + "tracing-test", "tracing-tree", "url", ] @@ -3561,6 +3562,29 @@ dependencies = [ ] [[package]] +name = "tracing-test" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6992d8a98f570be1c729fe8b6f464fb18c4117054c10f1f952c22d533b48a74" +dependencies = [ + "lazy_static", + "tracing-core", + "tracing-subscriber", + "tracing-test-macro", +] + +[[package]] +name = "tracing-test-macro" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719fa6c65bada6a7a3b8466702ec6fb4c4189b69339f78c9e597f796e493712e" +dependencies = [ + "lazy_static", + "quote 1.0.18", + "syn 1.0.93", +] + +[[package]] name = "tracing-tree" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/kittybox-rs/Cargo.toml b/kittybox-rs/Cargo.toml index 556a85b..833785c 100644 --- a/kittybox-rs/Cargo.toml +++ b/kittybox-rs/Cargo.toml @@ -49,6 +49,7 @@ test-logger = "^0.1.0" # Simple helper to initialize env_logger before uni httpmock = "^0.6" # HTTP mocking library that allows you to simulate responses from HTTP based services faker_rand = "^0.1.1" # Seedable, rand-compatible generators of fake data rand = "^0.8.5" # Utilities for random number generation +tracing-test = "^0.2.2" [dependencies] async-trait = "^0.1.50" # Type erasure for async trait methods diff --git a/kittybox-rs/src/media/storage/file.rs b/kittybox-rs/src/media/storage/file.rs index 84edb84..b46cdb2 100644 --- a/kittybox-rs/src/media/storage/file.rs +++ b/kittybox-rs/src/media/storage/file.rs @@ -113,7 +113,8 @@ impl MediaStore for FileStore { } hasher = _hasher; } - + tempfile.flush().await?; + tempfile.into_inner().sync_all().await?; let hash = hasher.finalize(); debug!("Pending upload hash: {}", hex::encode(&hash)); let filename = format!( @@ -193,39 +194,48 @@ mod tests { use tokio::io::AsyncReadExt; #[tokio::test] + #[tracing_test::traced_test] async fn test_streaming_read_write() { let tempdir = tempdir::TempDir::new("file").expect("Failed to create tempdir"); let store = FileStore::new(tempdir.path()); + let tempdir_path = tempdir.into_path(); + - let file: &[u8] = include_bytes!("../../../../README.md"); + let file: &[u8] = include_bytes!("../../../companion-lite/style.css"); let stream = tokio_stream::iter(file.chunks(100).map(|i| Ok(bytes::Bytes::copy_from_slice(i)))); let metadata = Metadata { - filename: Some("README.md".to_string()), - content_type: "text/markdown".to_string(), + filename: Some("style.css".to_string()), + content_type: "text/css".to_string(), length: None }; + // write through the interface let filename = store.write_streaming( "fireburn.ru", metadata, stream ).await.unwrap(); - + println!("{}, {}", filename, tempdir_path + .join("fireburn.ru") + .join(&filename) + .display()); let content = tokio::fs::read( - tempdir.path() + tempdir_path .join("fireburn.ru") .join(&filename) ).await.unwrap(); assert_eq!(content, file); + // check internal metadata format let meta: Metadata = serde_json::from_slice(&tokio::fs::read( - tempdir.path() + tempdir_path .join("fireburn.ru") .join(filename.clone() + ".json") ).await.unwrap()).unwrap(); - assert_eq!(&meta.content_type, "text/markdown"); - assert_eq!(meta.filename.as_deref(), Some("README.md")); + assert_eq!(&meta.content_type, "text/css"); + assert_eq!(meta.filename.as_deref(), Some("style.css")); assert_eq!(meta.length, Some(file.len())); + // read back the data using the interface let (metadata, read_back) = { let (metadata, stream) = store.read_streaming( "fireburn.ru", @@ -240,8 +250,8 @@ mod tests { }; assert_eq!(read_back, file); - assert_eq!(&metadata.content_type, "text/markdown"); - assert_eq!(meta.filename.as_deref(), Some("README.md")); + assert_eq!(&metadata.content_type, "text/css"); + assert_eq!(meta.filename.as_deref(), Some("style.css")); assert_eq!(meta.length, Some(file.len())); } |