trim extensions from filename when uploading a policy file (#4693)
This commit is contained in:
parent
d499eeac51
commit
08c7cddef9
|
@ -1,4 +1,5 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
|
import trimRight from 'vault/utils/trim-right';
|
||||||
import PolicyEditController from 'vault/mixins/policy-edit-controller';
|
import PolicyEditController from 'vault/mixins/policy-edit-controller';
|
||||||
|
|
||||||
export default Ember.Controller.extend(PolicyEditController, {
|
export default Ember.Controller.extend(PolicyEditController, {
|
||||||
|
@ -11,7 +12,8 @@ export default Ember.Controller.extend(PolicyEditController, {
|
||||||
let model = this.get('model');
|
let model = this.get('model');
|
||||||
model.set('policy', value);
|
model.set('policy', value);
|
||||||
if (!model.get('name')) {
|
if (!model.get('name')) {
|
||||||
model.set('name', fileName);
|
let trimmedFileName = trimRight(fileName, ['.json','.txt', '.hcl', '.policy']);
|
||||||
|
model.set('name', trimmedFileName);
|
||||||
}
|
}
|
||||||
this.set('showFileUpload', false);
|
this.set('showFileUpload', false);
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
export default function(fileName, toTrimArray=[]){
|
||||||
|
const extension = new RegExp(toTrimArray.join('$|'));
|
||||||
|
return fileName.replace(extension, "");
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
import trimRight from 'vault/utils/trim-right';
|
||||||
|
import { module, test } from 'qunit';
|
||||||
|
|
||||||
|
module('Unit | Util | trim right');
|
||||||
|
|
||||||
|
|
||||||
|
test('it trims extension array from end of string', function(assert) {
|
||||||
|
const trimmedName = trimRight("my-file-name-is-cool.json", ['.json','.txt', '.hcl', '.policy']);
|
||||||
|
|
||||||
|
assert.equal(trimmedName, 'my-file-name-is-cool');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it only trims extension array from the very end of string', function(assert) {
|
||||||
|
const trimmedName = trimRight("my-file-name.json-is-cool.json", ['.json','.txt', '.hcl', '.policy']);
|
||||||
|
|
||||||
|
assert.equal(trimmedName, 'my-file-name.json-is-cool');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it returns string as is if trim array is empty', function(assert) {
|
||||||
|
const trimmedName = trimRight("my-file-name-is-cool.json", []);
|
||||||
|
|
||||||
|
assert.equal(trimmedName, 'my-file-name-is-cool.json');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it returns string as is if trim array is not passed in', function(assert) {
|
||||||
|
const trimmedName = trimRight("my-file-name-is-cool.json");
|
||||||
|
|
||||||
|
assert.equal(trimmedName, 'my-file-name-is-cool.json');
|
||||||
|
});
|
Loading…
Reference in New Issue