download 기능 수정

This commit is contained in:
ysj7705 committed 2026-07-23 17:17:24 +09:00
1 parent 80eae206b6
commit f5d880caf5
21 files changed
+461 -122

No files matched your search

+2 -29
View File
@@ -7,11 +7,10 @@ import {
addDocument,
deleteDocument,
getDocument,
overwriteDocument,
} from '../services/sessionStore.js';
import {
buildEditorConfig,
convertToOriginalFormat,
persistDocumentFromUrl,
} from '../services/onlyOffice.js';
import {
createBlankDocument,
@@ -187,33 +186,7 @@ documentsRouter.post('/:docId/callback', async (req, res) => {
}
try {
const downloaded = await fetch(fileUrl);
if (!downloaded.ok) {
console.error('[callback] download failed', downloaded.status);
res.json({ error: 1 });
return;
}
let buffer: Buffer = Buffer.from(await downloaded.arrayBuffer());
const returnedType = (filetype ?? doc.fileType).toLowerCase();
if (returnedType !== doc.fileType) {
const converted = await convertToOriginalFormat(
fileUrl,
returnedType,
doc.fileType,
);
if (!converted) {
console.error(
`[callback] refused save: got ${returnedType}, need ${doc.fileType}`,
);
res.json({ error: 1 });
return;
}
buffer = Buffer.from(converted);
}
await overwriteDocument(doc, buffer, doc.fileType);
await persistDocumentFromUrl(doc, fileUrl, filetype);
} catch (err) {
console.error('[callback] save error', err);
res.json({ error: 1 });
@@ -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,