office: save OOXML documents in OOXML format, not as ODF
- Commit
42cb0c0f4e47cf5cfa44ec61b879a922f5d318de- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:10 UTC (28 days ago)
- Parents
ac79a87
Symptom: opening a .docx/.xlsx/.pptx, editing, and pressing Save destroyed the file: an ODF package (content.xml, meta.xml, ODF manifest) was written over the OOXML target, mixed with copied OOXML parts, producing a corrupt hybrid no reader could open. Root cause: OfficeViewModel.save always called OdfWriter.save regardless of the target format, and passed the OOXML source package as the copy-source for ODF saves. Fix: route by target extension — OOXML targets are written with OoxmlExporter; ODF targets keep OdfWriter, and an OOXML source package is never used as the copy-source for an ODF package. Verification: new OfficeViewModelSaveSlice3Test test saves to a .docx target and asserts word/document.xml is present, content.xml absent, and the file re-imports with its text; targeted :office:testDevUnitTest passes.
Diff
office/src/main/java/com/vayunmathur/office/util/OfficeViewModelDocuments.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModelDocuments.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModelDocuments.ktindex f1b6cfef43..d2321b3141 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModelDocuments.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModelDocuments.kt@@ -190,16 +190,35 @@ fun OfficeViewModel.createNewPresentation() { originalUri = null } +/** OOXML (Microsoft) extensions whose files must be written with the OOXML exporter. */+private val OOXML_EXTENSIONS = setOf(+ "docx", "docm", "dotx", "dotm", "xlsx", "xlsm", "xltx", "xltm",+ "pptx", "pptm", "potx", "potm", "ppsx", "ppsm")++internal fun uriExtension(uri: Uri?): String =+ uri?.lastPathSegment?.substringAfterLast('.', "")?.lowercase() ?: ""+ fun OfficeViewModel.save(targetUri: Uri? = null) { val doc = (stateMutable.value as? ViewState.Loaded)?.document ?: return- // Source may be null for a brand-new document; the writer then builds the package from scratch.- val source = documentUri // Write back to the document the user opened, not to the app-private read cache. val target = targetUri ?: originalUri ?: return+ // An OOXML target (.docx/.xlsx/.pptx…) must be written by the OOXML exporter:+ // writing an ODF package over it (the old behaviour) destroyed the file.+ val targetIsOoxml = uriExtension(target) in OOXML_EXTENSIONS+ // Source may be null for a brand-new document; the writer then builds the package from scratch.+ // Never copy an OOXML source package's parts into an ODF package.+ val source = documentUri?.takeUnless { uriExtension(it) in OOXML_EXTENSIONS } isSavingMutable.value = true viewModelScope.launch(Dispatchers.IO) { try {- OdfWriter.save(getApplication(), source, doc, target)+ if (targetIsOoxml) {+ val bytes = com.vayunmathur.library.ui.odf.OoxmlExporter.export(doc)+ getApplication<Application>().contentResolver.openOutputStream(target)+ ?.use { it.write(bytes) }+ ?: throw java.io.IOException("Cannot open $target for writing")+ } else {+ OdfWriter.save(getApplication(), source, doc, target)+ } hasUnsavedChangesMutable.value = false originalUri = target if (documentUri == null) documentUri = target
office/src/test/java/com/vayunmathur/office/util/OfficeViewModelSaveSlice3Test.kt
diff --git a/office/src/test/java/com/vayunmathur/office/util/OfficeViewModelSaveSlice3Test.kt b/office/src/test/java/com/vayunmathur/office/util/OfficeViewModelSaveSlice3Test.ktindex c93e4a37fd..999b0f183e 100644--- a/office/src/test/java/com/vayunmathur/office/util/OfficeViewModelSaveSlice3Test.kt+++ b/office/src/test/java/com/vayunmathur/office/util/OfficeViewModelSaveSlice3Test.kt@@ -133,6 +133,36 @@ class OfficeViewModelSaveSlice3Test : OfficeViewModelSlice3Harness() { assertTrue(text.contains("v2 text"), "rewritten text was: $text") } + @Test+ fun `save to a docx target writes OOXML not an ODF package`() = runTest {+ val vm = roboLoaded("docx line one")+ val target = java.io.File(tmpDir, "roundtrip.docx")+ val targetUri = android.net.Uri.fromFile(target)+ vm.documentUri = null+ vm.originalUri = targetUri+ vm.save()+ var spins = 0+ while (!target.exists() && spins < 200) {+ Thread.sleep(25)+ spins+++ }+ assertTrue(target.exists(), "save should write ${target.absolutePath}")+ val names = mutableListOf<String>()+ java.util.zip.ZipInputStream(target.inputStream()).use { zip ->+ var e = zip.nextEntry+ while (e != null) {+ names.add(e.name)+ e = zip.nextEntry+ }+ }+ assertTrue("word/document.xml" in names, "OOXML parts missing: $names")+ assertTrue("content.xml" !in names, "ODF part leaked into docx: $names")+ val reopened = com.vayunmathur.office.odf.DocumentImporter.open(+ roboApp, targetUri, "roundtrip.docx")+ assertTrue(+ paragraphsOf(reopened as OdfDocument.TextDocument).contains("docx line one"))+ }+ @Test fun `loadDocument imports a saved package`() = runTest { // Seed a real .odt via the writer, then loadDocument should import it