2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-12-13 18:37:10 +00:00
|
|
|
import Service from '@ember/service';
|
2023-03-22 18:19:11 +00:00
|
|
|
import timestamp from 'core/utils/timestamp';
|
2022-12-13 18:37:10 +00:00
|
|
|
|
2022-12-15 22:57:20 +00:00
|
|
|
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',
|
|
|
|
};
|
|
|
|
|
2022-12-13 18:37:10 +00:00
|
|
|
export default class DownloadService extends Service {
|
2022-12-15 22:57:20 +00:00
|
|
|
download(filename: string, content: string, extension: string) {
|
|
|
|
// replace spaces with hyphens, append extension to filename
|
|
|
|
const formattedFilename =
|
|
|
|
`${filename?.replace(/\s+/g, '-')}.${extension}` ||
|
2023-03-22 18:19:11 +00:00
|
|
|
`vault-data-${timestamp.now().toISOString()}.${extension}`;
|
2022-12-15 22:57:20 +00:00
|
|
|
|
|
|
|
// map extension to MIME type or use default
|
|
|
|
const mimetype = EXTENSION_TO_MIME[extension as keyof Extensions] || 'text/plain';
|
|
|
|
|
|
|
|
// commence download
|
2022-12-13 18:37:10 +00:00
|
|
|
const { document, URL } = window;
|
|
|
|
const downloadElement = document.createElement('a');
|
2022-12-15 22:57:20 +00:00
|
|
|
const data = new File([content], formattedFilename, { type: mimetype });
|
|
|
|
downloadElement.download = formattedFilename;
|
|
|
|
downloadElement.href = URL.createObjectURL(data);
|
2022-12-13 18:37:10 +00:00
|
|
|
document.body.appendChild(downloadElement);
|
|
|
|
downloadElement.click();
|
|
|
|
URL.revokeObjectURL(downloadElement.href);
|
|
|
|
downloadElement.remove();
|
2022-12-15 22:57:20 +00:00
|
|
|
return formattedFilename;
|
2022-12-13 18:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-12-15 22:57:20 +00:00
|
|
|
this.download(filename, content, 'csv');
|
2022-12-13 18:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pem(filename: string, content: string) {
|
2022-12-15 22:57:20 +00:00
|
|
|
this.download(filename, content, 'pem');
|
|
|
|
}
|
|
|
|
|
|
|
|
miscExtension(filename: string, content: string, extension: string) {
|
|
|
|
this.download(filename, content, extension);
|
2022-12-13 18:37:10 +00:00
|
|
|
}
|
|
|
|
}
|