2018-09-25 16:28:26 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { set } from '@ember/object';
|
2019-05-03 03:20:28 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
2018-04-03 14:16:57 +00:00
|
|
|
const BASE_64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gi;
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend({
|
2019-10-10 02:09:25 +00:00
|
|
|
'data-test-pgp-file': true,
|
2018-04-03 14:16:57 +00:00
|
|
|
classNames: ['box', 'is-fullwidth', 'is-marginless', 'is-shadowless'],
|
|
|
|
key: null,
|
|
|
|
index: null,
|
|
|
|
onChange: () => {},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @public
|
|
|
|
* @param String
|
|
|
|
* Text to use as the label for the file input
|
|
|
|
* If null, a default will be rendered
|
|
|
|
*/
|
|
|
|
label: null,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @public
|
|
|
|
* @param String
|
|
|
|
* Text to use as help under the file input
|
|
|
|
* If null, a default will be rendered
|
|
|
|
*/
|
|
|
|
fileHelpText: null,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @public
|
|
|
|
* @param String
|
|
|
|
* Text to use as help under the textarea in text-input mode
|
|
|
|
* If null, a default will be rendered
|
|
|
|
*/
|
|
|
|
textareaHelpText: null,
|
|
|
|
|
|
|
|
readFile(file) {
|
|
|
|
const reader = new FileReader();
|
2019-05-03 03:20:28 +00:00
|
|
|
reader.onload = () => this.setPGPKey.perform(reader.result, file.name);
|
2018-04-03 14:16:57 +00:00
|
|
|
// this gives us a base64-encoded string which is important in the onload
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
},
|
|
|
|
|
2019-05-03 03:20:28 +00:00
|
|
|
setPGPKey: task(function*(dataURL, filename) {
|
2018-04-03 14:16:57 +00:00
|
|
|
const b64File = dataURL.split(',')[1].trim();
|
|
|
|
const decoded = atob(b64File).trim();
|
|
|
|
|
|
|
|
// If a b64-encoded file was uploaded, then after decoding, it
|
|
|
|
// will still be b64.
|
|
|
|
// If after decoding it's not b64, we want
|
|
|
|
// the original as it was only encoded when we used `readAsDataURL`.
|
|
|
|
const fileData = decoded.match(BASE_64_REGEX) ? decoded : b64File;
|
2019-05-03 03:20:28 +00:00
|
|
|
yield this.get('onChange')(this.get('index'), { value: fileData, fileName: filename });
|
|
|
|
}).withTestWaiter(),
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
pickedFile(e) {
|
|
|
|
const { files } = e.target;
|
|
|
|
if (!files.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let i = 0, len = files.length; i < len; i++) {
|
|
|
|
this.readFile(files[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateData(e) {
|
|
|
|
const key = this.get('key');
|
|
|
|
set(key, 'value', e.target.value);
|
|
|
|
this.get('onChange')(this.get('index'), this.get('key'));
|
|
|
|
},
|
|
|
|
clearKey() {
|
|
|
|
this.get('onChange')(this.get('index'), { value: '' });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|