@@ -37,210 +37,210 @@ |
||
| 37 | 37 | use OCP\Migration\IRepairStep; |
| 38 | 38 | |
| 39 | 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(' |
|
| 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 | 63 | SELECT count(`mimetype`) |
| 64 | 64 | FROM `*PREFIX*mimetypes` |
| 65 | 65 | WHERE `mimetype` = ? |
| 66 | 66 | '); |
| 67 | - } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - private static function getIdStmt() { |
|
| 70 | - return \OC_DB::prepare(' |
|
| 69 | + private static function getIdStmt() { |
|
| 70 | + return \OC_DB::prepare(' |
|
| 71 | 71 | SELECT `id` |
| 72 | 72 | FROM `*PREFIX*mimetypes` |
| 73 | 73 | WHERE `mimetype` = ? |
| 74 | 74 | '); |
| 75 | - } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - private static function insertStmt() { |
|
| 78 | - return \OC_DB::prepare(' |
|
| 77 | + private static function insertStmt() { |
|
| 78 | + return \OC_DB::prepare(' |
|
| 79 | 79 | INSERT INTO `*PREFIX*mimetypes` ( `mimetype` ) |
| 80 | 80 | VALUES ( ? ) |
| 81 | 81 | '); |
| 82 | - } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - private static function updateByNameStmt() { |
|
| 85 | - return \OC_DB::prepare(' |
|
| 84 | + private static function updateByNameStmt() { |
|
| 85 | + return \OC_DB::prepare(' |
|
| 86 | 86 | UPDATE `*PREFIX*filecache` |
| 87 | 87 | SET `mimetype` = ? |
| 88 | 88 | WHERE `mimetype` <> ? AND `mimetype` <> ? AND `name` ILIKE ? |
| 89 | 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 | - } |
|
| 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 | 246 | } |