2023-12-10 20:03:56 +00:00
|
|
|
import { ResponseType } from "axios";
|
|
|
|
|
2023-12-11 01:05:57 +00:00
|
|
|
import { S5Client } from "../client.js";
|
2024-02-13 04:25:48 +00:00
|
|
|
import { CustomClientOptions, optionsToConfig } from "../utils/options.js";
|
2023-12-10 20:03:56 +00:00
|
|
|
import path from "path";
|
2024-02-13 04:25:48 +00:00
|
|
|
import { DEFAULT_UPLOAD_OPTIONS } from "#methods/upload.js";
|
|
|
|
import { getS5DownloadCid, getS5MetadataCid } from "#generated/index.js";
|
2024-02-13 05:01:34 +00:00
|
|
|
import { addUrlQuery } from "#utils/url.js";
|
2023-12-10 20:03:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom download options.
|
|
|
|
*
|
|
|
|
* @property [endpointDownload] - The relative URL path of the portal endpoint to contact.
|
|
|
|
* @property [download=false] - Indicates to `getCidUrl` whether the file should be downloaded (true) or opened in the browser (false). `downloadFile` and `openFile` override this value.
|
|
|
|
* @property [path] - A path to append to the cid, e.g. `dir1/dir2/file`. A Unix-style path is expected. Each path component will be URL-encoded.
|
|
|
|
* @property [range] - The Range request header to set for the download. Not applicable for in-borwser downloads.
|
|
|
|
* @property [responseType] - The response type.
|
|
|
|
* @property [subdomain=false] - Whether to return the final cid in subdomain format.
|
|
|
|
*/
|
2024-02-13 04:25:48 +00:00
|
|
|
export type CustomDownloadOptions = CustomClientOptions & {
|
2023-12-10 20:03:56 +00:00
|
|
|
path?: string;
|
|
|
|
range?: string;
|
|
|
|
responseType?: ResponseType;
|
|
|
|
};
|
|
|
|
|
2024-02-13 04:25:48 +00:00
|
|
|
export type CustomGetMetadataOptions = CustomClientOptions & {};
|
2023-12-10 20:03:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The response for a get metadata request.
|
|
|
|
*
|
|
|
|
* @property metadata - The metadata in JSON format.
|
|
|
|
* @property portalUrl - The URL of the portal.
|
|
|
|
* @property cid - 46-character cid.
|
|
|
|
*/
|
2024-02-13 04:25:48 +00:00
|
|
|
export type MetadataResult = {
|
2023-12-10 20:03:56 +00:00
|
|
|
metadata: Record<string, unknown>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DEFAULT_DOWNLOAD_OPTIONS = {
|
|
|
|
range: undefined,
|
|
|
|
responseType: undefined,
|
2023-12-11 04:33:55 +00:00
|
|
|
subdomain: "",
|
2023-12-10 20:03:56 +00:00
|
|
|
};
|
|
|
|
|
2024-02-13 04:25:48 +00:00
|
|
|
const DEFAULT_GET_METADATA_OPTIONS = {};
|
2023-12-10 20:03:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates a download of the content of the cid within the browser.
|
|
|
|
*
|
|
|
|
* @param this - S5Client
|
|
|
|
* @param cid - 46-character cid, or a valid cid URL. Can be followed by a path. Note that the cid will not be encoded, so if your path might contain special characters, consider using `customOptions.path`.
|
|
|
|
* @param [customOptions] - Additional settings that can optionally be set.
|
|
|
|
* @param [customOptions.endpointDownload="/"] - The relative URL path of the portal endpoint to contact.
|
|
|
|
* @returns - The full URL that was used.
|
|
|
|
* @throws - Will throw if the cid does not contain a cid or if the path option is not a string.
|
|
|
|
*/
|
|
|
|
export async function downloadFile(
|
|
|
|
this: S5Client,
|
|
|
|
cid: string,
|
|
|
|
customOptions?: CustomDownloadOptions,
|
|
|
|
): Promise<string> {
|
2024-02-13 04:25:48 +00:00
|
|
|
const url = await this.getCidUrl(cid, customOptions);
|
2023-12-10 20:03:56 +00:00
|
|
|
|
|
|
|
// Download the url.
|
|
|
|
window.location.assign(url);
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the full URL for the given cid.
|
|
|
|
*
|
|
|
|
* @param this - S5Client
|
|
|
|
* @param cid - Base64 cid, or a valid URL that contains a cid. See `downloadFile`.
|
|
|
|
* @param [customOptions] - Additional settings that can optionally be set.
|
|
|
|
* @param [customOptions.endpointDownload="/"] - The relative URL path of the portal endpoint to contact.
|
|
|
|
* @returns - The full URL for the cid.
|
|
|
|
* @throws - Will throw if the cid does not contain a cid or if the path option is not a string.
|
|
|
|
*/
|
|
|
|
export async function getCidUrl(
|
|
|
|
this: S5Client,
|
|
|
|
cid: string,
|
2024-02-13 04:25:48 +00:00
|
|
|
customOptions: CustomDownloadOptions = {},
|
2023-12-10 20:03:56 +00:00
|
|
|
): Promise<string> {
|
2024-02-13 05:01:34 +00:00
|
|
|
const opt = { ...this.customOptions, customOptions };
|
|
|
|
return addUrlQuery(path.join(this.portalUrl, cid), {
|
|
|
|
auth_token: opt.ApiKey,
|
|
|
|
});
|
2023-12-10 20:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets only the metadata for the given cid without the contents.
|
|
|
|
*
|
|
|
|
* @param this - S5Client
|
|
|
|
* @param cid - Base64 cid.
|
|
|
|
* @param [customOptions] - Additional settings that can optionally be set. See `downloadFile` for the full list.
|
|
|
|
* @param [customOptions.endpointGetMetadata="/"] - The relative URL path of the portal endpoint to contact.
|
|
|
|
* @returns - The metadata in JSON format. Empty if no metadata was found.
|
|
|
|
* @throws - Will throw if the cid does not contain a cid .
|
|
|
|
*/
|
|
|
|
export async function getMetadata(
|
|
|
|
this: S5Client,
|
|
|
|
cid: string,
|
2024-02-13 04:25:48 +00:00
|
|
|
customOptions: CustomGetMetadataOptions = {},
|
|
|
|
): Promise<MetadataResult> {
|
|
|
|
const config = optionsToConfig(
|
|
|
|
this,
|
|
|
|
DEFAULT_GET_METADATA_OPTIONS,
|
|
|
|
customOptions,
|
|
|
|
);
|
2023-12-10 20:03:56 +00:00
|
|
|
|
2024-02-13 04:25:48 +00:00
|
|
|
const response = await getS5MetadataCid(cid, config);
|
2023-12-10 20:03:56 +00:00
|
|
|
|
2024-02-13 04:25:48 +00:00
|
|
|
return { metadata: response };
|
2023-12-10 20:03:56 +00:00
|
|
|
}
|
2023-12-11 04:33:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads in-memory data from a S5 cid.
|
|
|
|
* @param this - S5Client
|
2024-02-13 04:25:48 +00:00
|
|
|
* @param cid - 46-character cid, or a valid cid URL.
|
2023-12-11 04:33:55 +00:00
|
|
|
* @param [customOptions] - Additional settings that can optionally be set.
|
|
|
|
* @returns - The data
|
|
|
|
*/
|
|
|
|
export async function downloadData(
|
|
|
|
this: S5Client,
|
|
|
|
cid: string,
|
2024-02-13 04:25:48 +00:00
|
|
|
customOptions: CustomDownloadOptions = {},
|
2023-12-11 04:33:55 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
|
|
|
const opts = {
|
|
|
|
...DEFAULT_DOWNLOAD_OPTIONS,
|
|
|
|
...this.customOptions,
|
|
|
|
...customOptions,
|
|
|
|
download: true,
|
|
|
|
};
|
|
|
|
|
2024-02-13 04:25:48 +00:00
|
|
|
const config = optionsToConfig(this, DEFAULT_UPLOAD_OPTIONS, customOptions);
|
2023-12-11 04:33:55 +00:00
|
|
|
|
2024-02-13 04:25:48 +00:00
|
|
|
return await (
|
|
|
|
await getS5DownloadCid(cid, {
|
|
|
|
...config,
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
})
|
|
|
|
).arrayBuffer();
|
2023-12-11 04:33:55 +00:00
|
|
|
}
|
2024-02-13 05:04:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads a proof for the given cid.
|
|
|
|
* @param this - S5Client
|
|
|
|
* @param cid - 46-character cid, or a valid cid URL.
|
|
|
|
* @param [customOptions] - Additional settings that can optionally be set.
|
|
|
|
* @returns - The data
|
|
|
|
*/
|
|
|
|
export async function downloadProof(
|
|
|
|
this: S5Client,
|
|
|
|
cid: string,
|
|
|
|
customOptions: CustomDownloadOptions = {},
|
|
|
|
): Promise<ArrayBuffer> {
|
|
|
|
return this.downloadData(`${cid}.obao`, customOptions);
|
|
|
|
}
|