Initial QOffice implementation
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
export const DOCX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||||
export const XLSX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
export const PPTX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
||||
export const EML_MIME_TYPE = "message/rfc822;charset=utf-8";
|
||||
|
||||
const pathSeparatorPattern = /[/\\]/;
|
||||
|
||||
export function officeTitleFromFileName(fileName: string, fallbackTitle: string) {
|
||||
const trimmed = fileName.trim();
|
||||
if (!trimmed) {
|
||||
return fallbackTitle;
|
||||
}
|
||||
|
||||
const parts = trimmed.split(pathSeparatorPattern).filter(Boolean);
|
||||
return parts[parts.length - 1] ?? fallbackTitle;
|
||||
}
|
||||
|
||||
export function replaceFileExtension(fileName: string, extension: string) {
|
||||
const normalizedExtension = extension.startsWith(".") ? extension : `.${extension}`;
|
||||
const baseName = fileName.trim() || "Документ";
|
||||
const withoutExtension = baseName.replace(/\.[^.\\/]+$/, "");
|
||||
|
||||
return `${withoutExtension}${normalizedExtension}`;
|
||||
}
|
||||
|
||||
export async function readOfficeFileAsArrayBuffer(file: File) {
|
||||
if (typeof file.arrayBuffer !== "function") {
|
||||
return readFileWithFileReader(file);
|
||||
}
|
||||
|
||||
return file.arrayBuffer();
|
||||
}
|
||||
|
||||
function readFileWithFileReader(file: File): Promise<ArrayBuffer> {
|
||||
if (typeof FileReader === "undefined") {
|
||||
throw new Error("Не удалось прочитать файл: среда не поддерживает File.arrayBuffer() и FileReader.");
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onerror = () => reject(new Error("Не удалось прочитать файл."));
|
||||
reader.onload = () => {
|
||||
if (reader.result instanceof ArrayBuffer) {
|
||||
resolve(reader.result);
|
||||
return;
|
||||
}
|
||||
|
||||
reject(new Error("Не удалось прочитать файл как ArrayBuffer."));
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user