Files
orion-visor/orion-visor-ui/src/utils/file.ts

204 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-08-03 14:21:27 +08:00
import { isEmptyStr } from './index';
2023-07-27 18:48:15 +08:00
/**
* base64
*/
export function getBase64Data(e: string) {
return e.substring(e.indexOf(',') + 1);
}
2024-10-10 23:18:53 +08:00
/**
* blob promise
*/
export function readBlobText(blob: Blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = event => {
resolve(event.target?.result as string);
};
reader.onerror = err => {
reject(err);
};
reader.readAsText(blob);
});
}
2023-07-27 18:48:15 +08:00
/**
2023-09-20 17:13:38 +08:00
* promise
2023-07-27 18:48:15 +08:00
*/
2023-09-25 14:36:13 +08:00
export function readFileText(e: File, encoding = 'UTF-8'): Promise<string> {
2023-07-27 18:48:15 +08:00
return new Promise((resolve, reject) => {
const reader = new FileReader();
2023-09-20 17:13:38 +08:00
reader.readAsText(e, encoding);
2023-07-27 18:48:15 +08:00
reader.onload = res => {
2023-09-25 14:36:13 +08:00
resolve(res.target?.result as string);
2023-07-27 18:48:15 +08:00
};
reader.onerror = err => {
reject(err);
};
});
}
/**
*
*/
2024-02-10 00:05:27 +08:00
export interface PathAnalysis {
2023-07-27 18:48:15 +08:00
name: string;
path: string;
}
/**
*
*/
2024-02-10 00:05:27 +08:00
export function getPathAnalysis(path: string, paths: PathAnalysis[] = []): PathAnalysis[] {
const lastSeparatorIndex = path.lastIndexOf('/');
if (lastSeparatorIndex === -1) {
2023-07-27 18:48:15 +08:00
return paths;
}
2024-02-10 00:05:27 +08:00
const name = path.substring(lastSeparatorIndex, path.length);
2023-07-27 18:48:15 +08:00
if (!isEmptyStr(name) && name !== '/') {
paths.unshift({
name: name.substring(1, name.length),
2024-02-10 00:05:27 +08:00
path: path
2023-07-27 18:48:15 +08:00
});
}
2024-02-10 00:05:27 +08:00
return getPathAnalysis(path.substring(0, lastSeparatorIndex), paths);
2023-07-27 18:48:15 +08:00
}
/**
*
*/
export function getPath(path: string) {
return path.replace(new RegExp('\\\\+', 'g'), '/')
2024-02-10 00:05:27 +08:00
.replace(new RegExp('/+', 'g'), '/');
2023-07-27 18:48:15 +08:00
}
2024-02-23 00:47:52 +08:00
/**
*
*/
export function getFileName(path: string) {
path = getPath(path);
return path.substring(path.lastIndexOf('/') + 1);
}
2023-07-27 18:48:15 +08:00
/**
*
*/
export function getParentPath(path: string) {
const paths = getPath(path).split('/');
const len = paths.length;
if (len <= 2) {
return '/';
}
let parent = '';
for (let i = 0; i < len - 1; i++) {
parent += paths[i];
if (i !== len - 2) {
parent += '/';
}
}
return parent;
}
2024-06-11 12:31:16 +08:00
/**
*
*/
export function openDownloadFile(url: string) {
try {
// 创建隐藏的可下载链接
let element = document.createElement('a');
element.setAttribute('href', url);
element.setAttribute('download', '');
element.style.display = 'none';
// 将其附加到文档中
document.body.appendChild(element);
// 点击该下载链接
element.click();
// 移除已下载的链接
document.body.removeChild(element);
} catch (e) {
window.open(url, 'newWindow');
}
}
2023-07-27 18:48:15 +08:00
/**
*
*/
2024-03-20 15:28:20 +08:00
export function downloadFile(res: any, fileName: string = '') {
2024-08-23 01:44:05 +08:00
const blob = new Blob([res.data], { type: 'application/octet-stream' });
2023-07-27 18:48:15 +08:00
const tempLink = document.createElement('a');
const blobURL = window.URL.createObjectURL(blob);
tempLink.style.display = 'none';
tempLink.href = blobURL;
if (!fileName) {
fileName = res.headers['content-disposition']
? res.headers['content-disposition'].split(';')[1].split('=')[1]
: new Date().getTime();
}
tempLink.download = decodeURIComponent(fileName);
if (typeof tempLink.download === 'undefined') {
tempLink.target = '_blank';
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
/**
* 10
*/
export function permission10toString(permission: number) {
2024-02-20 11:43:18 +08:00
if (permission === undefined) {
return '---';
}
2023-07-27 18:48:15 +08:00
const ps = (permission + '');
let res = '';
for (let i = 0; i < ps.length; i++) {
2023-09-21 17:12:00 +08:00
const per = ps.charAt(i) as unknown as number;
2023-07-27 18:48:15 +08:00
if ((per & 4) === 0) {
res += '-';
} else {
res += 'r';
}
if ((per & 2) === 0) {
res += '-';
} else {
res += 'w';
}
if ((per & 1) === 0) {
res += '-';
} else {
res += 'x';
}
}
2024-02-20 11:43:18 +08:00
if (res.length <= 9) {
res = res.padEnd(9, '-');
} else {
res = res.substring(0, 9);
}
2023-07-27 18:48:15 +08:00
return res;
}
2024-02-21 16:32:29 +08:00
// 获取文件大小
export function getFileSize(size: number, scale: number = 2) {
let result;
let unit;
if (size >= 1024 * 1024 * 1024) {
result = (size / (1024 * 1024 * 1024)).toFixed(scale);
unit = 'GB';
} else if (size >= 1024 * 1024) {
result = (size / (1024 * 1024)).toFixed(scale);
unit = 'MB';
} else if (size >= 1024) {
result = (size / 1024).toFixed(scale);
unit = 'KB';
} else {
result = size;
unit = 'B';
}
return `${result} ${unit}`;
}
2023-07-27 18:48:15 +08:00
export default null;