feat: 增加回收站开关功能 (#3161)

Refs https://github.com/1Panel-dev/1Panel/issues/2895
This commit is contained in:
zhengkunwang 2023-12-04 14:56:09 +08:00 committed by GitHub
parent 9658ebdfdb
commit d60338f350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 184 additions and 13 deletions

View File

@ -68,3 +68,19 @@ func (b *BaseApi) ClearRecycleBinFile(c *gin.Context) {
}
helper.SuccessWithOutData(c)
}
// @Tags File
// @Summary Get RecycleBin status
// @Description 获取回收站状态
// @Accept json
// @Success 200
// @Security ApiKeyAuth
// @Router /files/recycle/status [get]
func (b *BaseApi) GetRecycleStatus(c *gin.Context) {
settingInfo, err := settingService.GetSettingInfo()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, settingInfo.FileRecycleBin)
}

View File

@ -50,6 +50,8 @@ type SettingInfo struct {
AppStoreVersion string `json:"appStoreVersion"`
AppStoreLastModified string `json:"appStoreLastModified"`
AppStoreSyncStatus string `json:"appStoreSyncStatus"`
FileRecycleBin string `json:"fileRecycleBin"`
}
type SettingUpdate struct {

View File

@ -136,6 +136,10 @@ func (f *FileService) Create(op request.FileCreate) error {
func (f *FileService) Delete(op request.FileDelete) error {
fo := files.NewFileOp()
recycleBinStatus, _ := settingRepo.Get(settingRepo.WithByKey("FileRecycleBin"))
if recycleBinStatus.Value == "disable" {
op.ForceDelete = true
}
if op.ForceDelete {
if op.IsDir {
return fo.DeleteDir(op.Path)

View File

@ -59,6 +59,7 @@ func Init() {
migrations.AddDockerSockPath,
migrations.AddDatabaseSSL,
migrations.AddDefaultCA,
migrations.AddSettingRecycleBin,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)

View File

@ -77,3 +77,13 @@ var AddDefaultCA = &gormigrate.Migration{
return nil
},
}
var AddSettingRecycleBin = &gormigrate.Migration{
ID: "20231129-add-setting-recycle-bin",
Migrate: func(tx *gorm.DB) error {
if err := tx.Create(&model.Setting{Key: "FileRecycleBin", Value: "enable"}).Error; err != nil {
return err
}
return nil
},
}

View File

@ -43,6 +43,7 @@ func (f *FileRouter) InitFileRouter(Router *gin.RouterGroup) {
fileRouter.POST("/recycle/search", baseApi.SearchRecycleBinFile)
fileRouter.POST("/recycle/reduce", baseApi.ReduceRecycleBinFile)
fileRouter.POST("/recycle/clear", baseApi.ClearRecycleBinFile)
fileRouter.GET("/recycle/status", baseApi.GetRecycleStatus)
fileRouter.POST("/favorite/search", baseApi.SearchFavorite)
fileRouter.POST("/favorite", baseApi.CreateFavorite)

View File

@ -1,5 +1,5 @@
// Code generated by swaggo/swag. DO NOT EDIT.
// Package docs GENERATED BY SWAG; DO NOT EDIT
// This file was generated by swaggo/swag
package docs
import "github.com/swaggo/swag"
@ -5919,6 +5919,28 @@ const docTemplate = `{
}
}
},
"/files/recycle/status": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "获取回收站状态",
"consumes": [
"application/json"
],
"tags": [
"File"
],
"summary": "Get RecycleBin status",
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/files/rename": {
"post": {
"security": [
@ -13506,6 +13528,9 @@ const docTemplate = `{
"names"
],
"properties": {
"force": {
"type": "boolean"
},
"names": {
"type": "array",
"items": {
@ -16822,6 +16847,9 @@ const docTemplate = `{
"expirationTime": {
"type": "string"
},
"fileRecycleBin": {
"type": "string"
},
"ipv6": {
"type": "string"
},
@ -19035,6 +19063,9 @@ const docTemplate = `{
"autoRenew": {
"type": "boolean"
},
"description": {
"type": "string"
},
"dir": {
"type": "string"
},
@ -19652,6 +19683,9 @@ const docTemplate = `{
"certificatePath": {
"type": "string"
},
"description": {
"type": "string"
},
"privateKey": {
"type": "string"
},

View File

@ -5912,6 +5912,28 @@
}
}
},
"/files/recycle/status": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "获取回收站状态",
"consumes": [
"application/json"
],
"tags": [
"File"
],
"summary": "Get RecycleBin status",
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/files/rename": {
"post": {
"security": [
@ -13499,6 +13521,9 @@
"names"
],
"properties": {
"force": {
"type": "boolean"
},
"names": {
"type": "array",
"items": {
@ -16815,6 +16840,9 @@
"expirationTime": {
"type": "string"
},
"fileRecycleBin": {
"type": "string"
},
"ipv6": {
"type": "string"
},
@ -19028,6 +19056,9 @@
"autoRenew": {
"type": "boolean"
},
"description": {
"type": "string"
},
"dir": {
"type": "string"
},
@ -19645,6 +19676,9 @@
"certificatePath": {
"type": "string"
},
"description": {
"type": "string"
},
"privateKey": {
"type": "string"
},

View File

@ -95,6 +95,8 @@ definitions:
type: object
dto.BatchDelete:
properties:
force:
type: boolean
names:
items:
type: string
@ -2334,6 +2336,8 @@ definitions:
type: string
expirationTime:
type: string
fileRecycleBin:
type: string
ipv6:
type: string
language:
@ -3808,6 +3812,8 @@ definitions:
properties:
autoRenew:
type: boolean
description:
type: string
dir:
type: string
domains:
@ -4227,6 +4233,8 @@ definitions:
type: string
certificatePath:
type: string
description:
type: string
privateKey:
type: string
privateKeyPath:
@ -8522,6 +8530,19 @@ paths:
summary: List RecycleBin files
tags:
- File
/files/recycle/status:
get:
consumes:
- application/json
description: 获取回收站状态
responses:
"200":
description: OK
security:
- ApiKeyAuth: []
summary: Get RecycleBin status
tags:
- File
/files/rename:
post:
consumes:

View File

@ -120,3 +120,7 @@ export const RemoveFavorite = (id: number) => {
export const BatchChangeRole = (params: File.FileRole) => {
return http.post<any>('files/batch/role', params);
};
export const GetRecycleStatus = () => {
return http.get<string>('files/recycle/status');
};

View File

@ -88,7 +88,7 @@ export const Rewrites = [
'typecho',
'typecho2',
'thinkphp',
'yii2',
'yii2',
'laravel5',
'discuz',
'discuzx',

View File

@ -1087,6 +1087,8 @@ const message = {
deleteRecycleHelper: 'Are you sure you want to permanently delete the following files?',
typeErrOrEmpty: '[{0}] file type is wrong or empty folder',
dropHelper: 'Drag the files you want to upload here',
fileRecycleBin: 'File Recycle Bin',
fileRecycleBinMsg: '{0} recycle bin',
},
ssh: {
autoStart: 'Auto Start',

View File

@ -1036,6 +1036,8 @@ const message = {
deleteRecycleHelper: '確定永久刪除以下文件',
typeErrOrEmpty: '{0} 檔案類型錯誤或為空資料夾',
dropHelper: '將需要上傳的文件拖曳到此處',
fileRecycleBin: '檔案回收站',
fileRecycleBinMsg: '{0}回收站',
},
ssh: {
autoStart: '開機自啟',

View File

@ -1037,6 +1037,8 @@ const message = {
deleteRecycleHelper: '确定永久删除以下文件',
typeErrOrEmpty: '{0} 文件类型错误或为空文件夹',
dropHelper: '将需要上传的文件拖曳到此处',
fileRecycleBin: '文件回收站',
fileRecycleBinMsg: '{0}回收站',
},
ssh: {
autoStart: '开机自启',

View File

@ -8,7 +8,7 @@
<span>{{ $t('file.deleteHelper') }}</span>
</div>
</el-alert>
<div class="mt-4">
<div class="mt-4" v-if="recycleStatus === 'enable'">
<el-checkbox v-model="forceDelete">{{ $t('file.forceDeleteHelper') }}</el-checkbox>
</div>
<div class="file-list">
@ -49,7 +49,7 @@ import i18n from '@/lang';
import { ref } from 'vue';
import { File } from '@/api/interface/file';
import { getIcon } from '@/utils/util';
import { DeleteFile } from '@/api/modules/files';
import { DeleteFile, GetRecycleStatus } from '@/api/modules/files';
import { MsgSuccess } from '@/utils/message';
const open = ref(false);
@ -57,13 +57,25 @@ const files = ref();
const loading = ref(false);
const em = defineEmits(['close']);
const forceDelete = ref(false);
const recycleStatus = ref('enable');
const acceptParams = (props: File.File[]) => {
getStatus();
files.value = props;
open.value = true;
forceDelete.value = false;
};
const getStatus = async () => {
try {
const res = await GetRecycleStatus();
recycleStatus.value = res.data;
if (recycleStatus.value === 'disable') {
forceDelete.value = true;
}
} catch (error) {}
};
const onConfirm = () => {
const pros = [];
for (const s of files.value) {
@ -98,8 +110,9 @@ defineExpose({
}
.file-list {
height: 400px;
max-height: 400px;
overflow-y: auto;
margin-top: 15px;
}
.delete-warn {

View File

@ -3,12 +3,17 @@
<template #header>
<DrawerHeader :header="$t('file.recycleBin')" :back="handleClose" />
</template>
<el-button @click="clear" type="primary" :disabled="data == null || data.length == 0">
{{ $t('file.clearRecycleBin') }}
</el-button>
<el-button @click="patchDelete" :disabled="data == null || selects.length == 0">
{{ $t('commons.button.delete') }}
</el-button>
<div class="flex space-x-4">
<el-button @click="clear" type="primary" :disabled="data == null || data.length == 0">
{{ $t('file.clearRecycleBin') }}
</el-button>
<el-button @click="patchDelete" :disabled="data == null || selects.length == 0">
{{ $t('commons.button.delete') }}
</el-button>
<el-form-item :label="$t('file.fileRecycleBin')">
<el-switch v-model="status" active-value="enable" inactive-value="disable" @change="changeStatus" />
</el-form-item>
</div>
<ComplexTable
:pagination-config="paginationConfig"
v-model:selects="selects"
@ -46,11 +51,13 @@
</template>
<script lang="ts" setup>
import { clearRecycle, getRecycleList, reduceFile } from '@/api/modules/files';
import { GetRecycleStatus, clearRecycle, getRecycleList, reduceFile } from '@/api/modules/files';
import { reactive, ref } from 'vue';
import { dateFormat, computeSize } from '@/utils/util';
import i18n from '@/lang';
import Delete from './delete/index.vue';
import { updateSetting } from '@/api/modules/setting';
import { MsgSuccess } from '@/utils/message';
const open = ref(false);
const req = reactive({
@ -62,6 +69,7 @@ const em = defineEmits(['close']);
const selects = ref([]);
const loading = ref(false);
const files = ref([]);
const status = ref('enable');
const paginationConfig = reactive({
cacheSizeKey: 'recycle-page-size',
@ -83,6 +91,23 @@ const getFileSize = (size: number) => {
const acceptParams = () => {
search();
getStatus();
};
const getStatus = async () => {
try {
const res = await GetRecycleStatus();
status.value = res.data;
} catch (error) {}
};
const changeStatus = async () => {
try {
loading.value = true;
await updateSetting({ key: 'FileRecycleBin', value: status.value });
MsgSuccess(i18n.global.t('file.fileRecycleBinMsg', [i18n.global.t('commons.button.' + status.value)]));
loading.value = false;
} catch (error) {}
};
const search = async () => {