2017-01-24 03:07:22 +01:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage (C) 2016, 2018 MinIO, Inc.
|
2017-01-24 03:07:22 +01: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 21:49:12 +02:00
|
|
|
import JSONrpc from './jsonrpc'
|
|
|
|
import { minioBrowserPrefix } from './constants.js'
|
|
|
|
import Moment from 'moment'
|
|
|
|
import storage from 'local-storage-fallback'
|
2017-01-24 03:07:22 +01:00
|
|
|
|
2018-02-01 04:53:11 +01:00
|
|
|
class Web {
|
|
|
|
constructor(endpoint) {
|
2018-03-26 21:49:12 +02:00
|
|
|
const namespace = 'Web'
|
2017-01-24 03:07:22 +01:00
|
|
|
this.JSONrpc = new JSONrpc({
|
|
|
|
endpoint,
|
2018-03-22 20:25:56 +01:00
|
|
|
namespace
|
2017-01-24 03:07:22 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
makeCall(method, options) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.JSONrpc.call(method, {
|
|
|
|
params: options
|
|
|
|
}, storage.getItem('token'))
|
2017-01-24 03:07:22 +01:00
|
|
|
.catch(err => {
|
|
|
|
if (err.status === 401) {
|
2018-03-26 21:49:12 +02:00
|
|
|
storage.removeItem('token')
|
2018-02-01 04:53:11 +01:00
|
|
|
location.reload()
|
2018-03-26 21:49:12 +02:00
|
|
|
throw new Error('Please re-login.')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
2018-03-26 21:49:12 +02:00
|
|
|
if (err.status)
|
|
|
|
throw new Error(`Server returned error [${err.status}]`)
|
2019-04-09 20:39:42 +02:00
|
|
|
throw new Error('MinIO server is unreachable')
|
2017-01-24 03:07:22 +01: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 21:49:12 +02:00
|
|
|
if (result.uiVersion !== currentUiVersion
|
|
|
|
&& currentUiVersion !== 'MINIO_UI_VERSION') {
|
|
|
|
storage.setItem('newlyUpdated', true)
|
2017-01-24 03:07:22 +01:00
|
|
|
location.reload()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
}
|
|
|
|
LoggedIn() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return !!storage.getItem('token')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
Login(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('Login', args)
|
|
|
|
.then(res => {
|
|
|
|
storage.setItem('token', `${res.token}`)
|
|
|
|
return res
|
|
|
|
})
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
Logout() {
|
2018-03-26 21:49:12 +02:00
|
|
|
storage.removeItem('token')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
ServerInfo() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('ServerInfo')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
StorageInfo() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('StorageInfo')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
ListBuckets() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('ListBuckets')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
MakeBucket(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('MakeBucket', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
2017-12-29 14:15:44 +01:00
|
|
|
DeleteBucket(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('DeleteBucket', args)
|
2017-12-29 14:15:44 +01:00
|
|
|
}
|
2017-01-24 03:07:22 +01:00
|
|
|
ListObjects(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('ListObjects', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
PresignedGet(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('PresignedGet', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
PutObjectURL(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('PutObjectURL', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
RemoveObject(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('RemoveObject', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
GetAuth() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('GetAuth')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
GenerateAuth() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('GenerateAuth')
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
SetAuth(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('SetAuth', args)
|
|
|
|
.then(res => {
|
|
|
|
storage.setItem('token', `${res.token}`)
|
|
|
|
return res
|
|
|
|
})
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
2017-07-24 21:46:37 +02:00
|
|
|
CreateURLToken() {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('CreateURLToken')
|
2017-07-24 21:46:37 +02:00
|
|
|
}
|
2017-01-24 03:07:22 +01:00
|
|
|
GetBucketPolicy(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('GetBucketPolicy', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
SetBucketPolicy(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('SetBucketPolicy', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
ListAllBucketPolicies(args) {
|
2018-03-26 21:49:12 +02:00
|
|
|
return this.makeCall('ListAllBucketPolicies', args)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 04:53:11 +01:00
|
|
|
|
2018-03-26 21:49:12 +02:00
|
|
|
const web = new Web(`${window.location.protocol}//${window.location.host}${minioBrowserPrefix}/webrpc`);
|
2018-02-01 04:53:11 +01:00
|
|
|
|
2018-03-26 21:49:12 +02:00
|
|
|
export default web;
|