mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-18 19:05:03 +00:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import Service from '@ember/service';
|
|
import timestamp from 'core/utils/timestamp';
|
|
|
|
interface Extensions {
|
|
csv: string;
|
|
hcl: string;
|
|
sentinel: string;
|
|
json: string;
|
|
pem: string;
|
|
txt: string;
|
|
}
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
|
const EXTENSION_TO_MIME: Extensions = {
|
|
csv: 'txt/csv',
|
|
hcl: 'text/plain',
|
|
sentinel: 'text/plain',
|
|
json: 'application/json',
|
|
pem: 'application/x-pem-file',
|
|
txt: 'text/plain',
|
|
};
|
|
|
|
export default class DownloadService extends Service {
|
|
download(filename: string, content: string, extension: string) {
|
|
// replace spaces with hyphens, append extension to filename
|
|
const formattedFilename =
|
|
`${filename?.replace(/\s+/g, '-')}.${extension}` ||
|
|
`vault-data-${timestamp.now().toISOString()}.${extension}`;
|
|
|
|
// map extension to MIME type or use default
|
|
const mimetype = EXTENSION_TO_MIME[extension as keyof Extensions] || 'text/plain';
|
|
|
|
// commence download
|
|
const { document, URL } = window;
|
|
const downloadElement = document.createElement('a');
|
|
const data = new File([content], formattedFilename, { type: mimetype });
|
|
downloadElement.download = formattedFilename;
|
|
downloadElement.href = URL.createObjectURL(data);
|
|
document.body.appendChild(downloadElement);
|
|
downloadElement.click();
|
|
URL.revokeObjectURL(downloadElement.href);
|
|
downloadElement.remove();
|
|
return formattedFilename;
|
|
}
|
|
|
|
// SAMPLE CSV FORMAT ('content' argument)
|
|
// Must be a string with each row \n separated and each column comma separated
|
|
// 'Namespace path,Authentication method,Total clients,Entity clients,Non-entity clients\n
|
|
// namespacelonglonglong4/,,191,171,20\n
|
|
// namespacelonglonglong4/,auth/method/uMGBU,35,20,15\n'
|
|
csv(filename: string, content: string) {
|
|
this.download(filename, content, 'csv');
|
|
}
|
|
|
|
pem(filename: string, content: string) {
|
|
this.download(filename, content, 'pem');
|
|
}
|
|
|
|
miscExtension(filename: string, content: string, extension: string) {
|
|
this.download(filename, content, extension);
|
|
}
|
|
}
|