Passed
Push — master ( bd5a29...9708b0 )
by Morris
23:14 queued 11:02
created

RepairMimeTypes::insertStmt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Olivier Paroz <[email protected]>
10
 * @author Rello <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Stefan Weil <[email protected]>
13
 * @author Thomas Ebert <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Victor Dubiniuk <[email protected]>
16
 * @author Vincent Petry <[email protected]>
17
 *
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
34
namespace OC\Repair;
35
36
use OCP\Migration\IOutput;
37
use OCP\Migration\IRepairStep;
38
39
class RepairMimeTypes implements IRepairStep {
40
	/**
41
	 * @var \OCP\IConfig
42
	 */
43
	protected $config;
44
45
	/**
46
	 * @var int
47
	 */
48
	protected $folderMimeTypeId;
49
50
	/**
51
	 * @param \OCP\IConfig $config
52
	 */
53
	public function __construct($config) {
54
		$this->config = $config;
55
	}
56
57
	public function getName() {
58
		return 'Repair mime types';
59
	}
60
61
	private static function existsStmt() {
62
		return \OC_DB::prepare('
63
			SELECT count(`mimetype`)
64
			FROM   `*PREFIX*mimetypes`
65
			WHERE  `mimetype` = ?
66
		');
67
	}
68
69
	private static function getIdStmt() {
70
		return \OC_DB::prepare('
71
			SELECT `id`
72
			FROM   `*PREFIX*mimetypes`
73
			WHERE  `mimetype` = ?
74
		');
75
	}
76
77
	private static function insertStmt() {
78
		return \OC_DB::prepare('
79
			INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
80
			VALUES ( ? )
81
		');
82
	}
83
84
	private static function updateByNameStmt() {
85
		return \OC_DB::prepare('
86
			UPDATE `*PREFIX*filecache`
87
			SET `mimetype` = ?
88
			WHERE `mimetype` <> ? AND `mimetype` <> ? AND `name` ILIKE ?
89
		');
90
	}
91
92
	private function updateMimetypes($updatedMimetypes) {
93
		if (empty($this->folderMimeTypeId)) {
94
			$result = \OC_DB::executeAudited(self::getIdStmt(), ['httpd/unix-directory']);
95
			$this->folderMimeTypeId = (int)$result->fetchOne();
96
		}
97
98
		$count = 0;
99
		foreach ($updatedMimetypes as $extension => $mimetype) {
100
			$result = \OC_DB::executeAudited(self::existsStmt(), [$mimetype]);
101
			$exists = $result->fetchOne();
102
103
			if (!$exists) {
104
				// insert mimetype
105
				\OC_DB::executeAudited(self::insertStmt(), [$mimetype]);
106
			}
107
108
			// get target mimetype id
109
			$result = \OC_DB::executeAudited(self::getIdStmt(), [$mimetype]);
110
			$mimetypeId = $result->fetchOne();
111
112
			// change mimetype for files with x extension
113
			$count += \OC_DB::executeAudited(self::updateByNameStmt(), [$mimetypeId, $this->folderMimeTypeId, $mimetypeId, '%.' . $extension]);
114
		}
115
116
		return $count;
117
	}
118
119
	private function introduceImageTypes() {
120
		$updatedMimetypes = [
121
			'jp2' => 'image/jp2',
122
			'webp' => 'image/webp',
123
		];
124
125
		return $this->updateMimetypes($updatedMimetypes);
126
	}
127
128
	private function introduceWindowsProgramTypes() {
129
		$updatedMimetypes = [
130
			'htaccess' => 'text/plain',
131
			'bat' => 'application/x-msdos-program',
132
			'cmd' => 'application/cmd',
133
		];
134
135
		return $this->updateMimetypes($updatedMimetypes);
136
	}
137
138
	private function introduceLocationTypes() {
139
		$updatedMimetypes = [
140
			'gpx' => 'application/gpx+xml',
141
			'kml' => 'application/vnd.google-earth.kml+xml',
142
			'kmz' => 'application/vnd.google-earth.kmz',
143
			'tcx' => 'application/vnd.garmin.tcx+xml',
144
		];
145
146
		return $this->updateMimetypes($updatedMimetypes);
147
	}
148
149
	private function introduceInternetShortcutTypes() {
150
		$updatedMimetypes = [
151
			'url' => 'application/internet-shortcut',
152
			'webloc' => 'application/internet-shortcut'
153
		];
154
155
		return $this->updateMimetypes($updatedMimetypes);
156
	}
157
158
	private function introduceStreamingTypes() {
159
		$updatedMimetypes = [
160
			'm3u' => 'audio/mpegurl',
161
			'm3u8' => 'audio/mpegurl',
162
			'pls' => 'audio/x-scpls'
163
		];
164
165
		return $this->updateMimetypes($updatedMimetypes);
166
	}
167
168
	private function introduceVisioTypes() {
169
		$updatedMimetypes = [
170
			'vsdm' => 'application/vnd.visio',
171
			'vsdx' => 'application/vnd.visio',
172
			'vssm' => 'application/vnd.visio',
173
			'vssx' => 'application/vnd.visio',
174
			'vstm' => 'application/vnd.visio',
175
			'vstx' => 'application/vnd.visio',
176
		];
177
178
		return $this->updateMimetypes($updatedMimetypes);
179
	}
180
181
	private function introduceComicbookTypes() {
182
		$updatedMimetypes = [
183
			'cb7' => 'application/comicbook+7z',
184
			'cba' => 'application/comicbook+ace',
185
			'cbr' => 'application/comicbook+rar',
186
			'cbt' => 'application/comicbook+tar',
187
			'cbtc' => 'application/comicbook+truecrypt',
188
			'cbz' => 'application/comicbook+zip',
189
		];
190
191
		return $this->updateMimetypes($updatedMimetypes);
192
	}
193
194
	private function introduceOpenDocumentTemplates() {
195
		$updatedMimetypes = [
196
			'ott' => 'application/vnd.oasis.opendocument.text-template',
197
			'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
198
			'otp' => 'application/vnd.oasis.opendocument.presentation-template',
199
			'otg' => 'application/vnd.oasis.opendocument.graphics-template',
200
		];
201
202
		return $this->updateMimetypes($updatedMimetypes);
203
	}
204
205
	/**
206
	 * Fix mime types
207
	 */
208
	public function run(IOutput $out) {
209
		$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
210
211
		// NOTE TO DEVELOPERS: when adding new mime types, please make sure to
212
		// add a version comparison to avoid doing it every time
213
214
		if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) {
215
			$out->info('Fixed image mime types');
216
		}
217
218
		if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
219
			$out->info('Fixed windows program mime types');
220
		}
221
222
		if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) {
223
			$out->info('Fixed geospatial mime types');
224
		}
225
226
		if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) {
227
			$out->info('Fixed internet-shortcut mime types');
228
		}
229
230
		if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) {
231
			$out->info('Fixed streaming mime types');
232
		}
233
234
		if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) {
235
			$out->info('Fixed visio mime types');
236
		}
237
238
		if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) {
239
			$out->info('Fixed comicbook mime types');
240
		}
241
242
		if (version_compare($ocVersionFromBeforeUpdate, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) {
243
			$out->info('Fixed OpenDocument template mime types');
244
		}
245
	}
246
}
247