download 기능 수정
This commit is contained in:
1 parent
80eae206b6
commit
f5d880caf5
21 files changed
+461
-122
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { config } from '../config.js';
|
||||
import type { SessionDoc } from '../types/session.js';
|
||||
import { overwriteDocument } from './sessionStore.js';
|
||||
import { signOnlyOfficePayload } from '../utils/jwt.js';
|
||||
|
||||
export function buildEditorConfig(doc: SessionDoc, userId: string) {
|
||||
@@ -49,6 +50,59 @@ export function buildEditorConfig(doc: SessionDoc, userId: string) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Prefer the internal Document Server base when the backend fetches the file. */
|
||||
export function toInternalOnlyOfficeUrl(raw: string): string {
|
||||
try {
|
||||
const target = new URL(raw);
|
||||
const publicBase = new URL(config.onlyOfficeUrl);
|
||||
const internalBase = new URL(config.onlyOfficeInternalUrl);
|
||||
if (
|
||||
target.host === publicBase.host ||
|
||||
target.host === internalBase.host
|
||||
) {
|
||||
target.protocol = internalBase.protocol;
|
||||
target.host = internalBase.host;
|
||||
}
|
||||
return target.toString();
|
||||
} catch {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download an OnlyOffice-produced file URL and overwrite the session document
|
||||
* (converting back to the original extension when needed).
|
||||
*/
|
||||
export async function persistDocumentFromUrl(
|
||||
doc: SessionDoc,
|
||||
fileUrl: string,
|
||||
filetype?: string,
|
||||
): Promise<Buffer> {
|
||||
const fetchUrl = toInternalOnlyOfficeUrl(fileUrl);
|
||||
const downloaded = await fetch(fetchUrl);
|
||||
if (!downloaded.ok) {
|
||||
throw new Error(`download_failed_${downloaded.status}`);
|
||||
}
|
||||
|
||||
let buffer: Buffer = Buffer.from(await downloaded.arrayBuffer());
|
||||
const returnedType = (filetype ?? doc.fileType).toLowerCase();
|
||||
|
||||
if (returnedType !== doc.fileType) {
|
||||
const converted = await convertToOriginalFormat(
|
||||
fetchUrl,
|
||||
returnedType,
|
||||
doc.fileType,
|
||||
);
|
||||
if (!converted) {
|
||||
throw new Error(`convert_failed:${returnedType}->${doc.fileType}`);
|
||||
}
|
||||
buffer = Buffer.from(converted);
|
||||
}
|
||||
|
||||
await overwriteDocument(doc, buffer, doc.fileType);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
export async function convertToOriginalFormat(
|
||||
sourceUrl: string,
|
||||
fromExt: string,
|
||||
|
||||
Reference in new issue
Block a user