storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/uploads/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 Moment from "moment" 18 import storage from "local-storage-fallback" 19 import * as alertActions from "../alert/actions" 20 import * as objectsActions from "../objects/actions" 21 import { getCurrentBucket } from "../buckets/selectors" 22 import { getCurrentPrefix } from "../objects/selectors" 23 import { minioBrowserPrefix } from "../constants" 24 25 export const ADD = "uploads/ADD" 26 export const UPDATE_PROGRESS = "uploads/UPDATE_PROGRESS" 27 export const STOP = "uploads/STOP" 28 export const SHOW_ABORT_MODAL = "uploads/SHOW_ABORT_MODAL" 29 30 export const add = (slug, size, name) => ({ 31 type: ADD, 32 slug, 33 size, 34 name 35 }) 36 37 export const updateProgress = (slug, loaded) => ({ 38 type: UPDATE_PROGRESS, 39 slug, 40 loaded 41 }) 42 43 export const stop = slug => ({ 44 type: STOP, 45 slug 46 }) 47 48 export const showAbortModal = () => ({ 49 type: SHOW_ABORT_MODAL, 50 show: true 51 }) 52 53 export const hideAbortModal = () => ({ 54 type: SHOW_ABORT_MODAL, 55 show: false 56 }) 57 58 let requests = {} 59 60 export const addUpload = (xhr, slug, size, name) => { 61 return function(dispatch) { 62 requests[slug] = xhr 63 dispatch(add(slug, size, name)) 64 } 65 } 66 67 export const abortUpload = slug => { 68 return function(dispatch) { 69 const xhr = requests[slug] 70 if (xhr) { 71 xhr.abort() 72 } 73 dispatch(stop(slug)) 74 dispatch(hideAbortModal()) 75 } 76 } 77 78 export const uploadFile = file => { 79 return function(dispatch, getState) { 80 const state = getState() 81 const currentBucket = getCurrentBucket(state) 82 if (!currentBucket) { 83 dispatch( 84 alertActions.set({ 85 type: "danger", 86 message: "Please choose a bucket before trying to upload files." 87 }) 88 ) 89 return 90 } 91 const currentPrefix = getCurrentPrefix(state) 92 var _filePath = file.path || file.name 93 if (_filePath.charAt(0) == '/') { 94 _filePath = _filePath.substring(1) 95 } 96 const filePath = _filePath 97 const objectName = encodeURIComponent(`${currentPrefix}${filePath}`) 98 const uploadUrl = `${ 99 window.location.origin 100 }${minioBrowserPrefix}/upload/${currentBucket}/${objectName}` 101 const slug = `${currentBucket}-${currentPrefix}-${filePath}` 102 103 let xhr = new XMLHttpRequest() 104 xhr.open("PUT", uploadUrl, true) 105 xhr.withCredentials = false 106 const token = storage.getItem("token") 107 if (token) { 108 xhr.setRequestHeader( 109 "Authorization", 110 "Bearer " + storage.getItem("token") 111 ) 112 } 113 xhr.setRequestHeader( 114 "x-amz-date", 115 Moment() 116 .utc() 117 .format("YYYYMMDDTHHmmss") + "Z" 118 ) 119 120 dispatch(addUpload(xhr, slug, file.size, file.name)) 121 122 xhr.onload = function(event) { 123 if (xhr.status == 401 || xhr.status == 403) { 124 dispatch(hideAbortModal()) 125 dispatch(stop(slug)) 126 dispatch( 127 alertActions.set({ 128 type: "danger", 129 message: "Unauthorized request." 130 }) 131 ) 132 } 133 if (xhr.status == 500) { 134 dispatch(hideAbortModal()) 135 dispatch(stop(slug)) 136 dispatch( 137 alertActions.set({ 138 type: "danger", 139 message: xhr.responseText 140 }) 141 ) 142 } 143 if (xhr.status == 200) { 144 dispatch(hideAbortModal()) 145 dispatch(stop(slug)) 146 dispatch( 147 alertActions.set({ 148 type: "success", 149 message: "File '" + filePath + "' uploaded successfully." 150 }) 151 ) 152 dispatch(objectsActions.selectPrefix(currentPrefix)) 153 } 154 } 155 156 xhr.upload.addEventListener("error", event => { 157 dispatch(stop(slug)) 158 dispatch( 159 alertActions.set({ 160 type: "danger", 161 message: "Error occurred uploading '" + filePath + "'." 162 }) 163 ) 164 }) 165 166 xhr.upload.addEventListener("progress", event => { 167 if (event.lengthComputable) { 168 let loaded = event.loaded 169 let total = event.total 170 // Update the counter 171 dispatch(updateProgress(slug, loaded)) 172 } 173 }) 174 175 xhr.send(file) 176 } 177 }