|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OC\Core\BackgroundJobs; |
| 10 | + |
| 11 | +use OC\Preview\Db\Preview; |
| 12 | +use OC\Preview\Db\PreviewMapper; |
| 13 | +use OC\Preview\Storage\StorageFactory; |
| 14 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 15 | +use OCP\BackgroundJob\TimedJob; |
| 16 | +use OCP\DB\Exception; |
| 17 | +use OCP\Files\AppData\IAppDataFactory; |
| 18 | +use OCP\Files\IAppData; |
| 19 | +use OCP\Files\SimpleFS\ISimpleFile; |
| 20 | +use OCP\Files\SimpleFS\ISimpleFolder; |
| 21 | +use OCP\IAppConfig; |
| 22 | +use OCP\IDBConnection; |
| 23 | +use OCP\IPreview; |
| 24 | + |
| 25 | +class MovePreviewJob extends TimedJob { |
| 26 | + private IAppData $appData; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + ITimeFactory $time, |
| 30 | + private IAppConfig $appConfig, |
| 31 | + private PreviewMapper $previewMapper, |
| 32 | + private StorageFactory $storageFactory, |
| 33 | + private IDBConnection $connection, |
| 34 | + IAppDataFactory $appDataFactory, |
| 35 | + ) { |
| 36 | + parent::__construct($time); |
| 37 | + |
| 38 | + $this->appData = $appDataFactory->get('preview'); |
| 39 | + $this->setTimeSensitivity(self::TIME_INSENSITIVE); |
| 40 | + $this->setInterval(24 * 60 * 60); |
| 41 | + } |
| 42 | + |
| 43 | + protected function run(mixed $argument): void { |
| 44 | + try { |
| 45 | + $this->doRun($argument); |
| 46 | + } catch (\Throwable $exception) { |
| 47 | + echo $exception->getMessage(); |
| 48 | + throw $exception; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private function doRun($argument): void { |
| 53 | + if ($this->appConfig->getValueBool('core', 'previewMovedDone')) { |
| 54 | + //return; |
| 55 | + } |
| 56 | + |
| 57 | + $emptyHierarchicalPreviewFolders = false; |
| 58 | + |
| 59 | + $startTime = time(); |
| 60 | + while (true) { |
| 61 | + $previewFolders = []; |
| 62 | + |
| 63 | + // Check new hierarchical preview folders first |
| 64 | + if (!$emptyHierarchicalPreviewFolders) { |
| 65 | + $qb = $this->connection->getQueryBuilder(); |
| 66 | + $qb->select('*') |
| 67 | + ->from('filecache') |
| 68 | + ->where($qb->expr()->like('path', $qb->createNamedParameter('appdata_%/preview/%/%/%/%/%/%/%/%/%'))) |
| 69 | + ->setMaxResults(100); |
| 70 | + |
| 71 | + $result = $qb->executeQuery(); |
| 72 | + while ($row = $result->fetch()) { |
| 73 | + $pathSplit = explode('/', $row['path']); |
| 74 | + assert(count($pathSplit) >= 2); |
| 75 | + $fileId = $pathSplit[count($pathSplit) - 2]; |
| 76 | + $previewFolders[$fileId][] = $row['path']; |
| 77 | + } |
| 78 | + |
| 79 | + if (!empty($previewFolders)) { |
| 80 | + $this->processPreviews($previewFolders, false); |
| 81 | + continue; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // And then the flat preview folder (legacy) |
| 86 | + $emptyHierarchicalPreviewFolders = true; |
| 87 | + $qb = $this->connection->getQueryBuilder(); |
| 88 | + $qb->select('*') |
| 89 | + ->from('filecache') |
| 90 | + ->where($qb->expr()->like('path', $qb->createNamedParameter('appdata_%/preview/%/%.jpg'))) |
| 91 | + ->setMaxResults(100); |
| 92 | + |
| 93 | + $result = $qb->executeQuery(); |
| 94 | + while ($row = $result->fetch()) { |
| 95 | + $pathSplit = explode('/', $row['path']); |
| 96 | + assert(count($pathSplit) >= 2); |
| 97 | + $fileId = $pathSplit[count($pathSplit) - 2]; |
| 98 | + array_pop($pathSplit); |
| 99 | + $path = implode('/', $pathSplit); |
| 100 | + if (!isset($previewFolders[$fileId])) { |
| 101 | + $previewFolders[$fileId] = []; |
| 102 | + } |
| 103 | + if (!in_array($path, $previewFolders[$fileId])) { |
| 104 | + $previewFolders[$fileId][] = $path; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (empty($previewFolders)) { |
| 109 | + break; |
| 110 | + } else { |
| 111 | + $this->processPreviews($previewFolders, true); |
| 112 | + } |
| 113 | + |
| 114 | + // Stop if execution time is more than one hour. |
| 115 | + if (time() - $startTime > 3600) { |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Delete any left over preview directory |
| 121 | + $this->appData->getFolder('.')->delete(); |
| 122 | + $this->appConfig->setValueBool('core', 'previewMovedDone', true); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param array<string, string[]> $previewFolders |
| 127 | + */ |
| 128 | + private function processPreviews(array $previewFolders, bool $simplePaths): void { |
| 129 | + foreach ($previewFolders as $fileId => $previewFolder) { |
| 130 | + $internalPath = $this->getInternalFolder((string)$fileId, $simplePaths); |
| 131 | + $folder = $this->appData->getFolder($internalPath); |
| 132 | + |
| 133 | + /** |
| 134 | + * @var list<array{file: ISimpleFile, width: int, height: int, crop: bool, max: bool, extension: string, mtime: int, size: int}> $previewFiles |
| 135 | + */ |
| 136 | + $previewFiles = []; |
| 137 | + |
| 138 | + foreach ($folder->getDirectoryListing() as $previewFile) { |
| 139 | + [0 => $baseName, 1 => $extension] = explode('.', $previewFile->getName()); |
| 140 | + $nameSplit = explode('-', $baseName); |
| 141 | + |
| 142 | + // TODO VERSION/PREFIX extraction |
| 143 | + |
| 144 | + $width = $nameSplit[0]; |
| 145 | + $height = $nameSplit[1]; |
| 146 | + |
| 147 | + if (isset($nameSplit[2])) { |
| 148 | + $crop = $nameSplit[2] === 'crop'; |
| 149 | + $max = $nameSplit[2] === 'max'; |
| 150 | + } |
| 151 | + |
| 152 | + $previewFiles[] = [ |
| 153 | + 'file' => $previewFile, |
| 154 | + 'width' => $width, |
| 155 | + 'height' => $height, |
| 156 | + 'crop' => $crop, |
| 157 | + 'max' => $max, |
| 158 | + 'extension' => $extension, |
| 159 | + 'size' => $previewFile->getSize(), |
| 160 | + 'mtime' => $previewFile->getMTime(), |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + $qb = $this->connection->getQueryBuilder(); |
| 165 | + $qb->select('*') |
| 166 | + ->from('filecache') |
| 167 | + ->where($qb->expr()->like('fileid', $qb->createNamedParameter($fileId))); |
| 168 | + |
| 169 | + $result = $qb->executeQuery(); |
| 170 | + $result = $result->fetchAll(); |
| 171 | + |
| 172 | + if (count($result) > 0) { |
| 173 | + foreach ($previewFiles as $previewFile) { |
| 174 | + $preview = new Preview(); |
| 175 | + $preview->setFileId((int)$fileId); |
| 176 | + $preview->setStorageId($result[0]['storage']); |
| 177 | + $preview->setEtag($result[0]['etag']); |
| 178 | + $preview->setMtime($previewFile['mtime']); |
| 179 | + $preview->setWidth($previewFile['width']); |
| 180 | + $preview->setHeight($previewFile['height']); |
| 181 | + $preview->setCrop($previewFile['crop']); |
| 182 | + $preview->setIsMax($previewFile['max']); |
| 183 | + $preview->setMimetype(match ($previewFile['extension']) { |
| 184 | + 'png' => IPreview::MIMETYPE_PNG, |
| 185 | + 'webp' => IPreview::MIMETYPE_WEBP, |
| 186 | + 'gif' => IPreview::MIMETYPE_GIF, |
| 187 | + default => IPreview::MIMETYPE_JPEG, |
| 188 | + }); |
| 189 | + $preview->setSize($previewFile['size']); |
| 190 | + try { |
| 191 | + $preview = $this->previewMapper->insert($preview); |
| 192 | + } catch (Exception $e) { |
| 193 | + // We already have this preview in the preview table, skip |
| 194 | + continue; |
| 195 | + } |
| 196 | + |
| 197 | + try { |
| 198 | + $this->storageFactory->migratePreview($preview, $previewFile['file']); |
| 199 | + $previewFile['file']->delete(); |
| 200 | + } catch (Exception $e) { |
| 201 | + $this->previewMapper->delete($preview); |
| 202 | + throw $e; |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + $this->deleteFolder($internalPath, $folder); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + public static function getInternalFolder(string $name, bool $simplePaths): string { |
| 213 | + if ($simplePaths) { |
| 214 | + return '/' . $name; |
| 215 | + } |
| 216 | + return implode('/', str_split(substr(md5($name), 0, 7))) . '/' . $name; |
| 217 | + } |
| 218 | + |
| 219 | + private function deleteFolder(string $path, ISimpleFolder $folder): void { |
| 220 | + $folder->delete(); |
| 221 | + |
| 222 | + $current = $path; |
| 223 | + |
| 224 | + while (true) { |
| 225 | + $current = dirname($current); |
| 226 | + if ($current === '/' || $current === '.' || $current === '') { |
| 227 | + break; |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + $folder = $this->appData->getFolder($current); |
| 232 | + if (count($folder->getDirectoryListing()) !== 0) { |
| 233 | + break; |
| 234 | + } |
| 235 | + $folder->delete(); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments