summaryrefslogtreecommitdiffstats
path: root/lib/private/preview/office-cl.php
blob: 112909d652352c9d806a0fd98adeee4e8b806d93 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
 * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
namespace OC\Preview;

//we need imagick to convert
class Office extends Provider {

	private $cmd;

	public function getMimeType() {
		return null;
	}

	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
		$this->initCmd();
		if(is_null($this->cmd)) {
			return false;
		}

		$absPath = $fileview->toTmpFile($path);

		$tmpDir = get_temp_dir();

		$defaultParameters = ' --headless --nologo --nofirststartwizard --invisible --norestore -convert-to pdf -outdir ';
		$clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters);

		$exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
		$export = 'export HOME=/' . $tmpDir;

		shell_exec($export . "\n" . $exec);

		//create imagick object from pdf
		try{
			$pdf = new \imagick($absPath . '.pdf' . '[0]');
			$pdf->setImageFormat('jpg');
		} catch (\Exception $e) {
			unlink($absPath);
			unlink($absPath . '.pdf');
			\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
			return false;
		}

		$image = new \OC_Image($pdf);

		unlink($absPath);
		unlink($absPath . '.pdf');

		return $image->valid() ? $image : false;
	}

	private function initCmd() {
		$cmd = '';

		if(is_string(\OC_Config::getValue('preview_libreoffice_path', null))) {
			$cmd = \OC_Config::getValue('preview_libreoffice_path', null);
		}

		$whichLibreOffice = shell_exec('which libreoffice');
		if($cmd === '' && !empty($whichLibreOffice)) {
			$cmd = 'libreoffice';
		}

		$whichOpenOffice = shell_exec('which openoffice');
		if($cmd === '' && !empty($whichOpenOffice)) {
			$cmd = 'openoffice';
		}

		if($cmd === '') {
			$cmd = null;
		}

		$this->cmd = $cmd;
	}
}

//.doc, .dot
class MSOfficeDoc extends Office {

	public function getMimeType() {
		return '/application\/msword/';
	}

}

\OC\Preview::registerProvider('OC\Preview\MSOfficeDoc');

//.docm, .dotm, .xls(m), .xlt(m), .xla(m), .ppt(m), .pot(m), .pps(m), .ppa(m)
class MSOffice2003 extends Office {

	public function getMimeType() {
		return '/application\/vnd.ms-.*/';
	}

}

\OC\Preview::registerProvider('OC\Preview\MSOffice2003');

//.docx, .dotx, .xlsx, .xltx, .pptx, .potx, .ppsx
class MSOffice2007 extends Office {

	public function getMimeType() {
		return '/application\/vnd.openxmlformats-officedocument.*/';
	}

}

\OC\Preview::registerProvider('OC\Preview\MSOffice2007');

//.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt
class OpenDocument extends Office {

	public function getMimeType() {
		return '/application\/vnd.oasis.opendocument.*/';
	}

}

\OC\Preview::registerProvider('OC\Preview\OpenDocument');

//.sxw, .stw, .sxc, .stc, .sxd, .std, .sxi, .sti, .sxg, .sxm
class StarOffice extends Office {

	public function getMimeType() {
		return '/application\/vnd.sun.xml.*/';
	}

}

\OC\Preview::registerProvider('OC\Preview\StarOffice');