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

131 lines
2.9 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);
}
/**
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
}
/**
*
*/
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;
}
/**
*
*/
export function downloadFile(res: any, fileName: string) {
const blob = new Blob([res.data]);
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) {
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';
}
}
return res;
}
export default null;