การทดสอบ 6 รอบบนเครื่องจริง (Mac, Word 16.101) พิสูจน์ว่า ms-word:ofe
ถูกเมินแบบไม่ deterministic ทุกรูปแบบ (http/https/query/ไม่มี query/
file:// — Word ไม่ยิง request ถึง server เลย เด้งหน้าเลือกไฟล์แทน)
open -a Word <local file> ใช้ได้แต่เรียกจากเว็บไม่ได้ (sandbox +
backend รัน server กลางใน production) → สรุปทางเดียวที่ deterministic
คือ download-first
- Backend: OpenLinkService (HMAC อายุสั้น ValidateOnStart) +
OpenController /api/open/{t}/{e}/v/{did}/{vid}/file.docx และ /d/{objectKey}
— ทุกอย่างอยู่ใน path ไม่มี query string (Word เมิน URL มี query),
[AllowAnonymous] ด้วยลายเซ็นแทน Bearer, stream จาก MinIO + disposition
ชื่อไทยเหมือนเดิม · download-link + generate-draft ออก open-link
แทน presigned ของ MinIO (http) · ลบ dead code presigned ทิ้ง
- Web: openDocxUrl = ดาวน์โหลดทันทีทุกครั้ง + ยิง ms-word ควบคู่แบบ
best-effort + notice "ไฟล์อยู่ Downloads" — เอา heuristic สลับแอป
ออก (โดนหน้าเลือกไฟล์ของ Word หลอกจนไม่ดาวน์โหลด)
- Spec v9.2 + ตารางทดสอบเครื่องจริงใน acceptance-review + CLAUDE.md
กฎ 13 (ms-word = best-effort เท่านั้น) + ทางเลือก helper อนาคต
- backend 86 / web 58 / add-in 66 + build ครบ; QA reviewer PASS
(1 round, minors 4 แก้ครบ)
หมายเหตุ deploy: ต้องตั้ง OpenLinks__Secret (ค่าลับจริง) และ
OpenLinks__BaseUrl (https domain ของ backend) — fail ดังตั้งแต่สตาร์ทถ้าไม่ตั้ง
Co-Authored-By: Claude Code <noreply@anthropic.com>
83 lines
4.3 KiB
TypeScript
83 lines
4.3 KiB
TypeScript
import { onBeforeUnmount, ref } from 'vue'
|
|
import { versionDownloadPath, wordOpenUrl, type VersionFormat } from '../utils/versionDownload'
|
|
|
|
/** Shared by history and generate success — ลิงก์เปิดอายุสั้นของ backend (open-link) ใช้ทั้งดาวน์โหลดและเปิดแบบ best-effort */
|
|
export function useVersionOpen() {
|
|
const { apiJson } = useApi()
|
|
const openingVersionId = ref<string | null>(null)
|
|
const downloadError = ref<string | null>(null)
|
|
const fallbackUrl = ref<string | null>(null)
|
|
const openNotice = ref<string | null>(null)
|
|
let generation = 0
|
|
let pendingTab: Window | null = null
|
|
|
|
function resetOpen() {
|
|
generation++
|
|
pendingTab?.close()
|
|
pendingTab = null
|
|
openingVersionId.value = null
|
|
downloadError.value = null
|
|
fallbackUrl.value = null
|
|
openNotice.value = null
|
|
}
|
|
|
|
/** ให้เบราว์เซอร์เซฟ URL ลง Downloads (ลิงก์เปิดของ backend ส่ง disposition: attachment อยู่แล้ว —
|
|
* หน้าเว็บอยู่ที่เดิม ไม่ navigate ไปไหน) */
|
|
function saveToDownloads(url: string) {
|
|
const a = document.createElement('a')
|
|
a.href = url
|
|
document.body.append(a)
|
|
a.click()
|
|
a.remove()
|
|
}
|
|
|
|
/** เปิดไฟล์ docx แบบ download-first (v9.2): ดาวน์โหลดไฟล์ให้ทันทีทุกครั้ง แล้วยิง ms-word:ofe
|
|
* ควบคู่แบบ best-effort เผื่อเครื่องที่ scheme ยังทำงาน (Windows / Word รุ่นก่อน) —
|
|
* เลิกพึ่ง heuristic การสลับแอป เพราะ Word for Mac 16.101+ (ก.ย. 2026) เมิน URL ทุกรูปแบบ
|
|
* โดยเด้งหน้าเลือกไฟล์ (เหมือนสลับแอปจริง — heuristic โดนหลอกจนไม่ดาวน์โหลดให้)
|
|
* ใช้กับลิงก์ที่ถืออยู่เองได้เลย (ไฟล์ร่าง FR-16: response ของ generate-draft คือ URL แล้ว) */
|
|
function openDocxUrl(url: string) {
|
|
resetOpen()
|
|
fallbackUrl.value = url
|
|
saveToDownloads(url)
|
|
openNotice.value =
|
|
'ดาวน์โหลดไฟล์แล้วที่โฟลเดอร์ Downloads — เปิดจากที่นั่นได้เลย (บางเครื่อง Word จะเปิดให้อัตโนมัติ)'
|
|
window.location.assign(wordOpenUrl(url))
|
|
}
|
|
|
|
async function openVersion(documentId: string, versionId: string, format: VersionFormat = 'docx') {
|
|
if (openingVersionId.value) return
|
|
resetOpen()
|
|
const current = generation
|
|
openingVersionId.value = versionId
|
|
// Reserve a tab during the user gesture, before the authenticated request loses activation.
|
|
let tab: Window | null = null
|
|
try {
|
|
tab = format === 'pdf' ? window.open('about:blank', '_blank') : null
|
|
if (tab) tab.opener = null
|
|
pendingTab = tab
|
|
const link = await apiJson<{ url: string; expiresAt: string }>(versionDownloadPath(documentId, versionId, format))
|
|
if (current !== generation) { tab?.close(); return }
|
|
if (!link) throw new Error('ขอลิงก์เปิดไฟล์ไม่สำเร็จ')
|
|
fallbackUrl.value = link.url
|
|
if (format === 'pdf') {
|
|
if (tab && !tab.closed) {
|
|
tab.location.replace(link.url)
|
|
pendingTab = null
|
|
}
|
|
else openNotice.value = 'เบราว์เซอร์บล็อกแท็บใหม่ กรุณากดลิงก์เปิดไฟล์ด้านล่าง'
|
|
} else {
|
|
openDocxUrl(link.url)
|
|
}
|
|
} catch (err) {
|
|
tab?.close()
|
|
if (pendingTab === tab) pendingTab = null
|
|
if (current === generation) downloadError.value = err instanceof Error ? err.message : 'เปิดไฟล์ไม่สำเร็จ'
|
|
} finally {
|
|
if (current === generation) openingVersionId.value = null
|
|
}
|
|
}
|
|
|
|
onBeforeUnmount(resetOpen)
|
|
return { openingVersionId, downloadError, fallbackUrl, openNotice, openVersion, openDocxUrl, resetOpen }
|
|
}
|