Use match to avoid ownership issues on the TempFile / file_path variables in closures.

This commit is contained in:
Yip Rui Fung 2022-07-09 10:33:27 +08:00
parent 5c38b2c4eb
commit 31595888ea
2 changed files with 22 additions and 19 deletions

View file

@ -1,23 +1,24 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use chrono::{NaiveDateTime, Utc}; use chrono::{NaiveDateTime, Utc};
use rocket::fs::TempFile; use futures::{stream, stream::StreamExt};
use rocket::serde::json::Json;
use rocket::{ use rocket::{
form::{Form, FromForm}, form::{Form, FromForm},
Route, Route,
}; };
use rocket::fs::TempFile;
use rocket::serde::json::Json;
use serde_json::Value; use serde_json::Value;
use crate::{ use crate::{
api::{self, EmptyResult, JsonResult, JsonUpcase, Notify, PasswordData, UpdateType}, api::{self, EmptyResult, JsonResult, JsonUpcase, Notify, PasswordData, UpdateType},
auth::Headers, auth::Headers,
crypto,
db::{models::*, DbConn, DbPool},
CONFIG, CONFIG,
crypto,
db::{DbConn, DbPool, models::*},
}; };
use futures::{stream, stream::StreamExt, TryFutureExt}; use super::folders::FolderData;
pub fn routes() -> Vec<Route> { pub fn routes() -> Vec<Route> {
// Note that many routes have an `admin` variant; this seems to be // Note that many routes have an `admin` variant; this seems to be
@ -212,7 +213,8 @@ pub struct CipherData {
Card = 3, Card = 3,
Identity = 4 Identity = 4
*/ */
pub Type: i32, // TODO: Change this to NumberOrString pub Type: i32,
// TODO: Change this to NumberOrString
pub Name: String, pub Name: String,
Notes: Option<String>, Notes: Option<String>,
Fields: Option<Value>, Fields: Option<Value>,
@ -230,7 +232,8 @@ pub struct CipherData {
// These are used during key rotation // These are used during key rotation
#[serde(rename = "Attachments")] #[serde(rename = "Attachments")]
_Attachments: Option<Value>, // Unused, contains map of {id: filename} _Attachments: Option<Value>,
// Unused, contains map of {id: filename}
Attachments2: Option<HashMap<String, Attachments2Data>>, Attachments2: Option<HashMap<String, Attachments2Data>>,
// The revision datetime (in ISO 8601 format) of the client's local copy // 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(()) Ok(())
} }
use super::folders::FolderData;
#[derive(Deserialize)] #[derive(Deserialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
struct ImportData { struct ImportData {
@ -804,7 +805,7 @@ async fn share_cipher_by_uuid(
nt, nt,
UpdateType::CipherUpdate, UpdateType::CipherUpdate,
) )
.await?; .await?;
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, conn).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"); attachment.save(&conn).await.expect("Error saving attachment");
} }
data.data.persist_to(&file_path) match data.data.persist_to(&file_path).await {
.unwrap_or_else(data.data.move_copy_to(&file_path)) Ok(_result) => {}
.await?; Err(_error) => data.data.move_copy_to(&file_path).await?
}
nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(&conn).await).await; nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(&conn).await).await;

View file

@ -1,7 +1,6 @@
use std::path::Path; use std::path::Path;
use chrono::{DateTime, Duration, Utc}; use chrono::{DateTime, Duration, Utc};
use futures::TryFutureExt;
use rocket::form::Form; use rocket::form::Form;
use rocket::fs::NamedFile; use rocket::fs::NamedFile;
use rocket::fs::TempFile; use rocket::fs::TempFile;
@ -11,9 +10,9 @@ use serde_json::Value;
use crate::{ use crate::{
api::{ApiResult, EmptyResult, JsonResult, JsonUpcase, Notify, NumberOrString, UpdateType}, api::{ApiResult, EmptyResult, JsonResult, JsonUpcase, Notify, NumberOrString, UpdateType},
auth::{ClientIp, Headers, Host}, auth::{ClientIp, Headers, Host},
db::{models::*, DbConn, DbPool},
util::SafeString,
CONFIG, CONFIG,
db::{DbConn, DbPool, models::*},
util::SafeString,
}; };
const SEND_INACCESSIBLE_MSG: &str = "Send does not exist or is no longer available"; 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<UploadData<'_>>, headers: Headers, conn: DbCo
let file_path = folder_path.join(&file_id); let file_path = folder_path.join(&file_id);
tokio::fs::create_dir_all(&folder_path).await?; tokio::fs::create_dir_all(&folder_path).await?;
data.persist_to(&file_path)
.unwrap_or_else(data.move_copy_to(&file_path)) match data.persist_to(&file_path).await {
.await?; Ok(_result) => {}
Err(_error) => data.move_copy_to(&file_path).await?
}
let mut data_value: Value = serde_json::from_str(&send.data)?; let mut data_value: Value = serde_json::from_str(&send.data)?;
if let Some(o) = data_value.as_object_mut() { if let Some(o) = data_value.as_object_mut() {