feat: add uploadWebapp

This commit is contained in:
Derrick Hammer 2023-12-11 21:53:10 -05:00
parent 4da2719c26
commit 28d7c30225
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 62 additions and 1 deletions

View File

@ -13,6 +13,8 @@ import {
uploadSmallFile, uploadSmallFile,
uploadSmallFileRequest, uploadSmallFileRequest,
uploadLargeFileRequest, uploadLargeFileRequest,
uploadWebapp,
uploadWebappRequest,
} from "./methods/upload.js"; } from "./methods/upload.js";
import { import {
downloadData, downloadData,
@ -124,7 +126,8 @@ export class S5Client {
protected uploadLargeFileRequest = uploadLargeFileRequest; protected uploadLargeFileRequest = uploadLargeFileRequest;
uploadDirectory = uploadDirectory; uploadDirectory = uploadDirectory;
protected uploadDirectoryRequest = uploadDirectoryRequest; protected uploadDirectoryRequest = uploadDirectoryRequest;
protected uploadWebappRequest = uploadWebappRequest;
uploadWebapp = uploadWebapp;
// Download // Download
downloadFile = downloadFile; downloadFile = downloadFile;

View File

@ -401,6 +401,64 @@ export async function uploadDirectoryRequest(
}); });
} }
export async function uploadWebapp(
this: S5Client,
directory: Record<string, File>,
customOptions?: CustomUploadOptions,
): Promise<UploadRequestResponse> {
const response = await this.uploadWebappRequest(directory, customOptions);
return { cid: response.data.cid };
}
/**
* Makes a request to upload a directory to S5-net.
* @param this - S5Client
* @param directory - File objects to upload, indexed by their path strings.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath] - The relative URL path of the portal endpoint to contact.
* @returns - The upload response.
* @throws - Will throw if the input filename is not a string.
*/
export async function uploadWebappRequest(
this: S5Client,
directory: Record<string, File>,
customOptions?: CustomUploadOptions,
): Promise<AxiosResponse> {
const opts = {
...DEFAULT_UPLOAD_OPTIONS,
...this.customOptions,
...customOptions,
};
const formData = new FormData();
Object.entries(directory).forEach(([path, file]) => {
file = ensureFileObjectConsistency(file as File);
formData.append(path, file as File, path);
});
const query: { [key: string]: string | undefined } = { name: "" };
if (opts.tryFiles) {
query.tryfiles = JSON.stringify(opts.tryFiles);
} else {
query.tryfiles = JSON.stringify(["index.html"]);
}
if (opts.errorPages) {
query.errorpages = JSON.stringify(opts.errorPages);
} else {
query.errorpages = JSON.stringify({ 404: "/404.html" });
}
return await this.executeRequest({
...opts,
endpointPath: opts.endpointDirectoryUpload,
method: "post",
data: formData,
query,
});
}
/** /**
* Sometimes file object might have had the type property defined manually with * Sometimes file object might have had the type property defined manually with
* Object.defineProperty and some browsers (namely firefox) can have problems * Object.defineProperty and some browsers (namely firefox) can have problems