feat File storage (#3306)

* refactor: file upload

* refactor: support upload plan

* test: add tests
This commit is contained in:
Nathan.fooo
2023-09-01 22:27:29 +08:00
committed by GitHub
parent df8642d446
commit c652c32575
35 changed files with 844 additions and 306 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -1,3 +1,8 @@
use url::Url;
use uuid::Uuid;
use flowy_storage::StorageObject;
use crate::supabase_test::util::{file_storage_service, get_supabase_ci_config};
#[tokio::test]
@ -7,18 +12,49 @@ async fn supabase_get_object_test() {
}
let service = file_storage_service();
let file_name = format!("test-{}.txt", chrono::Utc::now().timestamp());
let file_name = format!("test-{}.txt", Uuid::new_v4());
let object = StorageObject::from_file(&file_name, "tests/test.txt");
// Upload a file
let key = service
.create_object(&file_name, "tests/test.txt")
let url = service
.create_object(object)
.await
.unwrap()
.parse::<Url>()
.unwrap();
assert_eq!(key, format!("data/{}", file_name));
// The url would be something like:
// https://acfrqdbdtbsceyjbxsfc.supabase.co/storage/v1/object/data/test-1693472809.txt
let name = url.path_segments().unwrap().last().unwrap();
assert_eq!(name, &file_name);
// Download the file
let bytes = service.get_object(&file_name).await.unwrap();
assert_eq!(bytes.len(), 248);
let bytes = service.get_object_by_url(url.to_string()).await.unwrap();
let s = String::from_utf8(bytes.to_vec()).unwrap();
assert_eq!(s, "hello world");
}
#[tokio::test]
async fn supabase_upload_image_test() {
if get_supabase_ci_config().is_none() {
return;
}
let service = file_storage_service();
let file_name = format!("image-{}.png", Uuid::new_v4());
let object = StorageObject::from_file(&file_name, "tests/logo.png");
// Upload a file
let url = service
.create_object(object)
.await
.unwrap()
.parse::<Url>()
.unwrap();
// Download object by url
let bytes = service.get_object_by_url(url.to_string()).await.unwrap();
assert_eq!(bytes.len(), 15694);
}
#[tokio::test]
@ -28,17 +64,15 @@ async fn supabase_delete_object_test() {
}
let service = file_storage_service();
let file_name = format!("test-{}.txt", chrono::Utc::now().timestamp());
let _ = service
.create_object(&file_name, "tests/test.txt")
.await
.unwrap();
let file_name = format!("test-{}.txt", Uuid::new_v4());
let object = StorageObject::from_file(&file_name, "tests/test.txt");
let url = service.create_object(object).await.unwrap();
let result = service.get_object(&file_name).await;
let result = service.get_object_by_url(url.clone()).await;
assert!(result.is_ok());
let _ = service.delete_object(&file_name).await;
let _ = service.delete_object_by_url(url.clone()).await;
let result = service.get_object(&file_name).await;
let result = service.get_object_by_url(url.clone()).await;
assert!(result.is_err());
}

View File

@ -7,6 +7,7 @@ use collab_plugins::cloud_storage::RemoteCollabStorage;
use uuid::Uuid;
use flowy_database_deps::cloud::DatabaseCloudService;
use flowy_error::FlowyError;
use flowy_folder_deps::cloud::{Folder, FolderCloudService};
use flowy_server::supabase::api::{
RESTfulPostgresServer, SupabaseCollabStorageImpl, SupabaseDatabaseServiceImpl,
@ -16,8 +17,9 @@ use flowy_server::supabase::define::{USER_DEVICE_ID, USER_EMAIL, USER_UUID};
use flowy_server::supabase::file_storage::core::SupabaseFileStorage;
use flowy_server::{AppFlowyEncryption, EncryptionImpl};
use flowy_server_config::supabase_config::SupabaseConfiguration;
use flowy_storage::core::FileStorageService;
use flowy_storage::{FileStoragePlan, FileStorageService, StorageObject};
use flowy_user_deps::cloud::UserCloudService;
use lib_infra::future::FutureResult;
use crate::setup_log;
@ -59,8 +61,16 @@ pub fn folder_service() -> Arc<dyn FolderCloudService> {
}
pub fn file_storage_service() -> Arc<dyn FileStorageService> {
let encryption_impl: Arc<dyn AppFlowyEncryption> = Arc::new(EncryptionImpl::new(None));
let config = SupabaseConfiguration::from_env().unwrap();
Arc::new(SupabaseFileStorage::new(&config).unwrap())
Arc::new(
SupabaseFileStorage::new(
&config,
Arc::downgrade(&encryption_impl),
Arc::new(TestFileStoragePlan),
)
.unwrap(),
)
}
#[allow(dead_code)]
@ -131,3 +141,21 @@ pub fn third_party_sign_up_param(uuid: String) -> HashMap<String, String> {
params.insert(USER_DEVICE_ID.to_string(), Uuid::new_v4().to_string());
params
}
pub struct TestFileStoragePlan;
impl FileStoragePlan for TestFileStoragePlan {
fn storage_size(&self) -> FutureResult<u64, FlowyError> {
// 1 GB
FutureResult::new(async { Ok(1024 * 1024 * 1024) })
}
fn maximum_file_size(&self) -> FutureResult<u64, FlowyError> {
// 5 MB
FutureResult::new(async { Ok(5 * 1024 * 1024) })
}
fn check_upload_object(&self, _object: &StorageObject) -> FutureResult<(), FlowyError> {
FutureResult::new(async { Ok(()) })
}
}