async function sendFiles(url, files, options = {}) {
url = url.replace(/\/$/gim, "") + "/";
const formData = new FormData();
const paths = {};
let idx = 0;
for await (const file of files) {
let content = file.content;
content = toBlob(typeof content === "function" ? await content() : content);
const fileKey = `file-${idx++}`;
paths[fileKey] = file.path;
const f = new File([content], fileKey);
formData.append("files", f);
}
formData.append("paths", JSON.stringify(paths));
const headers = new Headers();
if (options && options.username) {
headers.set(
"Authorization",
"Basic " + btoa(options.username + ":" + (options.password || ""))
);
}
const response = await fetch(url, {
method: "POST",
body: formData,
headers
});
const result = await response.json();
const map = new Map();
if (result.files) {
const list = result.files;
for (let i = 0; i < list.length; i++) {
const filePath = list[i];
const fileUrl = joinUrlSegments(url, filePath);
map.set(filePath, fileUrl);
}
}
return map;
}