Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/dropbox): read support range #2848

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/src/services/dropbox/backend.rs
Expand Up @@ -63,6 +63,7 @@ impl Accessor for DropboxBackend {
stat: true,

read: true,
read_with_range: true,

write: true,

Expand Down Expand Up @@ -93,11 +94,11 @@ impl Accessor for DropboxBackend {
}
}

async fn read(&self, path: &str, _args: OpRead) -> Result<(RpRead, Self::Reader)> {
let resp = self.core.dropbox_get(path).await?;
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
let resp = self.core.dropbox_get(path, args).await?;
let status = resp.status();
match status {
StatusCode::OK => {
StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
let meta = parse_into_metadata(path, resp.headers())?;
Ok((RpRead::with_metadata(meta), resp.into_body()))
}
Expand Down
34 changes: 19 additions & 15 deletions core/src/services/dropbox/core.rs
Expand Up @@ -33,16 +33,7 @@ use serde::Deserialize;
use serde::Serialize;
use tokio::sync::Mutex;

use crate::raw::build_rooted_abs_path;
use crate::raw::new_json_deserialize_error;
use crate::raw::new_json_serialize_error;
use crate::raw::new_request_build_error;
use crate::raw::AsyncBody;
use crate::raw::BatchedReply;
use crate::raw::HttpClient;
use crate::raw::IncomingAsyncBody;
use crate::raw::RpBatch;
use crate::raw::RpDelete;
use crate::raw::*;
use crate::services::dropbox::backend::DropboxDeleteBatchResponse;
use crate::services::dropbox::backend::DropboxDeleteBatchResponseEntry;
use crate::services::dropbox::error::parse_error;
Expand All @@ -58,8 +49,9 @@ pub struct DropboxCore {

impl Debug for DropboxCore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut de = f.debug_struct("DropboxCore");
de.finish()
f.debug_struct("DropboxCore")
.field("root", &self.root)
.finish()
}
}

Expand All @@ -71,16 +63,28 @@ impl DropboxCore {
path.trim_end_matches('/').to_string()
}

pub async fn dropbox_get(&self, path: &str) -> Result<Response<IncomingAsyncBody>> {
pub async fn dropbox_get(
&self,
path: &str,
args: OpRead,
) -> Result<Response<IncomingAsyncBody>> {
let url: String = "https://content.dropboxapi.com/2/files/download".to_string();
let download_args = DropboxDownloadArgs {
path: build_rooted_abs_path(&self.root, path),
};
let request_payload =
serde_json::to_string(&download_args).map_err(new_json_serialize_error)?;
let mut request = Request::post(&url)

let mut req = Request::post(&url)
.header("Dropbox-API-Arg", request_payload)
.header(CONTENT_LENGTH, 0)
.header(CONTENT_LENGTH, 0);

let range = args.range();
if !range.is_full() {
req = req.header(header::RANGE, range.to_header());
}

let mut request = req
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;

Expand Down