1 | <?php |
||
17 | class Converter { |
||
18 | |||
19 | const TEST_DOC_PATH = '/assets/test.doc'; |
||
20 | |||
21 | public static function testConversion(){ |
||
53 | |||
54 | public static function convert($input, $targetFilter, $targetExtension){ |
||
55 | if (self::getAppConfig()->getAppValue('converter') === 'local'){ |
||
56 | $output = self::convertLocal($input, $targetFilter, $targetExtension); |
||
57 | } else { |
||
58 | $output = self::convertExternal($input, $targetExtension); |
||
59 | } |
||
60 | |||
61 | if (empty($output)){ |
||
62 | \OC::$server->getLogger()->warning( |
||
63 | 'Empty conversion output', |
||
64 | ['app' => 'documents'] |
||
65 | |||
66 | ); |
||
67 | throw new \RuntimeException('Empty conversion output'); |
||
68 | } |
||
69 | return $output; |
||
70 | } |
||
71 | |||
72 | public static function checkConnection(){ |
||
73 | $expected = file_get_contents(dirname(__DIR__) . '/assets/response.odt'); |
||
74 | $converted = self::convertExternal('', 'odt'); |
||
75 | |||
76 | return $converted === $expected; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * convert via openOffice hosted on the same server |
||
81 | * @param string $input |
||
82 | * @param string $targetFilter |
||
83 | * @param string $targetExtension |
||
84 | * @return string |
||
85 | */ |
||
86 | protected static function convertLocal($input, $targetFilter, $targetExtension){ |
||
87 | $infile = \OC::$server->getTempManager()->getTemporaryFile(); |
||
88 | $outdir = \OC::$server->getTempManager()->getTemporaryFolder(); |
||
89 | $cmd = Helper::findOpenOffice(); |
||
90 | $params = ' --headless --convert-to ' . $targetFilter . ' --outdir ' |
||
91 | . escapeshellarg($outdir) |
||
92 | . ' --writer '. escapeshellarg($infile) |
||
93 | . ' -env:UserInstallation=file://' |
||
94 | . escapeshellarg(\OC::$server->getTempManager()->getTempBaseDir() . '/owncloud-' . \OC_Util::getInstanceId().'/') |
||
95 | ; |
||
96 | |||
97 | file_put_contents($infile, $input); |
||
98 | shell_exec($cmd . $params); |
||
99 | $output = file_get_contents($outdir . '/' . basename($infile) . '.' . $targetExtension); |
||
100 | |||
101 | return $output; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * convert via format-filter-server installed on the same host with openOffice |
||
106 | * @param string $input |
||
107 | * @return string |
||
108 | */ |
||
109 | protected static function convertExternal($input, $targetExtension){ |
||
110 | $options = array( |
||
111 | CURLOPT_RETURNTRANSFER => true, |
||
112 | CURLOPT_HEADER => false, |
||
113 | CURLOPT_FOLLOWLOCATION => true, |
||
114 | CURLOPT_ENCODING => "", |
||
115 | CURLOPT_AUTOREFERER => true, |
||
116 | CURLOPT_CONNECTTIMEOUT => 120, |
||
117 | CURLOPT_TIMEOUT => 120, |
||
118 | CURLOPT_MAXREDIRS => 2, |
||
119 | CURLOPT_POST => 1, |
||
120 | CURLOPT_POSTFIELDS => $input, |
||
121 | CURLOPT_SSL_VERIFYHOST => 0, |
||
122 | CURLOPT_SSL_VERIFYPEER => 0, |
||
123 | CURLOPT_VERBOSE => 1 |
||
124 | ); |
||
125 | |||
126 | $ch = curl_init(self::getAppConfig()->getAppValue('converter_url') . '?target_format=' . $targetExtension); |
||
127 | curl_setopt_array($ch, $options); |
||
128 | $content = curl_exec($ch); |
||
129 | if (curl_errno($ch)){ |
||
130 | \OC::$server->getLogger()->debug( |
||
131 | 'cURL error' . curl_errno($ch) . ':' . curl_error($ch), |
||
132 | ['app' => 'documents'] |
||
133 | |||
134 | ); |
||
135 | } |
||
136 | curl_close($ch); |
||
137 | |||
138 | return $content; |
||
139 | } |
||
140 | |||
141 | protected static function getAppConfig(){ |
||
146 | |||
147 | } |
||
148 |