-
Notifications
You must be signed in to change notification settings - Fork 10
/
OcrmypdfCommandExecutor.java
110 lines (93 loc) · 3.98 KB
/
OcrmypdfCommandExecutor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
* -
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* -
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* -
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.transformer.executors;
import static org.alfresco.transformer.util.RequestParamMap.TIMEOUT;
import static org.alfresco.transformer.util.Util.stringToLong;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class OcrmypdfCommandExecutor extends AbstractCommandExecutor {
public static String ID = "ocrmypdf";
public static String EXTRA_ARGUMENTS = "--extra-arguments";
public static final String LICENCE = "This transformer uses ocrmypdf which uses the Tesseract library from Google Inc. See the license at https://github.com/tesseract-ocr/tesseract/blob/master/LICENSE";
private final String exe;
private final String arguments;
@Autowired
public OcrmypdfCommandExecutor(@Value("${ocrmypdf.path}") String exe, @Value("${ocrmypdf.arguments}") String arguments) {
if (exe == null || exe.isEmpty()) {
throw new IllegalArgumentException("OcrmypdfCommandExecutor EXE variable cannot be null or empty");
}
this.exe = exe;
this.arguments = arguments;
super.transformCommand = createTransformCommand();
super.checkCommand = createCheckCommand();
}
@Override
public String getTransformerId() {
return ID;
}
@Override
protected RuntimeExec createTransformCommand() {
RuntimeExec runtimeExec = new RuntimeExec();
Map<String, String[]> commandsAndArguments = new HashMap<>();
commandsAndArguments.put(".*", new String[] { exe, "SPLIT:${options}", "${source}", "${target}" });
runtimeExec.setCommandsAndArguments(commandsAndArguments);
Map<String, String> defaultProperties = new HashMap<>();
defaultProperties.put("key", null);
runtimeExec.setDefaultProperties(defaultProperties);
runtimeExec.setErrorCodes("1");
return runtimeExec;
}
@Override
protected RuntimeExec createCheckCommand() {
RuntimeExec runtimeExec = new RuntimeExec();
Map<String, String[]> commandsAndArguments = new HashMap<>();
commandsAndArguments.put(".*", new String[] { exe, "--version" });
runtimeExec.setCommandsAndArguments(commandsAndArguments);
return runtimeExec;
}
@Override
public void transform(String transformName, String sourceMimetype, String targetMimetype,
Map<String, String> transformOptions, File sourceFile, File targetFile) {
Long timeout = stringToLong(transformOptions.get(TIMEOUT));
String extraArguments = transformOptions.get(EXTRA_ARGUMENTS);
StringBuilder args = new StringBuilder(arguments);
if(extraArguments != null) {
args.append(" ").append(extraArguments);
}
run(args.toString(), sourceFile, targetFile, timeout);
}
@Override
public void embedMetadata(String transformName, String sourceMimetype, String targetMimetype,
Map<String, String> transformOptions, File sourceFile, File targetFile) {
transform(transformName, sourceMimetype, targetMimetype, transformOptions, sourceFile, targetFile);
}
}