dsh-kb-layers 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/word-convert.ps1 +56 -56
- package/package.json +1 -1
package/lib/word-convert.ps1
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
#
|
|
2
|
-
# Convert a legacy Word binary to .docx with the Word that is already installed.
|
|
3
|
-
#
|
|
4
|
-
# Run by the host as an -EncodedCommand (UTF-16LE base64), so no script file ever
|
|
5
|
-
# needs to live outside the package. Inputs arrive through the environment:
|
|
6
|
-
#
|
|
7
|
-
# DSH_KB_DOC_IN absolute path of the .doc to read
|
|
8
|
-
# DSH_KB_DOC_OUT absolute path of the .docx to write
|
|
9
|
-
#
|
|
10
|
-
# Exit codes are the contract with lib/extract.js:
|
|
11
|
-
# 0 converted, DSH_KB_DOC_OUT exists
|
|
12
|
-
# 42 the user has Word or WPS open -> caller must NOT touch COM, just fall back
|
|
13
|
-
# 43 Word is not installed -> caller caches "no Word" for the process
|
|
14
|
-
# 1 conversion failed -> caller falls back to reading the bytes
|
|
15
|
-
#
|
|
16
|
-
$ErrorActionPreference = 'Stop'
|
|
17
|
-
$source = $env:DSH_KB_DOC_IN
|
|
18
|
-
$target = $env:DSH_KB_DOC_OUT
|
|
19
|
-
if (-not (Test-Path -LiteralPath $source)) { exit 2 }
|
|
20
|
-
|
|
21
|
-
# Word hands out one COM instance per desktop: New-Object here would attach to
|
|
22
|
-
# the document the user is editing, and Quit() at the end would close it. If
|
|
23
|
-
# Word — or WPS, which also answers to `Word.Application` — is already running
|
|
24
|
-
# we do not touch it at all. Checking only WINWORD missed the WPS case and let
|
|
25
|
-
# this script connect to the user's open document and Quit() it on the way out.
|
|
26
|
-
if (Get-Process -Name WINWORD, wps -ErrorAction SilentlyContinue) { exit 42 }
|
|
27
|
-
|
|
28
|
-
$word = $null
|
|
29
|
-
$document = $null
|
|
30
|
-
try {
|
|
31
|
-
try {
|
|
32
|
-
$word = New-Object -ComObject Word.Application
|
|
33
|
-
} catch {
|
|
34
|
-
exit 43
|
|
35
|
-
}
|
|
36
|
-
$word.Visible = $false
|
|
37
|
-
$word.DisplayAlerts = 0
|
|
38
|
-
# 3 = msoAutomationSecurityForceDisable: a .doc carrying a macro must never
|
|
39
|
-
# run just because it was uploaded.
|
|
40
|
-
$word.AutomationSecurity = 3
|
|
41
|
-
|
|
42
|
-
# Open read-only and do not ask about format conversion, so an odd file
|
|
43
|
-
# cannot park a modal dialog in front of an unmanned process.
|
|
44
|
-
$document = $word.Documents.Open($source, $false, $true)
|
|
45
|
-
# 12 = wdFormatXMLDocument (.docx)
|
|
46
|
-
$document.SaveAs([ref]$target, [ref]12)
|
|
47
|
-
$document.Close([ref]0)
|
|
48
|
-
} catch {
|
|
49
|
-
exit 1
|
|
50
|
-
} finally {
|
|
51
|
-
if ($null -ne $document) { try { [void]$document.Close([ref]0) } catch { } }
|
|
52
|
-
if ($null -ne $word) { try { $word.Quit() } catch { } }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (Test-Path -LiteralPath $target) { exit 0 }
|
|
56
|
-
exit 1
|
|
1
|
+
#
|
|
2
|
+
# Convert a legacy Word binary to .docx with the Word that is already installed.
|
|
3
|
+
#
|
|
4
|
+
# Run by the host as an -EncodedCommand (UTF-16LE base64), so no script file ever
|
|
5
|
+
# needs to live outside the package. Inputs arrive through the environment:
|
|
6
|
+
#
|
|
7
|
+
# DSH_KB_DOC_IN absolute path of the .doc to read
|
|
8
|
+
# DSH_KB_DOC_OUT absolute path of the .docx to write
|
|
9
|
+
#
|
|
10
|
+
# Exit codes are the contract with lib/extract.js:
|
|
11
|
+
# 0 converted, DSH_KB_DOC_OUT exists
|
|
12
|
+
# 42 the user has Word or WPS open -> caller must NOT touch COM, just fall back
|
|
13
|
+
# 43 Word is not installed -> caller caches "no Word" for the process
|
|
14
|
+
# 1 conversion failed -> caller falls back to reading the bytes
|
|
15
|
+
#
|
|
16
|
+
$ErrorActionPreference = 'Stop'
|
|
17
|
+
$source = $env:DSH_KB_DOC_IN
|
|
18
|
+
$target = $env:DSH_KB_DOC_OUT
|
|
19
|
+
if (-not (Test-Path -LiteralPath $source)) { exit 2 }
|
|
20
|
+
|
|
21
|
+
# Word hands out one COM instance per desktop: New-Object here would attach to
|
|
22
|
+
# the document the user is editing, and Quit() at the end would close it. If
|
|
23
|
+
# Word — or WPS, which also answers to `Word.Application` — is already running
|
|
24
|
+
# we do not touch it at all. Checking only WINWORD missed the WPS case and let
|
|
25
|
+
# this script connect to the user's open document and Quit() it on the way out.
|
|
26
|
+
if (Get-Process -Name WINWORD, wps -ErrorAction SilentlyContinue) { exit 42 }
|
|
27
|
+
|
|
28
|
+
$word = $null
|
|
29
|
+
$document = $null
|
|
30
|
+
try {
|
|
31
|
+
try {
|
|
32
|
+
$word = New-Object -ComObject Word.Application
|
|
33
|
+
} catch {
|
|
34
|
+
exit 43
|
|
35
|
+
}
|
|
36
|
+
$word.Visible = $false
|
|
37
|
+
$word.DisplayAlerts = 0
|
|
38
|
+
# 3 = msoAutomationSecurityForceDisable: a .doc carrying a macro must never
|
|
39
|
+
# run just because it was uploaded.
|
|
40
|
+
$word.AutomationSecurity = 3
|
|
41
|
+
|
|
42
|
+
# Open read-only and do not ask about format conversion, so an odd file
|
|
43
|
+
# cannot park a modal dialog in front of an unmanned process.
|
|
44
|
+
$document = $word.Documents.Open($source, $false, $true)
|
|
45
|
+
# 12 = wdFormatXMLDocument (.docx)
|
|
46
|
+
$document.SaveAs([ref]$target, [ref]12)
|
|
47
|
+
$document.Close([ref]0)
|
|
48
|
+
} catch {
|
|
49
|
+
exit 1
|
|
50
|
+
} finally {
|
|
51
|
+
if ($null -ne $document) { try { [void]$document.Close([ref]0) } catch { } }
|
|
52
|
+
if ($null -ne $word) { try { $word.Quit() } catch { } }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (Test-Path -LiteralPath $target) { exit 0 }
|
|
56
|
+
exit 1
|