mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-31 14:08:06 +08:00
feat: 文件,单文件下载直接下载,文件夹或者多文件下载,提示压缩
This commit is contained in:
parent
e1518e842f
commit
30d7ce7759
@ -71,9 +71,10 @@ type FileMove struct {
|
||||
}
|
||||
|
||||
type FileDownload struct {
|
||||
Paths []string `json:"paths" validate:"required"`
|
||||
Type string `json:"type" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Paths []string `json:"paths" validate:"required"`
|
||||
Type string `json:"type" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Compress bool `json:"compress" validate:"required"`
|
||||
}
|
||||
|
||||
type DirSizeReq struct {
|
||||
|
@ -38,8 +38,8 @@ func (f FileService) GetFileList(op request.FileOption) (response.FileInfo, erro
|
||||
|
||||
func (f FileService) SearchUploadWithPage(req request.SearchUploadWithPage) (int64, interface{}, error) {
|
||||
var (
|
||||
files []response.UploadInfo
|
||||
backDatas []response.UploadInfo
|
||||
files []response.UploadInfo
|
||||
backData []response.UploadInfo
|
||||
)
|
||||
|
||||
_ = filepath.Walk(req.Path, func(path string, info os.FileInfo, err error) error {
|
||||
@ -57,14 +57,14 @@ func (f FileService) SearchUploadWithPage(req request.SearchUploadWithPage) (int
|
||||
})
|
||||
total, start, end := len(files), (req.Page-1)*req.PageSize, req.Page*req.PageSize
|
||||
if start > total {
|
||||
backDatas = make([]response.UploadInfo, 0)
|
||||
backData = make([]response.UploadInfo, 0)
|
||||
} else {
|
||||
if end >= total {
|
||||
end = total
|
||||
}
|
||||
backDatas = files[start:end]
|
||||
backData = files[start:end]
|
||||
}
|
||||
return int64(total), backDatas, nil
|
||||
return int64(total), backData, nil
|
||||
}
|
||||
|
||||
func (f FileService) GetFileTree(op request.FileOption) ([]response.FileTree, error) {
|
||||
@ -216,15 +216,18 @@ func (f FileService) MvFile(m request.FileMove) error {
|
||||
}
|
||||
|
||||
func (f FileService) FileDownload(d request.FileDownload) (string, error) {
|
||||
tempPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
|
||||
if err := os.MkdirAll(tempPath, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
filePath := d.Paths[0]
|
||||
if d.Compress {
|
||||
tempPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
|
||||
if err := os.MkdirAll(tempPath, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
fo := files.NewFileOp()
|
||||
if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
filePath = filepath.Join(tempPath, d.Name)
|
||||
}
|
||||
fo := files.NewFileOp()
|
||||
if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
filePath := filepath.Join(tempPath, d.Name)
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ ErrAppRequired: "{{ .detail }} app is required"
|
||||
ErrNotInstall: "App not installed"
|
||||
ErrPortInOtherApp: "{{ .port }} port already in use by {{ .apps }}"
|
||||
ErrDbUserNotValid: "Stock database, username and password do not match!"
|
||||
ErrDockerComposeNotValid: "docker-compose file format error!"
|
||||
|
||||
#file
|
||||
ErrFileCanNotRead: "File can not read"
|
||||
|
@ -24,6 +24,7 @@ ErrAppRequired: "请先安装 {{ .detail }} 应用"
|
||||
ErrNotInstall: "应用未安装"
|
||||
ErrPortInOtherApp: "{{ .port }} 端口已被 {{ .apps }}占用!"
|
||||
ErrDbUserNotValid: "存量数据库,用户名密码不匹配!"
|
||||
ErrDockerComposeNotValid: "docker-compose 文件格式错误"
|
||||
|
||||
#file
|
||||
ErrFileCanNotRead: "此文件不支持预览"
|
||||
|
@ -72,25 +72,13 @@ let addForm = ref({
|
||||
paths: [] as string[],
|
||||
type: '',
|
||||
name: '',
|
||||
compress: true,
|
||||
});
|
||||
|
||||
const extension = computed(() => {
|
||||
return CompressExtention[addForm.value.type];
|
||||
});
|
||||
|
||||
// const onOpen = () => {
|
||||
// addForm.value = {
|
||||
// type: 'zip',
|
||||
// paths: props.paths,
|
||||
// name: props.name,
|
||||
// };
|
||||
// console.log(addForm);
|
||||
// options.value = [];
|
||||
// for (const t in CompressType) {
|
||||
// options.value.push(CompressType[t]);
|
||||
// }
|
||||
// };
|
||||
|
||||
const submit = async (formEl: FormInstance | undefined) => {
|
||||
if (!formEl) return;
|
||||
await formEl.validate((valid) => {
|
||||
@ -121,6 +109,7 @@ const acceptParams = (props: DownloadProps) => {
|
||||
addForm.value.paths = props.paths;
|
||||
addForm.value.name = props.name;
|
||||
addForm.value.type = 'zip';
|
||||
addForm.value.compress = true;
|
||||
options.value = [];
|
||||
for (const t in CompressType) {
|
||||
options.value.push(CompressType[t]);
|
||||
|
@ -146,7 +146,7 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { nextTick, onMounted, reactive, ref } from '@vue/runtime-core';
|
||||
import { GetFilesList, DeleteFile, GetFileContent, ComputeDirSize } from '@/api/modules/files';
|
||||
import { GetFilesList, DeleteFile, GetFileContent, ComputeDirSize, DownloadFile } from '@/api/modules/files';
|
||||
import { computeSize, dateFormat, getIcon, getRandomStr } from '@/utils/util';
|
||||
import { File } from '@/api/interface/file';
|
||||
import { useDeleteData } from '@/hooks/use-delete-data';
|
||||
@ -435,8 +435,25 @@ const openDownload = () => {
|
||||
paths.push(s['path']);
|
||||
}
|
||||
fileDownload.paths = paths;
|
||||
fileDownload.name = getRandomStr(6);
|
||||
downloadRef.value.acceptParams(fileDownload);
|
||||
if (selects.value.length > 1 || selects.value[0].isDir) {
|
||||
fileDownload.name = selects.value.length > 1 ? getRandomStr(6) : selects.value[0].name;
|
||||
downloadRef.value.acceptParams(fileDownload);
|
||||
} else {
|
||||
fileDownload.name = selects.value[0].name;
|
||||
DownloadFile(fileDownload as File.FileDownload)
|
||||
.then((res) => {
|
||||
const downloadUrl = window.URL.createObjectURL(new Blob([res], { type: 'application/octet-stream' }));
|
||||
const a = document.createElement('a');
|
||||
a.style.display = 'none';
|
||||
a.href = downloadUrl;
|
||||
a.download = fileDownload.name;
|
||||
const event = new MouseEvent('click');
|
||||
a.dispatchEvent(event);
|
||||
})
|
||||
.finally(() => {
|
||||
loading.value = false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// const openDetail = (row: File.File) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user