@codex-storage/sdk-js
Version:
Codex SDK to interact with the Codex decentralized storage network.
65 lines (63 loc) • 1.85 kB
JavaScript
import { CodexError } from './chunk-MVZZ6JVF.mjs';
// src/data/browser-upload.ts
var BrowserUploadStategy = class {
file;
onProgress;
metadata;
xhr;
constructor(file, onProgress, metadata) {
this.file = file;
this.onProgress = onProgress;
this.metadata = metadata;
}
upload(url, { auth }) {
const xhr = new XMLHttpRequest();
this.xhr = xhr;
return new Promise((resolve) => {
var _a, _b;
xhr.upload.onprogress = (evt) => {
var _a2;
if (evt.lengthComputable) {
(_a2 = this.onProgress) == null ? void 0 : _a2.call(this, evt.loaded, evt.total);
}
};
xhr.open("POST", url, true);
if ((_a = this.metadata) == null ? void 0 : _a.filename) {
xhr.setRequestHeader(
"Content-Disposition",
'attachment; filename="' + this.metadata.filename + '"'
);
}
if (auth == null ? void 0 : auth.basic) {
xhr.setRequestHeader("Authorization", "Basic " + auth.basic);
}
if ((_b = this.metadata) == null ? void 0 : _b.mimetype) {
xhr.setRequestHeader("Content-Type", this.metadata.mimetype);
}
xhr.send(this.file);
xhr.onload = function() {
if (xhr.status != 200) {
resolve({
error: true,
data: new CodexError(xhr.responseText, { code: xhr.status })
});
} else {
resolve({ error: false, data: xhr.response });
}
};
xhr.onerror = function() {
resolve({
error: true,
data: new CodexError("Something went wrong during the file upload.")
});
};
});
}
abort() {
var _a;
(_a = this.xhr) == null ? void 0 : _a.abort();
}
};
export { BrowserUploadStategy };
//# sourceMappingURL=browser.mjs.map
//# sourceMappingURL=browser.mjs.map