Add metadata for upload

This commit is contained in:
Arnaud 2024-11-01 18:02:05 +01:00
parent d8f0ddfc32
commit 8a4ee9855f
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
4 changed files with 20 additions and 7 deletions

View File

@ -266,6 +266,7 @@ Upload a file in a streaming manner
- file (File, required)
- onProgress (onProgress: (loaded: number, total: number) => void, optional)
- metadata ({ filename?: string, mimetype?: string }, optional)
- returns [UploadResponse](./src/data/types.ts#L85)
Example:
@ -273,10 +274,13 @@ Example:
```js
// Get file from previous event
const [file] = e.target.files
const metadata = {
filename: file.name,
mimetype: file.type,
}
const upload = data.upload(file, (loaded: number, total: number) => {
// Use loaded and total so update a progress bar for example
});
}, metadata);
await upload.result();
```

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@codex-storage/sdk-js",
"version": "0.0.13",
"version": "0.0.14",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@codex-storage/sdk-js",
"version": "0.0.13",
"version": "0.0.14",
"license": "MIT",
"dependencies": {
"valibot": "^0.32.0"

View File

@ -1,6 +1,6 @@
{
"name": "@codex-storage/sdk-js",
"version": "0.0.13",
"version": "0.0.14",
"description": "Codex SDK to interact with the Codex decentralized storage network.",
"repository": {
"type": "git",

View File

@ -59,7 +59,8 @@ export class CodexData {
*/
upload(
file: Document | XMLHttpRequestBodyInit,
onProgress?: (loaded: number, total: number) => void
onProgress?: (loaded: number, total: number) => void,
metadata: { filename?: string, mimetype?: string } = {},
): UploadResponse {
const url = this.url + Api.config.prefix + "/data";
@ -73,7 +74,15 @@ export class CodexData {
};
xhr.open("POST", url, true);
// xhr.setRequestHeader("Content-Disposition", "attachment; filename=\"" + file.name + "\"")
if (metadata.filename) {
xhr.setRequestHeader("Content-Disposition", "attachment; filename=\"" + metadata.filename + "\"")
}
if (metadata.mimetype) {
xhr.setRequestHeader("Content-Type", metadata.mimetype)
}
xhr.send(file);
xhr.onload = function () {