mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: support uploading image to cloud storage (#4413)
* feat: add object ext * feat: integrate upload image api * feat: support uploading local file to cloud * feat: abstact the CachedNetworkImage as FlowyNetworkImage * ci: fix tauri ci * fix: integration test --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io> Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
@ -18,3 +18,4 @@ flowy-error = { workspace = true, features = ["impl_from_reqwest"] }
|
||||
mime = "0.3.17"
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
fxhash = "0.2.1"
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::path::Path;
|
||||
|
||||
use bytes::Bytes;
|
||||
|
||||
use flowy_error::FlowyError;
|
||||
@ -9,6 +11,7 @@ use tracing::info;
|
||||
pub struct ObjectIdentity {
|
||||
pub workspace_id: String,
|
||||
pub file_id: String,
|
||||
pub ext: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -17,19 +20,33 @@ pub struct ObjectValue {
|
||||
pub mime: Mime,
|
||||
}
|
||||
|
||||
impl ObjectValue {
|
||||
pub async fn from_file(local_file_path: &str) -> Result<Self, FlowyError> {
|
||||
let mut file = tokio::fs::File::open(local_file_path).await?;
|
||||
let mut content = Vec::new();
|
||||
let n = file.read_to_end(&mut content).await?;
|
||||
info!("read {} bytes from file: {}", n, local_file_path);
|
||||
let mime = mime_guess::from_path(local_file_path).first_or_octet_stream();
|
||||
pub async fn object_from_disk(
|
||||
workspace_id: &str,
|
||||
local_file_path: &str,
|
||||
) -> Result<(ObjectIdentity, ObjectValue), FlowyError> {
|
||||
let ext = Path::new(local_file_path)
|
||||
.extension()
|
||||
.and_then(std::ffi::OsStr::to_str)
|
||||
.unwrap_or("")
|
||||
.to_owned();
|
||||
let mut file = tokio::fs::File::open(local_file_path).await?;
|
||||
let mut content = Vec::new();
|
||||
let n = file.read_to_end(&mut content).await?;
|
||||
info!("read {} bytes from file: {}", n, local_file_path);
|
||||
let mime = mime_guess::from_path(local_file_path).first_or_octet_stream();
|
||||
let hash = fxhash::hash(&content);
|
||||
|
||||
Ok(ObjectValue {
|
||||
Ok((
|
||||
ObjectIdentity {
|
||||
workspace_id: workspace_id.to_owned(),
|
||||
file_id: hash.to_string(),
|
||||
ext,
|
||||
},
|
||||
ObjectValue {
|
||||
raw: content.into(),
|
||||
mime,
|
||||
})
|
||||
}
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
/// Provides a service for object storage.
|
||||
|
Reference in New Issue
Block a user