🔨 Add delete for cloud

This commit is contained in:
SebClem 2022-11-14 14:23:15 +01:00
parent 07235fda1d
commit bff494252b
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
14 changed files with 219 additions and 54 deletions

View File

@ -0,0 +1,11 @@
{
"folders": [
{
"path": "backend"
},
{
"path": "frontend"
}
],
"settings": {}
}

View File

@ -1,51 +1,63 @@
import express from "express"; import express from "express";
import Joi from "joi";
import { import {
getWebdavConfig, getWebdavConfig,
validateWebdavConfig, validateWebdavConfig,
} from "../services/webdavConfigService.js"; } from "../services/webdavConfigService.js";
import * as webdavService from "../services/webdavService.js"; import * as webdavService from "../services/webdavService.js";
import * as pathTools from "../tools/pathTools.js"; import * as pathTools from "../tools/pathTools.js";
import type { WebdavDelete } from "../types/services/webdav.js";
import { WebdavDeleteValidation } from "../types/services/webdavValidation.js";
const webdavRouter = express.Router(); const webdavRouter = express.Router();
webdavRouter.get("/backup/auto", (req, res, next) => { webdavRouter.get("/backup/auto", (req, res, next) => {
const config = getWebdavConfig(); const config = getWebdavConfig();
validateWebdavConfig(config) validateWebdavConfig(config)
.then(() => { .then(async () => {
webdavService const value = await webdavService
.getBackups(pathTools.auto, config) .getBackups(pathTools.auto, config);
.then((value) => {
res.json(value); res.json(value);
}) })
.catch((reason) => { .catch((reason) => {
res.status(500); res.status(500);
res.json(reason); res.json(reason);
}); });
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
}); });
webdavRouter.get("/backup/manual", (req, res, next) => { webdavRouter.get("/backup/manual", (req, res, next) => {
const config = getWebdavConfig(); const config = getWebdavConfig();
validateWebdavConfig(config) validateWebdavConfig(config)
.then(() => { .then(async () => {
webdavService const value = await webdavService
.getBackups(pathTools.manual, config) .getBackups(pathTools.manual, config);
.then((value) => {
res.json(value); res.json(value);
}) })
.catch((reason) => { .catch((reason) => {
res.status(500); res.status(500);
res.json(reason); res.json(reason);
}); });
}) });
.catch((reason) => {
res.status(500); webdavRouter.delete("/", (req, res, next) => {
res.json(reason); const body: WebdavDelete = req.body;
}); const validator = Joi.object(WebdavDeleteValidation);
const config = getWebdavConfig();
validateWebdavConfig(config).then(() => {
validator
.validateAsync(body)
.then(() => {
webdavService.deleteBackup(body.path, config)
.then(()=>{
res.status(201).send();
}).catch((reason)=>{
res.status(500).json(reason);
});
})
.catch((reason) => {
res.status(400).json(reason);
});
});
}); });
export default webdavRouter; export default webdavRouter;

View File

@ -2,20 +2,19 @@ import fs from "fs";
import Joi from "joi"; import Joi from "joi";
import logger from "../config/winston.js"; import logger from "../config/winston.js";
import { default_root } from "../tools/pathTools.js"; import { default_root } from "../tools/pathTools.js";
import WebdavConfigValidation from "../types/services/webdavConfigValidation.js";
import { import {
WebdavConfig, WebdavConfig,
WebdavEndpointType, WebdavEndpointType
} from "../types/services/webdavConfig.js"; } from "../types/services/webdavConfig.js";
import WebdavConfigValidation from "../types/services/webdavConfigValidation.js";
const webdavConfigPath = "/data/webdavConfigV2.json"; const webdavConfigPath = "/data/webdavConfigV2.json";
const NEXTCLOUD_ENDPOINT = "/remote.php/dav/files/$username"; const NEXTCLOUD_ENDPOINT = "/remote.php/dav/files/$username";
export function validateWebdavConfig(config: WebdavConfig) { export function validateWebdavConfig(config: WebdavConfig) {
const validator = Joi.object(WebdavConfigValidation); const validator = Joi.object(WebdavConfigValidation);
return validator.validateAsync(config, { return validator.validateAsync(config, {
abortEarly: false abortEarly: false,
}); });
} }
@ -35,16 +34,22 @@ export function getWebdavConfig(): WebdavConfig {
} }
export function getEndpoint(config: WebdavConfig) { export function getEndpoint(config: WebdavConfig) {
let endpoint: string;
if (config.webdavEndpoint.type == WebdavEndpointType.NEXTCLOUD) { if (config.webdavEndpoint.type == WebdavEndpointType.NEXTCLOUD) {
return NEXTCLOUD_ENDPOINT.replace("$username", config.username); endpoint = NEXTCLOUD_ENDPOINT.replace("$username", config.username);
} else if (config.webdavEndpoint.customEndpoint) { } else if (config.webdavEndpoint.customEndpoint) {
return config.webdavEndpoint.customEndpoint.replace( endpoint = config.webdavEndpoint.customEndpoint.replace(
"$username", "$username",
config.username config.username
); );
} else { } else {
return ""; return "";
} }
if (endpoint.endsWith("/")) {
return endpoint.slice(0, -1);
}
return endpoint;
} }
export function getWebdavDefaultConfig(): WebdavConfig { export function getWebdavDefaultConfig(): WebdavConfig {

View File

@ -107,7 +107,7 @@ export function getBackups(folder: string, config: WebdavConfig) {
body: PROPFIND_BODY, body: PROPFIND_BODY,
}).then( }).then(
(value) => { (value) => {
return parseXmlBackupData(value.body); return parseXmlBackupData(value.body, config);
}, },
(reason) => { (reason) => {
messageManager.error( messageManager.error(
@ -120,7 +120,28 @@ export function getBackups(folder: string, config: WebdavConfig) {
); );
} }
function parseXmlBackupData(body: string) { export function deleteBackup(path: string, config: WebdavConfig){
const endpoint = getEndpoint(config);
return got.delete(config.url + endpoint + path, {
headers: {
authorization:
"Basic " +
Buffer.from(config.username + ":" + config.password).toString("base64")
}
}).then(
(response) => {
return response;
},
(reason) => {
messageManager.error("Fail to delete backup in webdav", reason?.message);
logger.error(`Fail to delete backup in Cloud`);
logger.error(reason);
return Promise.reject(reason);
}
);
}
function parseXmlBackupData(body: string, config: WebdavConfig) {
const parser = new XMLParser(); const parser = new XMLParser();
const data = parser.parse(body); const data = parser.parse(body);
const multistatus = data["d:multistatus"]; const multistatus = data["d:multistatus"];
@ -141,6 +162,7 @@ function parseXmlBackupData(body: string) {
lastEdit: lastEdit, lastEdit: lastEdit,
size: propstat["d:prop"]["d:getcontentlength"], size: propstat["d:prop"]["d:getcontentlength"],
name: name, name: name,
path: href.replace(getEndpoint(config), "")
}); });
} }
} }

View File

@ -5,4 +5,10 @@ export interface WebdavBackup {
name: string; name: string;
size: number; size: number;
lastEdit: DateTime; lastEdit: DateTime;
path: string;
}
export interface WebdavDelete {
path: string;
} }

View File

@ -1,4 +1,4 @@
import Joi, { not } from "joi"; import Joi from "joi";
import { WebdavEndpointType } from "./webdavConfig.js"; import { WebdavEndpointType } from "./webdavConfig.js";

View File

@ -0,0 +1,5 @@
import Joi from "joi";
export const WebdavDeleteValidation = {
path: Joi.string().not().empty().required()
}

View File

@ -20,7 +20,7 @@
"pretty-bytes": "^6.0.0", "pretty-bytes": "^6.0.0",
"roboto-fontface": "*", "roboto-fontface": "*",
"vue": "^3.2.40", "vue": "^3.2.40",
"vuetify": "3.0.0-beta.15", "vuetify": "3.0.0",
"webfontloader": "^1.0.0" "webfontloader": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -25,7 +25,7 @@ specifiers:
vite-plugin-vuetify: ^1.0.0-alpha.12 vite-plugin-vuetify: ^1.0.0-alpha.12
vue: ^3.2.40 vue: ^3.2.40
vue-tsc: 1.0.7 vue-tsc: 1.0.7
vuetify: 3.0.0-beta.15 vuetify: 3.0.0
webfontloader: ^1.0.0 webfontloader: ^1.0.0
dependencies: dependencies:
@ -38,7 +38,7 @@ dependencies:
pretty-bytes: 6.0.0 pretty-bytes: 6.0.0
roboto-fontface: 0.10.0 roboto-fontface: 0.10.0
vue: 3.2.40 vue: 3.2.40
vuetify: 3.0.0-beta.15_vdkwhj2kz5kpysy3rwb432nm3y vuetify: 3.0.0_vdkwhj2kz5kpysy3rwb432nm3y
webfontloader: 1.6.28 webfontloader: 1.6.28
devDependencies: devDependencies:
@ -55,7 +55,7 @@ devDependencies:
prettier: 2.7.1 prettier: 2.7.1
typescript: 4.7.4 typescript: 4.7.4
vite: 3.1.7 vite: 3.1.7
vite-plugin-vuetify: 1.0.0-alpha.17_g64smqxqrz57uhsly7clskj5fy vite-plugin-vuetify: 1.0.0-alpha.17_al4u2thft73kbehmgonig46c5m
vue-tsc: 1.0.7_typescript@4.7.4 vue-tsc: 1.0.7_typescript@4.7.4
packages: packages:
@ -484,7 +484,7 @@ packages:
'@types/node': 16.11.65 '@types/node': 16.11.65
dev: true dev: true
/@vuetify/loader-shared/1.6.0_l2xw63bflzffxrhpro723k3mhy: /@vuetify/loader-shared/1.6.0_vue@3.2.40+vuetify@3.0.0:
resolution: {integrity: sha512-mRvswe5SIecagmKkL1c0UQx5V9ACKLyEGegOOe5vC3ccFH8rMbyI4AVZaAKm/EnGXKirWJ1umL8RQFcGd+C0tw==} resolution: {integrity: sha512-mRvswe5SIecagmKkL1c0UQx5V9ACKLyEGegOOe5vC3ccFH8rMbyI4AVZaAKm/EnGXKirWJ1umL8RQFcGd+C0tw==}
peerDependencies: peerDependencies:
vue: ^3.0.0 vue: ^3.0.0
@ -493,7 +493,7 @@ packages:
find-cache-dir: 3.3.2 find-cache-dir: 3.3.2
upath: 2.0.1 upath: 2.0.1
vue: 3.2.40 vue: 3.2.40
vuetify: 3.0.0-beta.15_vdkwhj2kz5kpysy3rwb432nm3y vuetify: 3.0.0_vdkwhj2kz5kpysy3rwb432nm3y
/acorn-jsx/5.3.2_acorn@8.8.0: /acorn-jsx/5.3.2_acorn@8.8.0:
resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==}
@ -2157,18 +2157,18 @@ packages:
spdx-expression-parse: 3.0.1 spdx-expression-parse: 3.0.1
dev: true dev: true
/vite-plugin-vuetify/1.0.0-alpha.17_g64smqxqrz57uhsly7clskj5fy: /vite-plugin-vuetify/1.0.0-alpha.17_al4u2thft73kbehmgonig46c5m:
resolution: {integrity: sha512-lP4vG+Z3LnIEjI8nSE8/MJwDRQK5OnS2i4zHDu36yyD4E7wmPKyIkWJdkfBIsx/O+PU7dGc/3MeLARgr+RErEQ==} resolution: {integrity: sha512-lP4vG+Z3LnIEjI8nSE8/MJwDRQK5OnS2i4zHDu36yyD4E7wmPKyIkWJdkfBIsx/O+PU7dGc/3MeLARgr+RErEQ==}
engines: {node: '>=12'} engines: {node: '>=12'}
peerDependencies: peerDependencies:
vite: ^2.7.0 || ^3.0.0 vite: ^2.7.0 || ^3.0.0
vuetify: ^3.0.0-beta.4 vuetify: ^3.0.0-beta.4
dependencies: dependencies:
'@vuetify/loader-shared': 1.6.0_l2xw63bflzffxrhpro723k3mhy '@vuetify/loader-shared': 1.6.0_vue@3.2.40+vuetify@3.0.0
debug: 4.3.4 debug: 4.3.4
upath: 2.0.1 upath: 2.0.1
vite: 3.1.7 vite: 3.1.7
vuetify: 3.0.0-beta.15_vdkwhj2kz5kpysy3rwb432nm3y vuetify: 3.0.0_vdkwhj2kz5kpysy3rwb432nm3y
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
- vue - vue
@ -2259,8 +2259,8 @@ packages:
'@vue/server-renderer': 3.2.40_vue@3.2.40 '@vue/server-renderer': 3.2.40_vue@3.2.40
'@vue/shared': 3.2.40 '@vue/shared': 3.2.40
/vuetify/3.0.0-beta.15_vdkwhj2kz5kpysy3rwb432nm3y: /vuetify/3.0.0_vdkwhj2kz5kpysy3rwb432nm3y:
resolution: {integrity: sha512-Tw4StO4JJxwzN7RIAJ+nBVTaftlk3TqfqCtMLwJY/SO/ciVru+sHtahOpXIbp8B43d/DIRql3mfnrz1ooTSXtQ==} resolution: {integrity: sha512-0olLmKWb+oTaebGTM02+1fWaAgnGDuIt86K3jDv2LnALQ2atSzhDS1SSASJsJCZ2PgFBdDAeJVLcNWWavRYAlQ==}
engines: {node: ^12.20 || >=14.13} engines: {node: ^12.20 || >=14.13}
peerDependencies: peerDependencies:
vite-plugin-vuetify: ^1.0.0-alpha.12 vite-plugin-vuetify: ^1.0.0-alpha.12
@ -2275,7 +2275,7 @@ packages:
webpack-plugin-vuetify: webpack-plugin-vuetify:
optional: true optional: true
dependencies: dependencies:
vite-plugin-vuetify: 1.0.0-alpha.17_g64smqxqrz57uhsly7clskj5fy vite-plugin-vuetify: 1.0.0-alpha.17_al4u2thft73kbehmgonig46c5m
vue: 3.2.40 vue: 3.2.40
/webfontloader/1.6.28: /webfontloader/1.6.28:

View File

@ -0,0 +1,72 @@
<template>
<div>
<v-dialog
v-model="dialog"
:width="width"
:fullscreen="xs"
:persistent="loading"
>
<v-card>
<v-card-title class="d-flex align-center">
<v-icon color="red" class="mr-2">mdi-trash-can</v-icon> Delete Cloud
backup
</v-card-title>
<v-divider></v-divider>
<v-card-text>
Delete <v-code tag="code">{{ item?.name }}</v-code> backup in cloud ?
</v-card-text>
<v-divider></v-divider>
<v-card-actions class="justify-end">
<v-btn color="success" @click="dialog = false" :disabled="loading">
Close
</v-btn>
<v-btn color="red" @click="confirm()" :loading="loading">
<v-icon>mdi-trash-can</v-icon> Delete
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div>
</template>
<script setup lang="ts">
import { deleteWebdabBackup } from "@/services/webdavService";
import type { WebdavBackup } from "@/types/webdav";
import { computed, ref } from "vue";
import { useDisplay } from "vuetify/dist/vuetify";
const { xs, mdAndDown } = useDisplay();
const dialog = ref(false);
const loading = ref(false);
const item = ref<WebdavBackup | null>(null);
const width = computed(() => {
if (xs.value) {
return undefined;
} else if (mdAndDown.value) {
return "80%";
} else {
return "50%";
}
});
function confirm() {
loading.value = true;
if (item.value) {
deleteWebdabBackup(item.value?.path)
.then(() => {
loading.value = false;
dialog.value = false;
})
.catch(() => {
loading.value = false;
});
}
}
function open(value: WebdavBackup) {
item.value = value;
dialog.value = true;
}
defineExpose({ open });
</script>

View File

@ -21,6 +21,7 @@
:key="item.id" :key="item.id"
:item="item" :item="item"
:index="index" :index="index"
@delete="deleteBackup"
> >
</cloud-list-item> </cloud-list-item>
</v-list> </v-list>
@ -45,6 +46,7 @@
:key="item.id" :key="item.id"
:item="item" :item="item"
:index="index" :index="index"
@delete="deleteBackup"
> >
</cloud-list-item> </cloud-list-item>
</v-list> </v-list>
@ -54,20 +56,22 @@
</v-row> </v-row>
</v-card-text> </v-card-text>
</v-card> </v-card>
<cloud-delete-dialog ref="deleteDialog"></cloud-delete-dialog>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref } from "vue"; import { onBeforeUnmount, ref } from "vue";
import type { WebdavBackup } from "@/types/webdav"; import type { WebdavBackup } from "@/types/webdav";
import { import {
getAutoBackupList, getAutoBackupList,
getManualBackupList, getManualBackupList,
} from "@/services/webdavService"; } from "@/services/webdavService";
import CloudDeleteDialog from "./CloudDeleteDialog.vue";
import CloudListItem from "./CloudListItem.vue"; import CloudListItem from "./CloudListItem.vue";
const popup = ref(false); const deleteDialog = ref<InstanceType<typeof CloudDeleteDialog> | null>(null);
const deleteItem = ref<WebdavBackup | null>(null);
const autoBackups = ref<WebdavBackup[]>([]); const autoBackups = ref<WebdavBackup[]>([]);
const manualBackups = ref<WebdavBackup[]>([]); const manualBackups = ref<WebdavBackup[]>([]);
function refreshBackup() { function refreshBackup() {
@ -78,5 +82,16 @@ function refreshBackup() {
manualBackups.value = value; manualBackups.value = value;
}); });
} }
function deleteBackup(item: WebdavBackup) {
deleteItem.value = item;
deleteDialog.value?.open(item);
}
refreshBackup(); refreshBackup();
const interval = setInterval(refreshBackup, 2000);
onBeforeUnmount(() => {
clearInterval(interval);
});
</script> </script>

View File

@ -1,27 +1,25 @@
<template> <template>
<v-divider v-if="index != 0" color="grey-darken-3"></v-divider> <v-divider v-if="index != 0" color="grey-darken-3"></v-divider>
<v-list-item> <v-list-item>
<v-list-item-title class="text-deep-orange-darken-3">{{ <v-list-item-title>{{ item.name }}</v-list-item-title>
item.name
}}</v-list-item-title>
<template v-slot:append> <template v-slot:append>
<v-scroll-x-transition> <v-scroll-x-transition>
<v-chip <v-chip
color="primary" color="primary"
variant="flat" variant="flat"
size="small" size="small"
class="mr-2" class="mr-1"
v-show="!detail" v-show="!detail"
> >
{{ {{
DateTime.fromISO(item.lastEdit).toLocaleString( DateTime.fromISO(item.lastEdit).toLocaleString(
DateTime.DATETIME_SHORT DateTime.DATETIME_MED
) )
}} }}
</v-chip> </v-chip>
</v-scroll-x-transition> </v-scroll-x-transition>
<v-btn variant="text" icon color="secondary" @click="detail = !detail"> <v-btn variant="text" icon color="success" @click="detail = !detail">
<v-icon>{{ detail ? "mdi-chevron-up" : "mdi-information" }}</v-icon> <v-icon>{{ detail ? "mdi-chevron-up" : "mdi-information" }}</v-icon>
</v-btn> </v-btn>
</template> </template>
@ -33,7 +31,7 @@
<v-icon start icon="mdi-pencil"></v-icon> <v-icon start icon="mdi-pencil"></v-icon>
{{ {{
DateTime.fromISO(item.lastEdit).toLocaleString( DateTime.fromISO(item.lastEdit).toLocaleString(
DateTime.DATETIME_SHORT DateTime.DATETIME_MED
) )
}} }}
</v-chip> </v-chip>
@ -51,7 +49,7 @@
</v-btn> </v-btn>
</template> </template>
</v-tooltip> </v-tooltip>
<v-btn variant="outlined" color="red"> <v-btn variant="outlined" color="red" @click="emits('delete', item)">
<v-icon>mdi-trash-can</v-icon> <v-icon>mdi-trash-can</v-icon>
</v-btn> </v-btn>
</v-card-actions> </v-card-actions>
@ -61,8 +59,8 @@
<script setup lang="ts"> <script setup lang="ts">
import type { WebdavBackup } from "@/types/webdav"; import type { WebdavBackup } from "@/types/webdav";
import prettyBytes from "pretty-bytes";
import { DateTime } from "luxon"; import { DateTime } from "luxon";
import prettyBytes from "pretty-bytes";
import { ref } from "vue"; import { ref } from "vue";
const detail = ref(false); const detail = ref(false);
@ -70,4 +68,8 @@ defineProps<{
item: WebdavBackup; item: WebdavBackup;
index: number; index: number;
}>(); }>();
const emits = defineEmits<{
(e: "delete", item: WebdavBackup): void;
}>();
</script> </script>

View File

@ -8,3 +8,13 @@ export function getAutoBackupList() {
export function getManualBackupList() { export function getManualBackupList() {
return kyClient.get("webdav/backup/manual").json<WebdavBackup[]>(); return kyClient.get("webdav/backup/manual").json<WebdavBackup[]>();
} }
export function deleteWebdabBackup(path: string) {
return kyClient
.delete("webdav", {
json: {
path: path,
},
})
.text();
}

View File

@ -5,4 +5,9 @@ export interface WebdavBackup {
name: string; name: string;
size: number; size: number;
lastEdit: string; lastEdit: string;
path: string;
}
export interface WebdavDeletePayload {
path: string;
} }