storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/buckets/actions.js (about)

     1  /*
     2   * MinIO Cloud Storage (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import web from "../web"
    18  import history from "../history"
    19  import * as alertActions from "../alert/actions"
    20  import * as objectsActions from "../objects/actions"
    21  import { pathSlice } from "../utils"
    22  
    23  export const SET_LIST = "buckets/SET_LIST"
    24  export const ADD = "buckets/ADD"
    25  export const REMOVE = "buckets/REMOVE"
    26  export const SET_FILTER = "buckets/SET_FILTER"
    27  export const SET_CURRENT_BUCKET = "buckets/SET_CURRENT_BUCKET"
    28  export const SHOW_MAKE_BUCKET_MODAL = "buckets/SHOW_MAKE_BUCKET_MODAL"
    29  export const SHOW_BUCKET_POLICY = "buckets/SHOW_BUCKET_POLICY"
    30  export const SET_POLICIES = "buckets/SET_POLICIES"
    31  
    32  export const fetchBuckets = () => {
    33    return function(dispatch) {
    34      const { bucket, prefix } = pathSlice(history.location.pathname)
    35      return web.ListBuckets().then(res => {
    36        const buckets = res.buckets ? res.buckets.map(bucket => bucket.name) : []
    37        if (buckets.length > 0) {
    38          dispatch(setList(buckets))
    39          if (bucket && buckets.indexOf(bucket) > -1) {
    40            dispatch(selectBucket(bucket, prefix))
    41          } else {
    42            dispatch(selectBucket(buckets[0]))
    43          }
    44        } else {
    45          if (bucket) {
    46            dispatch(setList([bucket]))
    47            dispatch(selectBucket(bucket, prefix))
    48          } else {
    49            dispatch(selectBucket(""))
    50            history.replace("/")
    51          }
    52        }
    53      })
    54      .catch(err => {
    55        if (bucket && err.message === "Access Denied." || err.message.indexOf('Prefix access is denied') > -1 ) {
    56          dispatch(setList([bucket]))
    57          dispatch(selectBucket(bucket, prefix))
    58        } else {
    59          dispatch(
    60            alertActions.set({
    61              type: "danger",
    62              message: err.message,
    63              autoClear: true,
    64            })
    65          )
    66        }
    67      })
    68    }
    69  }
    70  
    71  export const setList = buckets => {
    72    return {
    73      type: SET_LIST,
    74      buckets
    75    }
    76  }
    77  
    78  export const setFilter = filter => {
    79    return {
    80      type: SET_FILTER,
    81      filter
    82    }
    83  }
    84  
    85  export const selectBucket = (bucket, prefix) => {
    86    return function(dispatch) {
    87      dispatch(setCurrentBucket(bucket))
    88      dispatch(objectsActions.selectPrefix(prefix || ""))
    89    }
    90  }
    91  
    92  export const setCurrentBucket = bucket => {
    93    return {
    94      type: SET_CURRENT_BUCKET,
    95      bucket
    96    }
    97  }
    98  
    99  export const makeBucket = bucket => {
   100    return function(dispatch) {
   101      return web
   102        .MakeBucket({
   103          bucketName: bucket
   104        })
   105        .then(() => {
   106          dispatch(addBucket(bucket))
   107          dispatch(selectBucket(bucket))
   108        })
   109        .catch(err =>
   110          dispatch(
   111            alertActions.set({
   112              type: "danger",
   113              message: err.message
   114            })
   115          )
   116        )
   117    }
   118  }
   119  
   120  export const deleteBucket = bucket => {
   121    return function(dispatch) {
   122      return web
   123        .DeleteBucket({
   124          bucketName: bucket
   125        })
   126        .then(() => {
   127          dispatch(
   128            alertActions.set({
   129              type: "info",
   130              message: "Bucket '" + bucket + "' has been deleted."
   131            })
   132          )
   133          dispatch(removeBucket(bucket))
   134          dispatch(fetchBuckets())
   135        })
   136        .catch(err => { 
   137          dispatch(
   138            alertActions.set({
   139              type: "danger",
   140              message: err.message
   141            })
   142          )
   143        })
   144    }
   145  }
   146  
   147  export const addBucket = bucket => ({
   148    type: ADD,
   149    bucket
   150  })
   151  
   152  export const removeBucket = bucket => ({
   153    type: REMOVE,
   154    bucket
   155  })
   156  
   157  export const showMakeBucketModal = () => ({
   158    type: SHOW_MAKE_BUCKET_MODAL,
   159    show: true
   160  })
   161  
   162  export const hideMakeBucketModal = () => ({
   163    type: SHOW_MAKE_BUCKET_MODAL,
   164    show: false
   165  })
   166  
   167  export const fetchPolicies = bucket => {
   168    return function(dispatch) {
   169      return web
   170        .ListAllBucketPolicies({
   171          bucketName: bucket
   172        })
   173        .then(res => {
   174          let policies = res.policies
   175          if(policies)
   176            dispatch(setPolicies(policies))
   177          else
   178            dispatch(setPolicies([]))
   179        })
   180        .catch(err => {
   181          dispatch(
   182            alertActions.set({
   183              type: "danger",
   184              message: err.message
   185            })
   186          )
   187        })
   188    }
   189  }
   190  
   191  export const setPolicies = policies => ({
   192    type: SET_POLICIES,
   193    policies
   194  })
   195  
   196  export const showBucketPolicy = () => ({
   197    type: SHOW_BUCKET_POLICY,
   198    show: true
   199  })
   200  
   201  export const hideBucketPolicy = () => ({
   202    type: SHOW_BUCKET_POLICY,
   203    show: false
   204  })