2017-01-23 21:07:22 -05:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage (C) 2016, 2018 MinIO, Inc.
|
2017-01-23 21:07:22 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-03-26 15:49:12 -04:00
|
|
|
import JSONrpc from './jsonrpc'
|
|
|
|
import { minioBrowserPrefix } from './constants.js'
|
|
|
|
import Moment from 'moment'
|
|
|
|
import storage from 'local-storage-fallback'
|
2017-01-23 21:07:22 -05:00
|
|
|
|
2018-01-31 22:53:11 -05:00
|
|
|
class Web {
|
|
|
|
constructor(endpoint) {
|
2018-03-26 15:49:12 -04:00
|
|
|
const namespace = 'Web'
|
2017-01-23 21:07:22 -05:00
|
|
|
this.JSONrpc = new JSONrpc({
|
|
|
|
endpoint,
|
2018-03-22 15:25:56 -04:00
|
|
|
namespace
|
2017-01-23 21:07:22 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
makeCall(method, options) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.JSONrpc.call(method, {
|
|
|
|
params: options
|
|
|
|
}, storage.getItem('token'))
|
2017-01-23 21:07:22 -05:00
|
|
|
.catch(err => {
|
|
|
|
if (err.status === 401) {
|
2018-03-26 15:49:12 -04:00
|
|
|
storage.removeItem('token')
|
2018-01-31 22:53:11 -05:00
|
|
|
location.reload()
|
2018-03-26 15:49:12 -04:00
|
|
|
throw new Error('Please re-login.')
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
2018-03-26 15:49:12 -04:00
|
|
|
if (err.status)
|
|
|
|
throw new Error(`Server returned error [${err.status}]`)
|
2019-04-09 14:39:42 -04:00
|
|
|
throw new Error('MinIO server is unreachable')
|
2017-01-23 21:07:22 -05:00
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
let json = JSON.parse(res.text)
|
|
|
|
let result = json.result
|
|
|
|
let error = json.error
|
|
|
|
if (error) {
|
|
|
|
throw new Error(error.message)
|
|
|
|
}
|
|
|
|
if (!Moment(result.uiVersion).isValid()) {
|
|
|
|
throw new Error("Invalid UI version in the JSON-RPC response")
|
|
|
|
}
|
2018-03-26 15:49:12 -04:00
|
|
|
if (result.uiVersion !== currentUiVersion
|
|
|
|
&& currentUiVersion !== 'MINIO_UI_VERSION') {
|
|
|
|
storage.setItem('newlyUpdated', true)
|
2017-01-23 21:07:22 -05:00
|
|
|
location.reload()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
}
|
|
|
|
LoggedIn() {
|
2018-03-26 15:49:12 -04:00
|
|
|
return !!storage.getItem('token')
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
Login(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('Login', args)
|
|
|
|
.then(res => {
|
|
|
|
storage.setItem('token', `${res.token}`)
|
|
|
|
return res
|
|
|
|
})
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
Logout() {
|
2018-03-26 15:49:12 -04:00
|
|
|
storage.removeItem('token')
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
2019-05-29 16:18:46 -04:00
|
|
|
GetToken() {
|
|
|
|
return storage.getItem('token')
|
|
|
|
}
|
2017-01-23 21:07:22 -05:00
|
|
|
ServerInfo() {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('ServerInfo')
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
StorageInfo() {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('StorageInfo')
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
ListBuckets() {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('ListBuckets')
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
MakeBucket(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('MakeBucket', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
2017-12-29 08:15:44 -05:00
|
|
|
DeleteBucket(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('DeleteBucket', args)
|
2017-12-29 08:15:44 -05:00
|
|
|
}
|
2017-01-23 21:07:22 -05:00
|
|
|
ListObjects(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('ListObjects', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
PresignedGet(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('PresignedGet', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
PutObjectURL(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('PutObjectURL', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
RemoveObject(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('RemoveObject', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
SetAuth(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('SetAuth', args)
|
|
|
|
.then(res => {
|
|
|
|
storage.setItem('token', `${res.token}`)
|
|
|
|
return res
|
|
|
|
})
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
2017-07-24 15:46:37 -04:00
|
|
|
CreateURLToken() {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('CreateURLToken')
|
2017-07-24 15:46:37 -04:00
|
|
|
}
|
2017-01-23 21:07:22 -05:00
|
|
|
GetBucketPolicy(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('GetBucketPolicy', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
SetBucketPolicy(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('SetBucketPolicy', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
ListAllBucketPolicies(args) {
|
2018-03-26 15:49:12 -04:00
|
|
|
return this.makeCall('ListAllBucketPolicies', args)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 22:53:11 -05:00
|
|
|
|
2018-03-26 15:49:12 -04:00
|
|
|
const web = new Web(`${window.location.protocol}//${window.location.host}${minioBrowserPrefix}/webrpc`);
|
2018-01-31 22:53:11 -05:00
|
|
|
|
2018-03-26 15:49:12 -04:00
|
|
|
export default web;
|