Skip to content

Commit 706de27

Browse files
authored
Merge pull request #56 from walle233/feat-oss
Update bucket add quota param
2 parents eaba65e + 0dcb91a commit 706de27

File tree

5 files changed

+87
-28
lines changed

5 files changed

+87
-28
lines changed

packages/app-console/src/api/oss.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ export async function createBucket(bucketName, mode, quota) {
6161
* @param {number} mode
6262
* @returns {Promise<any[]>}
6363
*/
64-
export async function updateBucket(bucketName, mode) {
64+
export async function updateBucket(bucketName, mode, quota) {
6565
const appid = store.state.app.appid
6666
const res = await request({
6767
url: `/apps/${appid}/oss/buckets/${bucketName}`,
6868
method: 'put',
6969
data: {
70-
mode
70+
mode,
71+
quota
7172
}
7273
})
7374

packages/app-console/src/styles/index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ aside {
173173

174174
.filter-container {
175175
padding-bottom: 10px;
176+
position: relative;
176177

177178
.filter-item {
178179
display: inline-block;

packages/app-console/src/utils/file.js

+18
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,21 @@ export function validateFileName(name) {
5151
const reg = /^[^\\\/\:\*\?\"\<\>\|\.]+$/
5252
return reg.test(name)
5353
}
54+
55+
export function byte2gb(bytes) {
56+
return Math.floor(bytes / 1024 / 1024 / 1024)
57+
}
58+
59+
export function gb2byte(gb) {
60+
return gb * 1024 * 1024 * 1024
61+
}
62+
63+
export function byte2GbOrMb(bytes) {
64+
if (bytes >= 1024 * 1024 * 1024) {
65+
return `${Math.floor(bytes / 1024 / 1024 / 1024)} GB`
66+
} else if (bytes > 1024 * 1024) {
67+
return `${Math.floor(bytes / 1024 / 1024)} MB`
68+
} else {
69+
return `${Math.floor(bytes / 1024)} KB`
70+
}
71+
}

packages/app-console/src/views/storage/buckets.vue

+13-4
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,12 @@ export default {
193193
const ret = await oss.getBuckets().catch(() => { this.listLoading = false })
194194
assert(ret.code === 0, 'get file buckets got error')
195195
196-
this.list = ret.data
197196
const usedQuota = ret.data.reduce((total, bucket) => {
198197
return total + bucket.quota
199198
}, 0)
200199
this.freeQuota = this.totalQuota - this.byte2gb(usedQuota)
200+
201+
this.list = ret.data
201202
this.listLoading = false
202203
},
203204
// 显示创建表单
@@ -219,7 +220,7 @@ export default {
219220
}
220221
221222
if (this.freeQuota < this.form.quota) {
222-
return showError('Bucket 容量不能超过总容量')
223+
return showError('所有Bucket容量相加不能超过总容量')
223224
}
224225
const quota = this.gb2byte(this.form.quota)
225226
// 执行创建请求
@@ -246,7 +247,7 @@ export default {
246247
},
247248
// 显示编辑表单
248249
showEditForm(row) {
249-
this.form = { ...row }
250+
this.form = { ...row, quota: this.byte2gb(row.quota) }
250251
this.dialogStatus = 'update'
251252
this.dialogFormVisible = true
252253
this.$nextTick(() => {
@@ -258,8 +259,16 @@ export default {
258259
this.$refs['dataForm'].validate(async(valid) => {
259260
if (!valid) { return }
260261
262+
// 检查quota是否可用
263+
const existedQuota = this.list.find(bucket => bucket.name === this.form.name).quota
264+
const freeQuota = this.freeQuota + this.byte2gb(existedQuota)
265+
if (freeQuota < this.form.quota) {
266+
return showError('所有Bucket容量相加不能超过总容量')
267+
}
268+
const quota = this.gb2byte(this.form.quota)
269+
261270
// 执行更新请求
262-
const r = await oss.updateBucket(this.form.name, this.form.mode)
271+
const r = await oss.updateBucket(this.form.name, this.form.mode, quota)
263272
264273
if (r.code) {
265274
this.$notify({

packages/app-console/src/views/storage/files.vue

+52-22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<span style="font-size: 16px;color: gray; margin-right: 5px;">当前:</span>
2222
<path-link :path="currentPath" :bucket="bucket" @change="onChangeDirectory" />
2323
</div>
24+
<div class="filter-item tips">
25+
<span>bucket容量:{{ bucketQuota }} </span>
26+
<span>已用容量:{{ bucketSize }} </span>
27+
<span>文件数量:{{ bucketObjects }} </span>
28+
</div>
2429
</div>
2530

2631
<!-- 表格 -->
@@ -105,7 +110,7 @@
105110
:show-file-list="true"
106111
:file-list="uploadFileList"
107112
:auto-upload="true"
108-
:http-request="uploadFile"
113+
:http-request="handleUploadFile"
109114
>
110115
<i class="el-icon-upload" />
111116
<div class="el-upload__text">
@@ -122,6 +127,7 @@ import * as oss from '@/api/oss'
122127
import { assert } from '@/utils/assert'
123128
import { showError, showSuccess } from '@/utils/show'
124129
import PathLink from './components/path-link.vue'
130+
import { byte2gb, byte2GbOrMb, gb2byte } from '@/utils/file'
125131
126132
export default {
127133
name: 'BucketsListPage',
@@ -131,16 +137,19 @@ export default {
131137
bucket: null,
132138
bucketDetail: {
133139
name: '',
134-
mode: 0,
140+
mode: 'private',
135141
full_token: '',
136142
read_token: '',
137-
credentials: {}
143+
credentials: {},
144+
objects: 0,
145+
size: 0,
146+
quota: 0,
138147
},
139148
tableKey: 0,
140149
list: null,
141150
currentPath: '/',
142151
total: 0,
143-
listLoading: true,
152+
listLoading: false,
144153
listQuery: {
145154
page: 1,
146155
limit: 20,
@@ -153,9 +162,20 @@ export default {
153162
},
154163
downloadLoading: false,
155164
uploadCommand: 'uploadFile',
156-
uploadFileList: []
165+
uploadFileList: [],
157166
}
158167
},
168+
computed: {
169+
bucketQuota() {
170+
return this.bucketDetail.quota ? byte2GbOrMb(this.bucketDetail.quota) : 0
171+
},
172+
bucketSize() {
173+
return this.bucketDetail.size ? byte2GbOrMb(this.bucketDetail.size) : 0
174+
},
175+
bucketObjects() {
176+
return this.bucketDetail.objects
177+
},
178+
},
159179
async created() {
160180
this.bucket = this.$route.params?.bucket
161181
@@ -168,21 +188,25 @@ export default {
168188
/**
169189
* 获取数据列表
170190
*/
171-
async getList() {
191+
getList(upload = false) {
192+
if (this.listLoading) return
172193
this.listLoading = true
173-
// 拼装查询条件 by this.listQuery
174-
// const { limit, page } = this.listQuery
175-
// 执行数据查询
176-
const res = await oss.getFilesByBucketName(this.bucket, {
177-
marker: undefined,
178-
prefix: this.currentPath,
179-
credentials: this.bucketDetail.credentials
180-
}).finally(() => { this.listLoading = false })
181194
182-
const files = res.Contents || []
183-
const dirs = res.CommonPrefixes || []
184-
this.list = [...files, ...dirs]
185-
this.listLoading = false
195+
const getList = async () => {
196+
const res = await oss.getFilesByBucketName(this.bucket, {
197+
marker: undefined,
198+
prefix: this.currentPath,
199+
credentials: this.bucketDetail.credentials
200+
}).finally(() => { this.listLoading = false })
201+
202+
const files = res.Contents || []
203+
const dirs = res.CommonPrefixes || []
204+
this.list = [...files, ...dirs]
205+
this.listLoading = false
206+
}
207+
208+
// 对多文件或文件夹上传做节流处理
209+
upload ? setTimeout(getList, 1000) : getList()
186210
},
187211
// 搜索
188212
handleFilter() {
@@ -253,19 +277,19 @@ export default {
253277
},
254278
handleUploadCommand(command) {
255279
this.dialogFormVisible = true
280+
this.uploadCommand = command
256281
if (command === 'uploadFolder') {
257-
this.uploadCommand = 'uploadFolder'
258282
this.$nextTick(() => {
259283
document.getElementsByClassName('el-upload__input')[0].webkitdirectory = true
260284
})
261285
} else {
262-
this.uploadCommand = 'uploadFile'
263286
this.$nextTick(() => {
264287
document.getElementsByClassName('el-upload__input')[0].webkitdirectory = false
265288
})
266289
}
267290
},
268-
async uploadFile(param) {
291+
async handleUploadFile(param) {
292+
console.log('upload file', param)
269293
const file = param.file
270294
const key = this.currentPath + (file.webkitRelativePath ? file.webkitRelativePath : file.name)
271295
const res = await oss.uploadAppFile(this.bucket, key, file, this.bucketDetail.credentials, { contentType: file.type })
@@ -274,7 +298,7 @@ export default {
274298
}
275299
276300
showSuccess('文件上传成功: ' + key)
277-
this.getList()
301+
this.getList(true)
278302
},
279303
// 判断是否为图片类型
280304
isImage(row) {
@@ -313,6 +337,12 @@ export default {
313337
</script>
314338
315339
<style scoped>
340+
.filter-container .tips {
341+
font-size: 14px; color: #333;position: absolute;right: 20px;
342+
}
343+
.filter-container .tips span {
344+
margin-right: 10px;
345+
}
316346
.thumb-image {
317347
width: 100px;
318348
max-height: 60px;

0 commit comments

Comments
 (0)