Welcome to mirror list, hosted at ThFree Co, Russian Federation.

repairmimetypes.php « repair « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f7618c6e06055c7c10f60f396888c76c80c7a0bb (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
<?php
/**
 * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
 * Copyright (c) 2014 Jörn Dreyer jfd@owncloud.com
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\Repair;

use OC\Hooks\BasicEmitter;

class RepairMimeTypes extends BasicEmitter implements \OC\RepairStep {

	public function getName() {
		return 'Repair mime types';
	}

	private function fixOfficeMimeTypes() {
		// update wrong mimetypes
		$wrongMimetypes = array(
			'application/mspowerpoint' => 'application/vnd.ms-powerpoint',
			'application/msexcel' => 'application/vnd.ms-excel',
		);

		$existsStmt = \OC_DB::prepare('
			SELECT count(`mimetype`)
			FROM   `*PREFIX*mimetypes`
			WHERE  `mimetype` = ?
		');

		$getIdStmt = \OC_DB::prepare('
			SELECT `id`
			FROM   `*PREFIX*mimetypes`
			WHERE  `mimetype` = ?
		');

		$insertStmt = \OC_DB::prepare('
			INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
			VALUES ( ? )
		');

		$updateWrongStmt = \OC_DB::prepare('
			UPDATE `*PREFIX*filecache`
			SET `mimetype` = (
				SELECT `id`
				FROM `*PREFIX*mimetypes`
				WHERE `mimetype` = ?
			) WHERE `mimetype` = ?
		');

		$deleteStmt = \OC_DB::prepare('
			DELETE FROM `*PREFIX*mimetypes`
			WHERE `id` = ?
		');

		foreach ($wrongMimetypes as $wrong => $correct) {


			// do we need to remove a wrong mimetype?
			$result = \OC_DB::executeAudited($getIdStmt, array($wrong));
			$wrongId = $result->fetchOne();

			if ($wrongId !== false) {

				// do we need to insert the correct mimetype?
				$result = \OC_DB::executeAudited($existsStmt, array($correct));
				$exists = $result->fetchOne();

				if ( ! $exists ) {
					// insert mimetype
					\OC_DB::executeAudited($insertStmt, array($correct));
				}

				// change wrong mimetype to correct mimetype in filecache
				\OC_DB::executeAudited($updateWrongStmt, array($correct, $wrongId));

				// delete wrong mimetype
				\OC_DB::executeAudited($deleteStmt, array($wrongId));

			}

		}

		$updatedMimetypes = array(
			'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
			'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
			'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
		);

		$updateByNameStmt = \OC_DB::prepare('
			UPDATE `*PREFIX*filecache`
			SET `mimetype` = (
				SELECT `id`
				FROM `*PREFIX*mimetypes`
				WHERE `mimetype` = ?
			) WHERE `name` LIKE ?
		');

		// separate doc from docx etc
		foreach ($updatedMimetypes as $extension => $mimetype ) {
			$result = \OC_DB::executeAudited($existsStmt, array($mimetype));
			$exists = $result->fetchOne();

			if ( ! $exists ) {
				// insert mimetype
				\OC_DB::executeAudited($insertStmt, array($mimetype));
			}

			// change mimetype for files with x extension
			\OC_DB::executeAudited($updateByNameStmt, array($mimetype, '%.'.$extension));
		}
	}

	/**
	 * Fix mime types
	 */
	public function run() {
		if ($this->fixOfficeMimeTypes()) {
			$this->emit('\OC\Repair', 'info', array('Fixed office mime types'));
		}
	}
}