diff --git a/backend/src/api/core.js b/backend/src/api/core.js index 2d06335..709a712 100644 --- a/backend/src/api/core.js +++ b/backend/src/api/core.js @@ -1,3 +1,5 @@ +import { S3Client } from '@aws-sdk/client-s3' + function JsonResponse (json, status = 200, headers = {}) { return new Response(JSON.stringify(json), { headers: { @@ -7,8 +9,37 @@ function JsonResponse (json, status = 200, headers = {}) { 'access-control-allow-methods': '*', ...headers }, - status: status + status }) } -export { JsonResponse } +function getS3 (accountId, accessToken, secretToken) { + return new S3Client({ + region: 'auto', + endpoint: `https://${accountId}.r2.cloudflarestorage.com`, + credentials: { + + accessKeyId: `${accessToken}`, + secretAccessKey: `${secretToken}` + }, + accessKeyId: `${accessToken}`, + secretAccessKey: `${secretToken}`, + s3DisableBodySigning: false, + s3ForcePathStyle: true, + maxRetries: 2 + }) +} + +async function getS3ForEmail (env, email) { + let data = await env.WEB_DRIVE.get(email) + console.log(data) + if (!data) { + return null + } + + data = JSON.parse(data) + + return getS3(data.accountId, data.accessToken, data.secretToken) +} + +export { JsonResponse, getS3, getS3ForEmail } diff --git a/backend/src/api/createFolder.js b/backend/src/api/createFolder.js index 77e0e5f..bfb0aee 100644 --- a/backend/src/api/createFolder.js +++ b/backend/src/api/createFolder.js @@ -4,6 +4,7 @@ import { PutObjectCommand } from '@aws-sdk/client-s3' async function createFolder (request, env, context) { const body = await request.json() const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params console.log(body) diff --git a/backend/src/api/deleteObject.js b/backend/src/api/deleteObject.js index 3f99643..2ddefa1 100644 --- a/backend/src/api/deleteObject.js +++ b/backend/src/api/deleteObject.js @@ -4,6 +4,7 @@ import { DeleteObjectCommand } from '@aws-sdk/client-s3' async function deleteObject (request, env, context) { const body = await request.json() const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params const { name } = body diff --git a/backend/src/api/getDownloadUrl.js b/backend/src/api/getDownloadUrl.js index 11089e9..7433815 100644 --- a/backend/src/api/getDownloadUrl.js +++ b/backend/src/api/getDownloadUrl.js @@ -5,6 +5,7 @@ import { getSignedUrl } from '@aws-sdk/s3-request-presigner' async function getDownloadUrl (request, env, context) { const body = await request.json() const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params const { name } = body diff --git a/backend/src/api/getUploadUrl.js b/backend/src/api/getUploadUrl.js index 26a9c9c..e860be3 100644 --- a/backend/src/api/getUploadUrl.js +++ b/backend/src/api/getUploadUrl.js @@ -5,6 +5,7 @@ import { getSignedUrl } from '@aws-sdk/s3-request-presigner' async function getUploadUrl (request, env, context) { const body = await request.json() const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params const { name } = body diff --git a/backend/src/api/isRegisted.js b/backend/src/api/isRegisted.js new file mode 100644 index 0000000..281af78 --- /dev/null +++ b/backend/src/api/isRegisted.js @@ -0,0 +1,7 @@ +import { JsonResponse } from './core' + +async function isRegisted (request, env, context) { + return JsonResponse({ result: context.s3Client !== null }) +} + +export { isRegisted } diff --git a/backend/src/api/listContents.js b/backend/src/api/listContents.js index f265cd5..0058c4c 100644 --- a/backend/src/api/listContents.js +++ b/backend/src/api/listContents.js @@ -3,6 +3,7 @@ import { ListObjectsV2Command } from '@aws-sdk/client-s3' async function listContents (request, env, context) { const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params const { path } = request.query || '' diff --git a/backend/src/api/listDisks.js b/backend/src/api/listDisks.js index c27f2d1..5792791 100644 --- a/backend/src/api/listDisks.js +++ b/backend/src/api/listDisks.js @@ -3,10 +3,12 @@ import { ListBucketsCommand } from '@aws-sdk/client-s3' 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 data = await s3Client.send(new ListBucketsCommand({})) - return JsonResponse(data, data.$metadata.httpStatusCode) + return JsonResponse({ user: email, ...data }, data.$metadata.httpStatusCode) } export { listDisks } diff --git a/backend/src/api/register.js b/backend/src/api/register.js new file mode 100644 index 0000000..1295a81 --- /dev/null +++ b/backend/src/api/register.js @@ -0,0 +1,26 @@ +import { getS3, JsonResponse } from './core' +import { ListBucketsCommand } from '@aws-sdk/client-s3' + +async function registerEmail (request, env, context) { + const email = request.headers.get('Cf-Access-Authenticated-User-Email') || 'g4bryrm98@gmail.com' + const body = await request.json() + const { accountId, accessToken, secretToken } = body + + const s3Client = getS3(accountId, accessToken, secretToken) + console.log(body) + // Test the credentials + try { + await s3Client.send(new ListBucketsCommand({})) + } catch (err) { + console.log(err) + return JsonResponse({ result: 'unauthorized' }, 401) + } + + await env.WEB_DRIVE.put(email, JSON.stringify({ + accountId, accessToken, secretToken + })) + + return JsonResponse({ result: 'success' }) +} + +export { registerEmail } diff --git a/backend/src/api/renameObject.js b/backend/src/api/renameObject.js index b747450..ff7c2bf 100644 --- a/backend/src/api/renameObject.js +++ b/backend/src/api/renameObject.js @@ -4,6 +4,7 @@ import { CopyObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3' async function renameObject (request, env, context) { const body = await request.json() const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params const { path } = body diff --git a/backend/src/api/uploadFiles.js b/backend/src/api/uploadFiles.js index fcd5600..79e9a8e 100644 --- a/backend/src/api/uploadFiles.js +++ b/backend/src/api/uploadFiles.js @@ -4,6 +4,7 @@ import { PutObjectCommand } from '@aws-sdk/client-s3' async function uploadFiles (request, env, context) { const form = await request.formData() const { s3Client } = context + if (s3Client === null) return JsonResponse('unauthorized', 401) const { disk } = request.params // const path = form.get('path') diff --git a/backend/src/config.js b/backend/src/config.js index 042a2be..1a19b88 100644 --- a/backend/src/config.js +++ b/backend/src/config.js @@ -4,10 +4,7 @@ const config = { 'access-control-allow-headers': '*', 'access-control-allow-methods': '*', 'timing-allow-origin': '*' - }, - accountid: '0983f9c21d0167d8d677be145016932e', - access_key_id: '112326db443e709a2e6ea23bc2af7754', - access_key_secret: 'e6892b8890a2e4d889eecf903bf8e476fa2f6d3f460f3286684c7d9dd39cfb47' + } } // eslint-disable-next-line no-undef diff --git a/backend/src/index.js b/backend/src/index.js index 0e19751..e54c102 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -1,6 +1,5 @@ import { Router } from 'itty-router' import { config } from './config' -import { S3Client } from '@aws-sdk/client-s3' import { listDisks } from './api/listDisks' import { listContents } from './api/listContents' import { getDownloadUrl } from './api/getDownloadUrl' @@ -8,9 +7,15 @@ import { uploadFiles } from './api/uploadFiles' import { createFolder } from './api/createFolder' import { deleteObject } from './api/deleteObject' import { renameObject } from './api/renameObject' +import { registerEmail } from './api/register' +import { isRegisted } from './api/isRegisted' +import { getS3ForEmail } from './api/core' const router = Router() +router.get('/api/is-registed', isRegisted) +router.post('/api/register', registerEmail) + router.get('/api/disks', listDisks) router.get('/api/disks/:disk', listContents) router.post('/api/disks/:disk/rename', renameObject) @@ -33,20 +38,7 @@ router.all('*', () => new Response('404, not found!', { status: 404 })) export default { async fetch (request, env, context) { - const s3Client = new S3Client({ - region: 'auto', - endpoint: `https://${config.accountid}.r2.cloudflarestorage.com`, - credentials: { - - accessKeyId: `${config.access_key_id}`, - secretAccessKey: `${config.access_key_secret}` - }, - accessKeyId: `${config.access_key_id}`, - secretAccessKey: `${config.access_key_secret}`, - s3DisableBodySigning: false, - s3ForcePathStyle: true, - maxRetries: 2 - }) + const s3Client = await getS3ForEmail(env, request.headers.get('Cf-Access-Authenticated-User-Email') || 'g4bryrm98@gmail.com') return router .handle(request, env, { ...context, s3Client }) diff --git a/backend/wrangler.toml b/backend/wrangler.toml index 4bec366..b47a3b8 100644 --- a/backend/wrangler.toml +++ b/backend/wrangler.toml @@ -1,3 +1,7 @@ -name = "Drive Backend" +name = "drive" main = "src/index.js" compatibility_date = "2022-06-21" + +kv_namespaces = [ + { binding = "WEB_DRIVE", id = "4355f4de9a624466867c8a2c1dce32df", preview_id = "a4d4a08e61cc4ce884f4869f67378c7a" } +] diff --git a/src/components/base/TopbarView.vue b/src/components/base/TopbarView.vue index 4c927f5..5322e7e 100644 --- a/src/components/base/TopbarView.vue +++ b/src/components/base/TopbarView.vue @@ -4,10 +4,10 @@
diff --git a/src/main.js b/src/main.js index 16812fc..be2665b 100644 --- a/src/main.js +++ b/src/main.js @@ -25,5 +25,3 @@ app.use(router) app.use(VueToast) app.mount('#app') - -store.dispatch('loadUserDisks') diff --git a/src/router/index.js b/src/router/index.js index 3648935..ee2c341 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -3,12 +3,12 @@ import HomeView from '../views/HomeView.vue' const routes = [ { - path: '/', + path: '/explorer', name: 'home', component: HomeView }, { - path: '/login', + path: '/', name: 'login', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index c516d70..91d1c7d 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -65,6 +65,7 @@ import Swal from 'sweetalert2' import Gallery from '@/components/Gallery' import DragAndDrop from '@/components/DragAndDrop' import repo from '@/repo' + export default { components: { DragAndDrop, Gallery }, methods: { @@ -96,6 +97,9 @@ export default { } }) } + }, + created () { + this.$store.dispatch('loadUserDisks') } } diff --git a/src/views/LoginView.vue b/src/views/LoginView.vue index 990434a..2821b29 100644 --- a/src/views/LoginView.vue +++ b/src/views/LoginView.vue @@ -1,10 +1,43 @@ +