Repository navigation
Expand file tree
/
Copy pathmyminioNodeConfig.js
More file actions
244 lines (230 loc) · 9.33 KB
/
Copy pathmyminioNodeConfig.js
File metadata and controls
244 lines (230 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict';
var $ = require('jquery');
var ko = require('knockout');
var m = require('mithril');
var bootbox = require('bootbox');
var Raven = require('raven-js');
var $osf = require('js/osfHelpers');
var oop = require('js/oop');
var OauthAddonFolderPicker = require('js/oauthAddonNodeConfig')._OauthAddonNodeConfigViewModel;
var MyMinIOFolderPickerViewModel = oop.extend(OauthAddonFolderPicker, {
constructor: function(addonName, url, selector, folderPicker, opts, tbOpts) {
var self = this;
self.super.constructor(addonName, url, selector, folderPicker, tbOpts);
// Non-OAuth fields
self.accessKey = ko.observable('');
self.secretKey = ko.observable('');
// Treebeard config
self.treebeardOptions = $.extend(
{},
OauthAddonFolderPicker.prototype.treebeardOptions,
{ // TreeBeard Options
columnTitles: function() {
return [{
title: 'Buckets',
width: '75%',
sort: false
}, {
title: 'Select',
width: '25%',
sort: false
}];
},
resolveToggle: function(item) {
return '';
},
resolveIcon: function(item) {
return m('i.fa.fa-folder-o', ' ');
},
},
tbOpts
);
},
connectAccount: function() {
var self = this;
if( !self.accessKey() && !self.secretKey() ){
self.changeMessage('Please enter both an API access key and secret key.', 'text-danger');
return;
}
if (!self.accessKey() ){
self.changeMessage('Please enter an API access key.', 'text-danger');
return;
}
if (!self.secretKey() ){
self.changeMessage('Please enter an API secret key.', 'text-danger');
return;
}
$osf.block();
return $osf.postJSON(
self.urls().create, {
secret_key: self.secretKey(),
access_key: self.accessKey()
}
).done(function(response) {
$osf.unblock();
self.clearModal();
$('#myminioInputCredentials').modal('hide');
self.changeMessage('Successfully added My MinIO credentials.', 'text-success', null, true);
self.updateFromData(response);
self.importAuth();
}).fail(function(xhr, status, error) {
$osf.unblock();
var message = '';
var response = JSON.parse(xhr.responseText);
if (response && response.message) {
message = response.message;
}
self.changeMessage(message, 'text-danger');
Raven.captureMessage('Could not add My MinIO credentials', {
extra: {
url: self.urls().importAuth,
textStatus: status,
error: error
}
});
});
},
/**
* Tests if the given string is a valid My MinIO bucket name. Supports two modes: strict and lax.
* Strict is for bucket creation and follows the guidelines at:
*
* http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html#bucketnamingrules
*
* However, the US East (N. Virginia) region currently permits much laxer naming rules. The S3
* docs claim this will be changed at some point, but to support our user's already existing
* buckets, we provide the lax mode checking.
*
* Strict checking is the default.
*
* @param {String} bucketName user-provided name of bucket to validate
* @param {Boolean} laxChecking whether to use the more permissive validation
*/
isValidBucketName: function(bucketName, laxChecking) {
if (laxChecking === true) {
return /^[a-zA-Z0-9.\-_]{1,255}$/.test(bucketName);
}
var label = '[a-z0-9]+(?:[a-z0-9\-]*[a-z0-9])?';
var strictBucketName = new RegExp('^' + label + '(?:\\.' + label + ')*$');
var isIpAddress = /^[0-9]+(?:\.[0-9]+){3}$/;
return bucketName.length >= 3 && bucketName.length <= 63 &&
strictBucketName.test(bucketName) && !isIpAddress.test(bucketName);
},
/** Reset all fields from My MinIO credentials input modal */
clearModal: function() {
var self = this;
self.message('');
self.messageClass('text-info');
self.secretKey(null);
self.accessKey(null);
},
createBucket: function(self, bucketName) {
$osf.block();
bucketName = bucketName.toLowerCase();
return $osf.postJSON(
self.urls().createBucket, {bucket_name: bucketName}
).done(function(response) {
$osf.unblock();
self.loadedFolders(false);
self.activatePicker();
var msg = 'Successfully created bucket "' + $osf.htmlEscape(bucketName) + '". You can now select it from the list.';
var msgType = 'text-success';
self.changeMessage(msg, msgType, null, true);
}).fail(function(xhr) {
var resp = JSON.parse(xhr.responseText);
var message = resp.message;
var title = resp.title || 'Problem creating bucket';
$osf.unblock();
if (!message) {
message = 'Looks like that name is taken. Try another name?';
}
bootbox.confirm({
title: $osf.htmlEscape(title),
message: $osf.htmlEscape(message),
callback: function(result) {
if (result) {
self.openCreateBucket();
}
},
buttons:{
confirm:{
label:'Try again'
}
}
});
});
},
openCreateBucket: function() {
var self = this;
bootbox.dialog({
title: 'Create a new bucket',
message:
'<div class="row"> ' +
'<div class="col-md-12"> ' +
'<form class="form-horizontal" onsubmit="return false"> ' +
'<div class="form-group"> ' +
'<label class="col-md-4 control-label" for="bucketName">Bucket Name</label> ' +
'<div class="col-md-8"> ' +
'<input id="bucketName" name="bucketName" type="text" placeholder="Enter bucket name" class="form-control" autofocus> ' +
'<div>' +
'<span id="bucketModalErrorMessage" ></span>' +
'</div>'+
'</div>' +
'</div>' +
'</form>' +
'</div>' +
'</div>',
buttons: {
cancel: {
label: 'Cancel',
className: 'btn-default'
},
confirm: {
label: 'Create',
className: 'btn-success',
callback: function () {
var bucketName = $('#bucketName').val();
if (!bucketName) {
var errorMessage = $('#bucketModalErrorMessage');
errorMessage.text('Bucket name cannot be empty');
errorMessage[0].classList.add('text-danger');
return false;
} else if (!self.isValidBucketName(bucketName, false)) {
bootbox.confirm({
title: 'Invalid bucket name',
message: 'My MinIO buckets can contain lowercase letters, numbers, and hyphens separated by' +
' periods. Please try another name.',
callback: function (result) {
if (result) {
self.openCreateBucket();
}
},
buttons: {
confirm: {
label: 'Try again'
}
}
});
} else {
self.createBucket(self, bucketName);
}
}
}
}
});
}
});
// Public API
function MyMinIONodeConfig(addonName, selector, url, folderPicker, opts, tbOpts) {
var self = this;
self.url = url;
self.folderPicker = folderPicker;
opts = opts || {};
tbOpts = tbOpts || {};
self.viewModel = new MyMinIOFolderPickerViewModel(addonName, url, selector, folderPicker, opts, tbOpts);
self.viewModel.updateFromData();
$osf.applyBindings(self.viewModel, selector);
}
module.exports = {
MyMinIONodeConfig: MyMinIONodeConfig,
_MyNinIONodeConfigViewModel: MyMinIOFolderPickerViewModel
};