Skip to content

Commit

Permalink
chore: docs
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Oct 9, 2024
1 parent 92e5320 commit 135ef2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
27 changes: 27 additions & 0 deletions libs/client-api/src/native/http_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ impl Client {
AppResponse::<()>::from_response(resp).await?.into_error()
}

/// Sends a POST request to import a file to the server.
///
/// This function streams the contents of a file located at the provided `file_path`
/// as part of a multipart form data request to the server's `/api/import` endpoint.
///
/// ### HTTP Request Details:
///
/// - **Method:** POST
/// - **URL:** `{base_url}/api/import`
/// - The `base_url` is dynamically provided and appended with `/api/import`.
///
/// - **Headers:**
/// - `X-Host`: The value of the `base_url` is sent as the host header.
/// - `X-Content-Length`: The size of the file, in bytes, is provided from the file's metadata.
///
/// - **Multipart Form:**
/// - The file is sent as a multipart form part:
/// - **Field Name:** The file name derived from the file path or a UUID if unavailable.
/// - **File Content:** The file's content is streamed using `reqwest::Body::wrap_stream`.
/// - **MIME Type:** Guessed from the file's extension using the `mime_guess` crate,
/// defaulting to `application/octet-stream` if undetermined.
///
/// ### Parameters:
/// - `file_path`: The path to the file to be uploaded.
/// - The file is opened asynchronously and its metadata (like size) is extracted.
/// - The MIME type is automatically determined based on the file extension using `mime_guess`.
///
pub async fn import_file(&self, file_path: &Path) -> Result<(), AppResponseError> {
let file = File::open(&file_path).await?;
let metadata = file.metadata().await?;
Expand Down
10 changes: 5 additions & 5 deletions services/appflowy-worker/src/import_worker/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async fn download_and_unzip_file(
let reader = BufReader::with_capacity(buffer_size, stream);
let zip_reader = ZipFileReader::new(reader);

let unique_file_name = uuid::Uuid::new_v4().to_string();
let unique_file_name = Uuid::new_v4().to_string();
let output_file_path = temp_dir().join(unique_file_name);
fs::create_dir_all(&output_file_path)
.await
Expand All @@ -325,14 +325,14 @@ fn buffer_size_from_content_length(content_length: Option<i64>) -> usize {
match content_length {
Some(file_size) => {
if file_size < 10 * 1024 * 1024 {
1024 * 1024 // 1MB buffer
3 * 1024 * 1024
} else if file_size < 100 * 1024 * 1024 {
5 * 1024 * 1024 // 2MB buffer
5 * 1024 * 1024 // 5MB buffer
} else {
10 * 1024 * 1024 // 5MB buffer
10 * 1024 * 1024 // 10MB buffer
}
},
None => 1024 * 1024,
None => 3 * 1024 * 1024,
}
}

Expand Down

0 comments on commit 135ef2d

Please sign in to comment.