-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Extend dropbox provider to support teams folders #5638
Open
vikas-rajvanshy
wants to merge
2
commits into
transloadit:main
Choose a base branch
from
vikas-rajvanshy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Diff output filesdiff --git a/packages/@uppy/companion/lib/server/provider/dropbox/index.d.ts b/packages/@uppy/companion/lib/server/provider/dropbox/index.d.ts
index d4d625f..8297b7a 100644
--- a/packages/@uppy/companion/lib/server/provider/dropbox/index.d.ts
+++ b/packages/@uppy/companion/lib/server/provider/dropbox/index.d.ts
@@ -4,6 +4,7 @@ export = DropBox;
*/
declare class DropBox extends Provider {
constructor(options: any);
+ rootNamespaceId: any;
download({ id, token }: {
id: any;
token: any;
diff --git a/packages/@uppy/companion/lib/server/provider/dropbox/index.js b/packages/@uppy/companion/lib/server/provider/dropbox/index.js
index 8c7eb6a..7cf757d 100644
--- a/packages/@uppy/companion/lib/server/provider/dropbox/index.js
+++ b/packages/@uppy/companion/lib/server/provider/dropbox/index.js
@@ -17,19 +17,22 @@ function httpHeaderSafeJson(v) {
return `\\u${(`000${c.charCodeAt(0).toString(16)}`).slice(-4)}`;
});
}
-const getClient = async ({ token }) =>
+const getClient = async ({ token, rootNamespaceId }) =>
(await got).extend({
prefixUrl: "https://api.dropboxapi.com/2",
headers: {
authorization: `Bearer ${token}`,
+ ...(rootNamespaceId && {
+ "Dropbox-API-Path-Root": httpHeaderSafeJson({ ".tag": "root", "root": rootNamespaceId }),
+ }),
},
});
const getOauthClient = async () =>
(await got).extend({
prefixUrl: "https://api.dropboxapi.com/oauth2",
});
-async function list({ directory, query, token }) {
- const client = await getClient({ token });
+async function list({ directory, query, token, rootNamespaceId }) {
+ const client = await getClient({ token, rootNamespaceId });
if (query.cursor) {
return client.post("files/list_folder/continue", { json: { cursor: query.cursor }, responseType: "json" }).json();
}
@@ -45,7 +48,8 @@ async function list({ directory, query, token }) {
}).json();
}
async function userInfo({ token }) {
- return (await getClient({ token })).post("users/get_current_account", { responseType: "json" }).json();
+ return (await getClient({ token, rootNamespaceId: null })).post("users/get_current_account", { responseType: "json" })
+ .json();
}
/**
* Adapter for API https://www.dropbox.com/developers/documentation/http/documentation
@@ -54,6 +58,7 @@ class DropBox extends Provider {
constructor(options) {
super(options);
this.needsCookieAuth = true;
+ this.rootNamespaceId = null;
}
static get oauthProvider() {
return "dropbox";
@@ -66,18 +71,26 @@ class DropBox extends Provider {
*/
async list(options) {
return this.#withErrorHandling("provider.dropbox.list.error", async () => {
- const responses = await Promise.all([
- list(options),
- userInfo(options),
- ]);
- // @ts-ignore
- const [stats, { email }] = responses;
+ const userInfoResponse = await userInfo(options);
+ const { email, root_info } = userInfoResponse;
+ // Store rootNamespaceId as class member
+ this.rootNamespaceId = root_info?.root_namespace_id;
+ // Then call list with the directory path and root namespace
+ const stats = await list({
+ ...options,
+ rootNamespaceId: this.rootNamespaceId,
+ });
return adaptData(stats, email, options.companion.buildURL);
});
}
async download({ id, token }) {
return this.#withErrorHandling("provider.dropbox.download.error", async () => {
- const stream = (await getClient({ token })).stream.post("files/download", {
+ // Fetch rootNamespaceId if not already set
+ if (!this.rootNamespaceId) {
+ const userInfoResponse = await userInfo({ token });
+ this.rootNamespaceId = userInfoResponse.root_info?.root_namespace_id;
+ }
+ const stream = (await getClient({ token, rootNamespaceId: this.rootNamespaceId })).stream.post("files/download", {
prefixUrl: "https://content.dropboxapi.com/2",
headers: {
"Dropbox-API-Arg": httpHeaderSafeJson({ path: String(id) }),
@@ -92,34 +105,54 @@ class DropBox extends Provider {
}
async thumbnail({ id, token }) {
return this.#withErrorHandling("provider.dropbox.thumbnail.error", async () => {
- const stream = (await getClient({ token })).stream.post("files/get_thumbnail_v2", {
- prefixUrl: "https://content.dropboxapi.com/2",
- headers: {
- "Dropbox-API-Arg": httpHeaderSafeJson({
- resource: { ".tag": "path", path: `${id}` },
- size: "w256h256",
- format: "jpeg",
- }),
+ // Fetch rootNamespaceId if not already set
+ if (!this.rootNamespaceId) {
+ const userInfoResponse = await userInfo({ token });
+ this.rootNamespaceId = userInfoResponse.root_info?.root_namespace_id;
+ }
+ const stream = (await getClient({ token, rootNamespaceId: this.rootNamespaceId })).stream.post(
+ "files/get_thumbnail_v2",
+ {
+ prefixUrl: "https://content.dropboxapi.com/2",
+ headers: {
+ "Dropbox-API-Arg": httpHeaderSafeJson({
+ resource: { ".tag": "path", path: `${id}` },
+ size: "w256h256",
+ format: "jpeg",
+ }),
+ },
+ body: Buffer.alloc(0),
+ responseType: "json",
},
- body: Buffer.alloc(0),
- responseType: "json",
- });
+ );
await prepareStream(stream);
return { stream, contentType: "image/jpeg" };
});
}
async size({ id, token }) {
return this.#withErrorHandling("provider.dropbox.size.error", async () => {
- const { size } = await (await getClient({ token })).post("files/get_metadata", {
- json: { path: id },
- responseType: "json",
- }).json();
+ // Fetch rootNamespaceId if not already set
+ if (!this.rootNamespaceId) {
+ const userInfoResponse = await userInfo({ token });
+ this.rootNamespaceId = userInfoResponse.root_info?.root_namespace_id;
+ }
+ const { size } = await (await getClient({ token, rootNamespaceId: this.rootNamespaceId })).post(
+ "files/get_metadata",
+ { json: { path: id }, responseType: "json" },
+ ).json();
return parseInt(size, 10);
});
}
async logout({ token }) {
return this.#withErrorHandling("provider.dropbox.logout.error", async () => {
- await (await getClient({ token })).post("auth/token/revoke", { responseType: "json" });
+ // Fetch rootNamespaceId if not already set
+ if (!this.rootNamespaceId) {
+ const userInfoResponse = await userInfo({ token });
+ this.rootNamespaceId = userInfoResponse.root_info?.root_namespace_id;
+ }
+ await (await getClient({ token, rootNamespaceId: this.rootNamespaceId })).post("auth/token/revoke", {
+ responseType: "json",
+ });
return { revoked: true };
});
} |
Murderlon
reviewed
Feb 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this 👍
const { email, root_info } = userInfoResponse | ||
|
||
// Store rootNamespaceId as class member | ||
this.rootNamespaceId = root_info?.root_namespace_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to double check, do we initialize this class once or per user? Because setting this will only work for the latter.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In cases where the logged in user is part of a team, the current implementation does not show the teams folders. This is because the default home directory is often a child of the team folders. This change sets the active root to be root_namespace_id which will enable the provider to list all folders.
See: https://developers.dropbox.com/dbx-team-files-guide