From ab686aa2acd13c47ea87abe813c6cf5b1a6a9bce Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Fri, 13 Jan 2023 16:24:47 +0100 Subject: [PATCH] Fixed an issue where an re-uploaded file would remain in the database and be uploaded again, and where a failed screenshot could be removed from the database while not having been uploaded. --- Cargo.lock | 11 ++++++++++- Cargo.toml | 5 +++-- README.md | 2 ++ src/database.rs | 6 +++--- src/main.rs | 4 ++-- src/uploaders/noop.rs | 10 ++++++++-- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f55be1c..792908b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,15 @@ version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -1335,8 +1344,8 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c53a5ade47760e8cc4986bdc5e72daeffaaaee64cbc374f9cfe0a00c1cd87b1f" dependencies = [ + "bincode", "serde", - "serde_json", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 68afc75..75bf6fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ log = "*" notify = "5.0.0" oauth2 = "4.3.0" onedrive-api = { version = "0.9.0", default-features = false } -pickledb = "0.5.1" +pickledb = { version = "0.5.1", default-features = false, features = ["bincode"] } reqwest = { version = "0.11.13", default-features = false, features = ["rustls-tls"] } rust-s3 = {version = "0.32.3", default-features = false, features = ["tokio-rustls-tls"] } serde = "^1.0" @@ -35,5 +35,6 @@ serde_json = "^1.0" serde_yaml = "0.9.16" serenity = { version = "0.11", default-features = false, features = ["builder", "client", "gateway", "http", "model", "rustls_backend"] } tokio = { version = "^1.23", features = ["macros", "rt-multi-thread", "sync", "io-util"] } - rand = "0.8.5" + +[dev-dependencies] diff --git a/README.md b/README.md index 052c97f..3ed2f14 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ The Google Drive integration requires a service account created from the [Google To do so, note the service account email address (`xxx@yyy@iam.gserviceaccount.com`), and share the folder with that email address, with write permissions. Also note the folder ID from the URL, you will need to indicate it in the configuration. +Finally, you will need to enable the [Google Drive API](https://console.cloud.google.com/marketplace/product/google/drive.googleapis.com). + ```yaml uploader: kind: GoogleDrive diff --git a/src/database.rs b/src/database.rs index 0890ea0..2a18534 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use anyhow::Context; -use pickledb::PickleDb; +use pickledb::{PickleDb, SerializationMethod}; use tokio::{ fs::{create_dir_all, File}, io::{AsyncReadExt, AsyncWriteExt}, @@ -26,14 +26,14 @@ pub fn init_db(config: &Config) -> Result { } fn create_db(config: &Config) -> PickleDb { - PickleDb::new(config.deckshot_path.join("deckshot.db"), pickledb::PickleDbDumpPolicy::AutoDump, pickledb::SerializationMethod::Json) + PickleDb::new(config.deckshot_path.join("deckshot.db"), pickledb::PickleDbDumpPolicy::AutoDump, SerializationMethod::Bin) } fn load_db(config: &Config) -> Result { Ok(PickleDb::load( config.deckshot_path.join("deckshot.db"), pickledb::PickleDbDumpPolicy::AutoDump, - pickledb::SerializationMethod::Json, + SerializationMethod::Bin, )?) } diff --git a/src/main.rs b/src/main.rs index ab48e9c..edf632a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,7 @@ async fn main() -> Result<(), anyhow::Error> { } } - for (index, path) in paths.iter().enumerate() { + for path in paths.iter() { let screenshot: GameScreenshot = PathBuf::from(path).into(); match screenshot.upload(&**uploader, db.clone()).await { @@ -104,7 +104,7 @@ async fn main() -> Result<(), anyhow::Error> { } } - db.lock().await.lpop::("screenshots", index); + let _ = db.lock().await.lrem_value("screenshots", path); } tokio::time::sleep(Duration::from_secs(config.retrier_interval)).await; diff --git a/src/uploaders/noop.rs b/src/uploaders/noop.rs index d3bab66..c755409 100644 --- a/src/uploaders/noop.rs +++ b/src/uploaders/noop.rs @@ -1,3 +1,6 @@ +use anyhow::anyhow; +use rand::{thread_rng, Rng}; + use crate::{GameScreenshot, Uploader}; #[derive(Clone)] @@ -15,7 +18,10 @@ impl Uploader for NoopUploader { "noop" } - async fn upload<'a>(&'a self, _screenshot: &'a GameScreenshot) -> Result<&'a GameScreenshot, anyhow::Error> { - unimplemented!(); + async fn upload<'a>(&'a self, screenshot: &'a GameScreenshot) -> Result<&'a GameScreenshot, anyhow::Error> { + match thread_rng().gen::() { + true => Ok(screenshot), + false => Err(anyhow!("upload failed!")), + } } }