@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
216 lines • 6.27 kB
JavaScript
import {
StorageError
} from "../index.js";
class DropboxStorage {
constructor(config) {
this.config = config;
if (!config.accessToken) {
throw new StorageError(
"Dropbox access token is required",
"MISSING_ACCESS_TOKEN",
"dropbox"
);
}
this.rootPath = config.rootPath ?? "/Vana Data";
}
config;
apiUrl = "https://api.dropboxapi.com/2";
contentUrl = "https://content.dropboxapi.com/2";
rootPath;
async upload(file, filename) {
try {
const fileName = filename ?? `vana-file-${Date.now()}.dat`;
const path = `${this.rootPath}/${fileName}`;
const response = await fetch(`${this.contentUrl}/files/upload`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.config.accessToken}`,
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": JSON.stringify({
path,
mode: "add",
autorename: true,
mute: false
})
},
body: file
});
if (!response.ok) {
const error = await response.text();
throw new StorageError(
`Failed to upload to Dropbox: ${error}`,
"UPLOAD_FAILED",
"dropbox"
);
}
const result = await response.json();
const sharedLinkUrl = await this.createSharedLink(result.path_lower);
const directDownloadUrl = sharedLinkUrl.replace("www.dropbox.com", "dl.dropboxusercontent.com").replace("?dl=1", "");
return {
url: directDownloadUrl,
size: file.size,
contentType: file.type || "application/octet-stream",
metadata: {
id: result.id,
path: result.path_display
}
};
} catch (error) {
if (error instanceof StorageError) throw error;
throw new StorageError(
`Dropbox upload error: ${error instanceof Error ? error.message : "Unknown error"}`,
"UPLOAD_ERROR",
"dropbox"
);
}
}
async download(url) {
const downloadUrl = url.replace(
"www.dropbox.com",
"dl.dropboxusercontent.com"
);
try {
const response = await fetch(downloadUrl);
if (!response.ok) {
throw new StorageError(
`Failed to download from Dropbox: ${response.statusText}`,
"DOWNLOAD_FAILED",
"dropbox"
);
}
return response.blob();
} catch (error) {
if (error instanceof StorageError) throw error;
throw new StorageError(
`Dropbox download error: ${error instanceof Error ? error.message : "Unknown error"}`,
"DOWNLOAD_ERROR",
"dropbox"
);
}
}
async list(options) {
if (!this.config.accessToken) {
throw new StorageError(
"Access token not provided",
"AUTH_ERROR",
"dropbox"
);
}
try {
const response = await fetch(`${this.apiUrl}/files/list_folder`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.config.accessToken}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
path: this.rootPath,
limit: options?.limit ?? 100,
include_deleted: false
})
});
if (!response.ok) {
throw new StorageError(
`Failed to list Dropbox files: ${await response.text()}`,
"LIST_FAILED",
"dropbox"
);
}
const result = await response.json();
return result.entries.filter((entry) => entry[".tag"] === "file").map((file) => ({
id: file.id,
name: file.name,
url: `dropbox://${file.path_lower}`,
// Placeholder URL
size: file.size,
contentType: "application/octet-stream",
// Dropbox API doesn't provide this in list
createdAt: new Date(file.server_modified)
}));
} catch (error) {
if (error instanceof StorageError) throw error;
throw new StorageError(
`Dropbox list error: ${error instanceof Error ? error.message : "Unknown error"}`,
"LIST_ERROR",
"dropbox"
);
}
}
async delete(url) {
try {
const path = new URL(url).pathname;
const response = await fetch(`${this.apiUrl}/files/delete_v2`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.config.accessToken}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ path })
});
if (!response.ok && response.status !== 404) {
const error = await response.text();
throw new StorageError(
`Failed to delete from Dropbox: ${error}`,
"DELETE_FAILED",
"dropbox"
);
}
return true;
} catch (error) {
if (error instanceof StorageError) throw error;
throw new StorageError(
`Dropbox delete error: ${error instanceof Error ? error.message : "Unknown error"}`,
"DELETE_ERROR",
"dropbox"
);
}
}
getConfig() {
return {
name: "Dropbox",
type: "dropbox",
requiresAuth: true,
features: {
upload: true,
download: true,
list: true,
delete: true
}
};
}
async createSharedLink(path) {
const response = await fetch(
`${this.apiUrl}/sharing/create_shared_link_with_settings`,
{
method: "POST",
headers: {
Authorization: `Bearer ${this.config.accessToken}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
path,
settings: {
requested_visibility: "public"
}
})
}
);
if (!response.ok) {
const errorData = await response.json();
if (response.status === 409 && errorData.error?.shared_link_already_exists) {
return errorData.error.shared_link_already_exists.metadata.url;
}
throw new StorageError(
`Failed to create shared link: ${JSON.stringify(errorData)}`,
"LINK_CREATION_FAILED",
"dropbox"
);
}
const result = await response.json();
return result.url.replace("?dl=0", "?dl=1");
}
}
export {
DropboxStorage
};
//# sourceMappingURL=dropbox.js.map