vault/ui/app/components/file-to-array-buffer.js
Jordan Reimer 27f194f9d3
UI: Snapshot load form (#8743) (#9093)
* build mirage handler for snapshot recovery endpoints

* WIP add namespace and mount fields to read/recover form

* change option to engine object

* update badge color according to status and make loaded snapshot reqs within root ns

* wip read reqs and view

* add recovery banner and form validation

* move read view into modal

* tidying

* organize

* tests

* style fixes

* error handling

* tests

* WIP load form and update header

* styling

* update requests with snapshot params

* style fix

* yarn lock update for latest spec updates

* hide dr seconday + flesh out load reqs

* tests

* add polling for snapshot status

* disable button when not in ready state

* reset errors

* don't poll during tests

* remove todo

* test updates

* fix ns select + test

* remove todo

* Revert "Merge branch 'copy/main/VAULT-38374/build-snapshot-manage-form' into ui/VAULT-38373/build-snapshot-load-form"

This reverts commit f7cce759cda286bc7b76e84186b5497589706095, reversing
changes made to 6cba99557ce6d8478f4c875579b71be4c531522e.

* add breadcrumbs for load form

* hide shared paths on performance secondaries

* styling, tidy, mount options, engine types

* lots of tidying, add manual mount input, slower polling

* make read + recover requests in selected namespace

* link to child ns if recovering there

* test updates

* remove todos

* centralize recovery data values

* type, state and error improvements

* update FileToArrayBuffer to use HDS FileInput

* error handling

* allow manual input

* tests

* remove unneeded services

* api call update

* update radio fields to allow a badge for 404 message

* update tests

---------

Co-authored-by: lane-wetmore <lane.wetmore@hashicorp.com>
Co-authored-by: claire bontempo <cbontempo@hashicorp.com>
2025-09-03 15:36:57 -06:00

63 lines
1.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import filesize from 'filesize';
/**
* @module FileToArrayBuffer
* `FileToArrayBuffer` is a component that will allow you to pick a file from the local file system. Once
* loaded, this file will be emitted as a JS ArrayBuffer to the passed `onChange` callback.
*
* @example
* ```js
* <FileToArrayBuffer @onChange={{fn (mut file)}} />
* ```
* @param {function} onChange - The function to call when the file read is complete. Receives the file as a JS ArrayBuffer
* @param {string} [error] - Text to use as the error message for the file input
* @param {string} [label] - Text to use as the label for the file input
* @param {string} [fileHelpText] - Text to use as help under the file input
*
*/
export default class FileToArrayBufferComponent extends Component {
@tracked filename = null;
@tracked fileSize = null;
@tracked fileLastModified = null;
readFile(file) {
const reader = new FileReader();
reader.onload = () => {
// raft-snapshot-restore test was failing on CI trying to invoke fileChange on destroyed object
// ensure that the component has not been torn down
if (!this.isDestroyed && !this.isDestroying) {
this.fileChange(reader.result, file);
}
};
reader.readAsArrayBuffer(file);
}
@action
pickedFile(e) {
const { files } = e.target;
if (!files.length) {
return;
}
for (let i = 0, len = files.length; i < len; i++) {
this.readFile(files[i]);
}
}
@action
fileChange(fileAsBytes, fileMeta) {
const { name, size, lastModifiedDate } = fileMeta || {};
this.filename = name;
this.fileSize = size ? filesize(size) : null;
this.fileLastModified = lastModifiedDate;
this.args.onChange(fileAsBytes, name);
}
}