office-cl.php 2.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?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;

10
11
	//we need imagick to convert
	class Office extends Provider {
12

13
		private $cmd;
14

15
16
		public function getMimeType() {
			return null;
17
18
		}

19
20
21
22
23
24
25
26
27
		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();
28

Olivier Paroz's avatar
Olivier Paroz committed
29
			$defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId().'/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to pdf --outdir ';
30
			$clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters);
31

32
			$exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
Georg Ehrke's avatar
Georg Ehrke committed
33

josh4trunks's avatar
josh4trunks committed
34
			shell_exec($exec);
35

36
37
38
39
40
41
42
43
44
45
46
			//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;
			}

47
48
			$image = new \OC_Image();
			$image->loadFromData($pdf);
49

Georg Ehrke's avatar
Georg Ehrke committed
50
51
			unlink($absPath);
			unlink($absPath . '.pdf');
52
53

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

56
57
		private function initCmd() {
			$cmd = '';
58

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

63
			$whichLibreOffice = shell_exec('command -v libreoffice');
64
65
66
			if($cmd === '' && !empty($whichLibreOffice)) {
				$cmd = 'libreoffice';
			}
67

68
			$whichOpenOffice = shell_exec('command -v openoffice');
69
70
71
			if($cmd === '' && !empty($whichOpenOffice)) {
				$cmd = 'openoffice';
			}
72

73
74
75
			if($cmd === '') {
				$cmd = null;
			}
76

77
			$this->cmd = $cmd;
78
		}
79
	}
80

81
82
	//.doc, .dot
	class MSOfficeDoc extends Office {
83

84
85
		public function getMimeType() {
			return '/application\/msword/';
86
		}
87

88
89
	}

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

93
94
95
		public function getMimeType() {
			return '/application\/vnd.ms-.*/';
		}
96
97
98

	}

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

102
103
104
		public function getMimeType() {
			return '/application\/vnd.openxmlformats-officedocument.*/';
		}
105
106
107

	}

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

111
112
113
		public function getMimeType() {
			return '/application\/vnd.oasis.opendocument.*/';
		}
Georg Ehrke's avatar
Georg Ehrke committed
114

115
116
	}

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

120
121
122
		public function getMimeType() {
			return '/application\/vnd.sun.xml.*/';
		}
123
124

	}