add action to create new buckets

This commit is contained in:
Gabriel Massadas
2022-07-09 16:07:44 +01:00
parent bb41f248e3
commit 1c04ffc055
6 changed files with 69 additions and 14 deletions
+18
View File
@@ -0,0 +1,18 @@
import { JsonResponse } from './core'
import { CreateBucketCommand } from '@aws-sdk/client-s3'
async function createDisk (request, env, context) {
const { s3Client } = context
if (s3Client === null) return JsonResponse('unauthorized', 401)
const { disk } = request.params
const command = new CreateBucketCommand({
Bucket: disk
})
const data = await s3Client.send(command)
return JsonResponse(data, data.$metadata.httpStatusCode)
}
export { createDisk }
+1 -1
View File
@@ -5,7 +5,7 @@ async function listDisks (request, env, context) {
const { s3Client } = context
if (s3Client === null) return JsonResponse('unauthorized', 401)
const email = request.headers.get('Cf-Access-Authenticated-User-Email')
const email = request.headers.get('Cf-Access-Authenticated-User-Email') || 'unknown user'
const data = await s3Client.send(new ListBucketsCommand({}))
return JsonResponse({ user: email, ...data }, data.$metadata.httpStatusCode)
+2
View File
@@ -11,6 +11,7 @@ import { registerEmail } from './api/register'
import { isRegisted } from './api/isRegisted'
import { downloadFile } from './api/downloadFile'
import { getS3ForEmail } from './api/core'
import { createDisk } from './api/createDisk'
const router = Router()
@@ -18,6 +19,7 @@ router.get('/api/is-registed', isRegisted)
router.post('/api/register', registerEmail)
router.get('/api/disks', listDisks)
router.post('/api/disks/:disk', createDisk)
router.get('/api/disks/:disk', listContents)
router.post('/api/disks/:disk/rename', renameObject)
router.post('/api/disks/:disk/folder', createFolder)
+1 -1
View File
@@ -12,7 +12,7 @@
</a>
<div class="dropdown-menu dropdown-menu-end profile-dropdown ">
<!-- item-->
<a href="javascript:void(0);" class="dropdown-item notify-item">
<a href="/cdn-cgi/access/logout" class="dropdown-item notify-item">
<i class="bi bi-asterisk"></i>
<span>Logout</span>
</a>
+18 -12
View File
@@ -9,6 +9,9 @@ export default {
path: folderPath
})
},
createDisk: (name) => {
return axios.post(`/api/disks/${name}`)
},
deleteObject: (path, name) => {
return axios.post(`/api/disks/${store.state.activeBucket}/delete`, {
name,
@@ -53,19 +56,22 @@ export default {
}
})
let files = response.data.Contents.filter(function (obj) {
return !obj.Key.endsWith('/')
})
files = files.map(function (obj) {
const name = obj.Key.replace(store.state.currentFolder, '')
let files = []
if (response.data.Contents) {
files = response.data.Contents.filter(function (obj) {
return !obj.Key.endsWith('/')
})
files = files.map(function (obj) {
const name = obj.Key.replace(store.state.currentFolder, '')
return {
...obj,
name,
path: store.state.currentFolder,
extension: name.split('.').pop()
}
})
return {
...obj,
name,
path: store.state.currentFolder,
extension: name.split('.').pop()
}
})
}
let folders = []
if (response.data.CommonPrefixes) {
+29
View File
@@ -25,6 +25,7 @@
<div class="dropdown-menu">
<a class="dropdown-item pointer" @click="newFolder"><i class="bi bi-folder-fill me-1"></i> Folder</a>
<a class="dropdown-item pointer" @click="$refs.uploader.openUploader()"><i class="bi bi-file-earmark-text-fill me-1"></i> File</a>
<a class="dropdown-item pointer" @click="newDisk"><i class="bi bi-bucket-fill me-1"></i> Disk/Bucket</a>
</div>
</div>
<div class="mail-list mt-3">
@@ -93,6 +94,34 @@ export default {
)
}
})
},
newDisk () {
const self = this
Swal.fire({
title: 'New Disk name',
input: 'text',
showCancelButton: true,
inputValidator: (value) => {
if (!value) {
return 'You need to write something!'
}
}
}).then((data) => {
if (data.isConfirmed === true) {
repo.createDisk(data.value).then((resp) => {
self.$toast.open({
message: 'Disk created!',
type: 'success'
})
self.$store.dispatch('loadUserDisks')
setTimeout(function () {
self.$store.commit('changeBucket', data.value)
}, 500)
}
)
}
})
}
},
created () {