From 31595888ea0ae46eb93e49fc6876bbad368ab9d1 Mon Sep 17 00:00:00 2001 From: Yip Rui Fung Date: Sat, 9 Jul 2022 10:33:27 +0800 Subject: [PATCH] Use match to avoid ownership issues on the TempFile / file_path variables in closures. --- src/api/core/ciphers.rs | 28 +++++++++++++++------------- src/api/core/sends.rs | 13 +++++++------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index 0da7a0c2..18334ea6 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -1,23 +1,24 @@ use std::collections::{HashMap, HashSet}; use chrono::{NaiveDateTime, Utc}; -use rocket::fs::TempFile; -use rocket::serde::json::Json; +use futures::{stream, stream::StreamExt}; use rocket::{ form::{Form, FromForm}, Route, }; +use rocket::fs::TempFile; +use rocket::serde::json::Json; use serde_json::Value; use crate::{ api::{self, EmptyResult, JsonResult, JsonUpcase, Notify, PasswordData, UpdateType}, auth::Headers, - crypto, - db::{models::*, DbConn, DbPool}, CONFIG, + crypto, + db::{DbConn, DbPool, models::*}, }; -use futures::{stream, stream::StreamExt, TryFutureExt}; +use super::folders::FolderData; pub fn routes() -> Vec { // Note that many routes have an `admin` variant; this seems to be @@ -212,7 +213,8 @@ pub struct CipherData { Card = 3, Identity = 4 */ - pub Type: i32, // TODO: Change this to NumberOrString + pub Type: i32, + // TODO: Change this to NumberOrString pub Name: String, Notes: Option, Fields: Option, @@ -230,7 +232,8 @@ pub struct CipherData { // These are used during key rotation #[serde(rename = "Attachments")] - _Attachments: Option, // Unused, contains map of {id: filename} + _Attachments: Option, + // Unused, contains map of {id: filename} Attachments2: Option>, // The revision datetime (in ISO 8601 format) of the client's local copy @@ -470,8 +473,6 @@ pub async fn update_cipher_from_data( Ok(()) } -use super::folders::FolderData; - #[derive(Deserialize)] #[allow(non_snake_case)] struct ImportData { @@ -804,7 +805,7 @@ async fn share_cipher_by_uuid( nt, UpdateType::CipherUpdate, ) - .await?; + .await?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, conn).await)) } @@ -998,9 +999,10 @@ async fn save_attachment( attachment.save(&conn).await.expect("Error saving attachment"); } - data.data.persist_to(&file_path) - .unwrap_or_else(data.data.move_copy_to(&file_path)) - .await?; + match data.data.persist_to(&file_path).await { + Ok(_result) => {} + Err(_error) => data.data.move_copy_to(&file_path).await? + } nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(&conn).await).await; diff --git a/src/api/core/sends.rs b/src/api/core/sends.rs index 90b75b55..8b8ccfb1 100644 --- a/src/api/core/sends.rs +++ b/src/api/core/sends.rs @@ -1,7 +1,6 @@ use std::path::Path; use chrono::{DateTime, Duration, Utc}; -use futures::TryFutureExt; use rocket::form::Form; use rocket::fs::NamedFile; use rocket::fs::TempFile; @@ -11,9 +10,9 @@ use serde_json::Value; use crate::{ api::{ApiResult, EmptyResult, JsonResult, JsonUpcase, Notify, NumberOrString, UpdateType}, auth::{ClientIp, Headers, Host}, - db::{models::*, DbConn, DbPool}, - util::SafeString, CONFIG, + db::{DbConn, DbPool, models::*}, + util::SafeString, }; const SEND_INACCESSIBLE_MSG: &str = "Send does not exist or is no longer available"; @@ -227,9 +226,11 @@ async fn post_send_file(data: Form>, headers: Headers, conn: DbCo let file_path = folder_path.join(&file_id); tokio::fs::create_dir_all(&folder_path).await?; - data.persist_to(&file_path) - .unwrap_or_else(data.move_copy_to(&file_path)) - .await?; + + match data.persist_to(&file_path).await { + Ok(_result) => {} + Err(_error) => data.move_copy_to(&file_path).await? + } let mut data_value: Value = serde_json::from_str(&send.data)?; if let Some(o) = data_value.as_object_mut() {