Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Preview often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Preview, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Preview { |
||
| 39 | //the thumbnail folder |
||
| 40 | const THUMBNAILS_FOLDER = 'thumbnails'; |
||
| 41 | |||
| 42 | const MODE_FILL = 'fill'; |
||
| 43 | const MODE_COVER = 'cover'; |
||
| 44 | |||
| 45 | //config |
||
| 46 | private $maxScaleFactor; |
||
| 47 | /** @var int maximum width allowed for a preview */ |
||
| 48 | private $configMaxWidth; |
||
| 49 | /** @var int maximum height allowed for a preview */ |
||
| 50 | private $configMaxHeight; |
||
| 51 | |||
| 52 | //fileview object |
||
| 53 | private $fileView = null; |
||
| 54 | private $userView = null; |
||
| 55 | |||
| 56 | //vars |
||
| 57 | private $file; |
||
| 58 | private $maxX; |
||
| 59 | private $maxY; |
||
| 60 | private $scalingUp; |
||
| 61 | private $mimeType; |
||
| 62 | private $keepAspect = false; |
||
| 63 | private $mode = self::MODE_FILL; |
||
| 64 | |||
| 65 | //used to calculate the size of the preview to generate |
||
| 66 | /** @var int $maxPreviewWidth max width a preview can have */ |
||
| 67 | private $maxPreviewWidth; |
||
| 68 | /** @var int $maxPreviewHeight max height a preview can have */ |
||
| 69 | private $maxPreviewHeight; |
||
| 70 | /** @var int $previewWidth calculated width of the preview we're looking for */ |
||
| 71 | private $previewWidth; |
||
| 72 | /** @var int $previewHeight calculated height of the preview we're looking for */ |
||
| 73 | private $previewHeight; |
||
| 74 | |||
| 75 | //filemapper used for deleting previews |
||
| 76 | // index is path, value is fileinfo |
||
| 77 | static public $deleteFileMapper = array(); |
||
| 78 | static public $deleteChildrenMapper = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * preview images object |
||
| 82 | * |
||
| 83 | * @var \OCP\IImage |
||
| 84 | */ |
||
| 85 | private $preview; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var \OCP\Files\FileInfo |
||
| 89 | */ |
||
| 90 | protected $info; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * check if thumbnail or bigger version of thumbnail of file is cached |
||
| 94 | * |
||
| 95 | * @param string $user userid - if no user is given, OC_User::getUser will be used |
||
| 96 | * @param string $root path of root |
||
| 97 | * @param string $file The path to the file where you want a thumbnail from |
||
| 98 | * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the |
||
| 99 | * shape of the image |
||
| 100 | * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the |
||
| 101 | * shape of the image |
||
| 102 | * @param bool $scalingUp Disable/Enable upscaling of previews |
||
| 103 | * |
||
| 104 | * @throws \Exception |
||
| 105 | * @return mixed (bool / string) |
||
|
|
|||
| 106 | * false if thumbnail does not exist |
||
| 107 | * path to thumbnail if thumbnail exists |
||
| 108 | */ |
||
| 109 | public function __construct( |
||
| 110 | $user = '', |
||
| 111 | $root = '/', |
||
| 112 | $file = '', $maxX = 1, |
||
| 113 | $maxY = 1, |
||
| 114 | $scalingUp = true |
||
| 115 | ) { |
||
| 116 | //init fileviews |
||
| 117 | if ($user === '') { |
||
| 118 | $user = \OC_User::getUser(); |
||
| 119 | } |
||
| 120 | $this->fileView = new \OC\Files\View('/' . $user . '/' . $root); |
||
| 121 | $this->userView = new \OC\Files\View('/' . $user); |
||
| 122 | |||
| 123 | //set config |
||
| 124 | $sysConfig = \OC::$server->getConfig(); |
||
| 125 | $this->configMaxWidth = $sysConfig->getSystemValue('preview_max_x', 2048); |
||
| 126 | $this->configMaxHeight = $sysConfig->getSystemValue('preview_max_y', 2048); |
||
| 127 | $this->maxScaleFactor = $sysConfig->getSystemValue('preview_max_scale_factor', 2); |
||
| 128 | |||
| 129 | //save parameters |
||
| 130 | $this->setFile($file); |
||
| 131 | $this->setMaxX((int)$maxX); |
||
| 132 | $this->setMaxY((int)$maxY); |
||
| 133 | $this->setScalingUp($scalingUp); |
||
| 134 | |||
| 135 | $this->preview = null; |
||
| 136 | |||
| 137 | //check if there are preview backends |
||
| 138 | if (!\OC::$server->getPreviewManager() |
||
| 139 | ->hasProviders() |
||
| 140 | && \OC::$server->getConfig() |
||
| 141 | ->getSystemValue('enable_previews', true) |
||
| 142 | ) { |
||
| 143 | \OCP\Util::writeLog('core', 'No preview providers exist', \OCP\Util::ERROR); |
||
| 144 | throw new \Exception('No preview providers'); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * returns the path of the file you want a thumbnail from |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getFile() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * returns the max width of the preview |
||
| 159 | * |
||
| 160 | * @return integer |
||
| 161 | */ |
||
| 162 | public function getMaxX() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * returns the max height of the preview |
||
| 168 | * |
||
| 169 | * @return integer |
||
| 170 | */ |
||
| 171 | public function getMaxY() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * returns whether or not scalingup is enabled |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function getScalingUp() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * returns the name of the thumbnailfolder |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getThumbnailsFolder() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * returns the max scale factor |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getMaxScaleFactor() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * returns the max width set in ownCloud's config |
||
| 204 | * |
||
| 205 | * @return integer |
||
| 206 | */ |
||
| 207 | public function getConfigMaxX() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * returns the max height set in ownCloud's config |
||
| 213 | * |
||
| 214 | * @return integer |
||
| 215 | */ |
||
| 216 | public function getConfigMaxY() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns the FileInfo object associated with the file to preview |
||
| 222 | * |
||
| 223 | * @return false|Files\FileInfo|\OCP\Files\FileInfo |
||
| 224 | */ |
||
| 225 | protected function getFileInfo() { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @return array|null |
||
| 240 | */ |
||
| 241 | private function getChildren() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Sets the path of the file you want a preview of |
||
| 254 | * |
||
| 255 | * @param string $file |
||
| 256 | * @param \OCP\Files\FileInfo|null $info |
||
| 257 | * |
||
| 258 | * @return \OC\Preview |
||
| 259 | */ |
||
| 260 | public function setFile($file, $info = null) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Forces the use of a specific media type |
||
| 276 | * |
||
| 277 | * @param string $mimeType |
||
| 278 | */ |
||
| 279 | public function setMimetype($mimeType) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Sets the max width of the preview. It's capped by the maximum allowed size set in the |
||
| 285 | * configuration |
||
| 286 | * |
||
| 287 | * @param int $maxX |
||
| 288 | * |
||
| 289 | * @throws \Exception |
||
| 290 | * @return \OC\Preview |
||
| 291 | */ |
||
| 292 | public function setMaxX($maxX = 1) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Sets the max height of the preview. It's capped by the maximum allowed size set in the |
||
| 305 | * configuration |
||
| 306 | * |
||
| 307 | * @param int $maxY |
||
| 308 | * |
||
| 309 | * @throws \Exception |
||
| 310 | * @return \OC\Preview |
||
| 311 | */ |
||
| 312 | public function setMaxY($maxY = 1) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sets whether we're allowed to scale up when generating a preview. It's capped by the maximum |
||
| 325 | * allowed scale factor set in the configuration |
||
| 326 | * |
||
| 327 | * @param bool $scalingUp |
||
| 328 | * |
||
| 329 | * @return \OC\Preview |
||
| 330 | */ |
||
| 331 | public function setScalingup($scalingUp) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Set whether to cover or fill the specified dimensions |
||
| 342 | * |
||
| 343 | * @param string $mode |
||
| 344 | * |
||
| 345 | * @return \OC\Preview |
||
| 346 | */ |
||
| 347 | public function setMode($mode) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Sets whether we need to generate a preview which keeps the aspect ratio of the original file |
||
| 355 | * |
||
| 356 | * @param bool $keepAspect |
||
| 357 | * |
||
| 358 | * @return \OC\Preview |
||
| 359 | */ |
||
| 360 | public function setKeepAspect($keepAspect) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Makes sure we were given a file to preview and that it exists in the filesystem |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function isFileValid() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Deletes the preview of a file with specific width and height |
||
| 390 | * |
||
| 391 | * This should never delete the max preview, use deleteAllPreviews() instead |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function deletePreview() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Deletes all previews of a file |
||
| 411 | */ |
||
| 412 | public function deleteAllPreviews() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Checks if a preview matching the asked dimensions or a bigger version is already cached |
||
| 439 | * |
||
| 440 | * * We first retrieve the size of the max preview since this is what we be used to create |
||
| 441 | * all our preview. If it doesn't exist we return false, so that it can be generated |
||
| 442 | * * Using the dimensions of the max preview, we calculate what the size of the new |
||
| 443 | * thumbnail should be |
||
| 444 | * * And finally, we look for a suitable candidate in the cache |
||
| 445 | * |
||
| 446 | * @param int $fileId fileId of the original file we need a preview of |
||
| 447 | * |
||
| 448 | * @return string|false path to the cached preview if it exists or false |
||
| 449 | */ |
||
| 450 | public function isCached($fileId) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Returns the dimensions of the max preview |
||
| 513 | * |
||
| 514 | * @param FileInfo[] $allThumbnails the list of all our cached thumbnails |
||
| 515 | * |
||
| 516 | * @return int[] |
||
| 517 | */ |
||
| 518 | private function getMaxPreviewSize($allThumbnails) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Check if a specific thumbnail size is cached |
||
| 535 | * |
||
| 536 | * @param FileInfo[] $allThumbnails the list of all our cached thumbnails |
||
| 537 | * @param string $name |
||
| 538 | * @return bool |
||
| 539 | */ |
||
| 540 | private function thumbnailSizeExists(array $allThumbnails, $name) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Determines the size of the preview we should be looking for in the cache |
||
| 553 | * |
||
| 554 | * @return integer[] |
||
| 555 | */ |
||
| 556 | private function simulatePreviewDimensions() { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Resizes the boundaries to match the aspect ratio |
||
| 572 | * |
||
| 573 | * @param int $askedWidth |
||
| 574 | * @param int $askedHeight |
||
| 575 | * |
||
| 576 | * @param int $originalWidth |
||
| 577 | * @param int $originalHeight |
||
| 578 | * @return integer[] |
||
| 579 | */ |
||
| 580 | private function applyAspectRatio($askedWidth, $askedHeight, $originalWidth = 0, $originalHeight = 0) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Resizes the boundaries to cover the area |
||
| 605 | * |
||
| 606 | * @param int $askedWidth |
||
| 607 | * @param int $askedHeight |
||
| 608 | * @param int $previewWidth |
||
| 609 | * @param int $previewHeight |
||
| 610 | * @return integer[] |
||
| 611 | */ |
||
| 612 | private function applyCover($askedWidth, $askedHeight, $previewWidth, $previewHeight) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Makes sure an upscaled preview doesn't end up larger than the max dimensions defined in the |
||
| 631 | * config |
||
| 632 | * |
||
| 633 | * @param int $askedWidth |
||
| 634 | * @param int $askedHeight |
||
| 635 | * |
||
| 636 | * @return integer[] |
||
| 637 | */ |
||
| 638 | private function fixSize($askedWidth, $askedHeight) { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Checks if a bigger version of a file preview is cached and if not |
||
| 649 | * return the preview of max allowed dimensions |
||
| 650 | * |
||
| 651 | * @param int $fileId fileId of the original image |
||
| 652 | * @param FileInfo[] $allThumbnails the list of all our cached thumbnails |
||
| 653 | * |
||
| 654 | * @return string path to bigger thumbnail |
||
| 655 | */ |
||
| 656 | private function isCachedBigger($fileId, $allThumbnails) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Get possible bigger thumbnails of the given image with the proper aspect ratio |
||
| 677 | * |
||
| 678 | * @param FileInfo[] $allThumbnails the list of all our cached thumbnails |
||
| 679 | * |
||
| 680 | * @return string[] an array of paths to bigger thumbnails |
||
| 681 | */ |
||
| 682 | private function getPossibleThumbnails($allThumbnails) { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Looks at the preview filename from the cache and extracts the size of the preview |
||
| 709 | * |
||
| 710 | * @param string $name |
||
| 711 | * |
||
| 712 | * @return array<int,int,float> |
||
| 713 | */ |
||
| 714 | private function getDimensionsFromFilename($name) { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @param int $x |
||
| 725 | * @param int $y |
||
| 726 | * |
||
| 727 | * @return bool |
||
| 728 | */ |
||
| 729 | private function unscalable($x, $y) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Returns a preview of a file |
||
| 752 | * |
||
| 753 | * The cache is searched first and if nothing usable was found then a preview is |
||
| 754 | * generated by one of the providers |
||
| 755 | * |
||
| 756 | * @return \OCP\IImage |
||
| 757 | */ |
||
| 758 | public function getPreview() { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Sends the preview, including the headers to client which requested it |
||
| 789 | * |
||
| 790 | * @param null|string $mimeTypeForHeaders the media type to use when sending back the reply |
||
| 791 | * |
||
| 792 | * @throws NotFoundException |
||
| 793 | */ |
||
| 794 | public function showPreview($mimeTypeForHeaders = null) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Retrieves the preview from the cache and resizes it if necessary |
||
| 815 | * |
||
| 816 | * @param int $fileId fileId of the original image |
||
| 817 | * @param string $cached the path to the cached preview |
||
| 818 | */ |
||
| 819 | private function getCachedPreview($fileId, $cached) { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Resizes, crops, fixes orientation and stores in the cache |
||
| 848 | * |
||
| 849 | * @param int $fileId fileId of the original image |
||
| 850 | */ |
||
| 851 | private function resizeAndStore($fileId) { |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Calculates the new dimensions of the preview |
||
| 922 | * |
||
| 923 | * The new dimensions can be larger or smaller than the ones of the preview we have to resize |
||
| 924 | * |
||
| 925 | * @param \OCP\IImage $image |
||
| 926 | * @param int $askedWidth |
||
| 927 | * @param int $askedHeight |
||
| 928 | * @param int $previewWidth |
||
| 929 | * @param int $previewHeight |
||
| 930 | * |
||
| 931 | * @return int[] |
||
| 932 | */ |
||
| 933 | private function scale($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) { |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Crops a preview which is larger than the dimensions we've received |
||
| 974 | * |
||
| 975 | * @param \OCP\IImage $image |
||
| 976 | * @param int $askedWidth |
||
| 977 | * @param int $askedHeight |
||
| 978 | * @param int $previewWidth |
||
| 979 | * @param int $previewHeight |
||
| 980 | */ |
||
| 981 | private function crop($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight = null) { |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Crops an image if it's larger than the dimensions we've received and fills the empty space |
||
| 992 | * with a transparent background |
||
| 993 | * |
||
| 994 | * @param \OCP\IImage $image |
||
| 995 | * @param int $askedWidth |
||
| 996 | * @param int $askedHeight |
||
| 997 | * @param int $previewWidth |
||
| 998 | * @param int $previewHeight |
||
| 999 | */ |
||
| 1000 | private function cropAndFill($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) { |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * Saves a preview in the cache to speed up future calls |
||
| 1038 | * |
||
| 1039 | * Do not nullify the preview as it might send the whole process in a loop |
||
| 1040 | * |
||
| 1041 | * @param int $fileId fileId of the original image |
||
| 1042 | * @param int $previewWidth |
||
| 1043 | * @param int $previewHeight |
||
| 1044 | */ |
||
| 1045 | private function storePreview($fileId, $previewWidth, $previewHeight) { |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Returns the path to a preview based on its dimensions and aspect |
||
| 1060 | * |
||
| 1061 | * @param int $fileId |
||
| 1062 | * @param int|null $maxX |
||
| 1063 | * @param int|null $maxY |
||
| 1064 | * |
||
| 1065 | * @return string |
||
| 1066 | */ |
||
| 1067 | private function buildCachePath($fileId, $maxX = null, $maxY = null) { |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Returns the path to the folder where the previews are stored, identified by the fileId |
||
| 1095 | * |
||
| 1096 | * @param int $fileId |
||
| 1097 | * |
||
| 1098 | * @return string |
||
| 1099 | */ |
||
| 1100 | private function getPreviewPath($fileId) { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Asks the provider to send a preview of the file which respects the maximum dimensions |
||
| 1106 | * defined in the configuration and after saving it in the cache, it is then resized to the |
||
| 1107 | * asked dimensions |
||
| 1108 | * |
||
| 1109 | * This is only called once in order to generate a large PNG of dimensions defined in the |
||
| 1110 | * configuration file. We'll be able to quickly resize it later on. |
||
| 1111 | * We never upscale the original conversion as this will be done later by the resizing |
||
| 1112 | * operation |
||
| 1113 | * |
||
| 1114 | * @param int $fileId fileId of the original image |
||
| 1115 | */ |
||
| 1116 | private function generatePreview($fileId) { |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Defines the media icon, for the media type of the original file, as the preview |
||
| 1174 | */ |
||
| 1175 | private function getMimeIcon() { |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Stores the max preview in the cache |
||
| 1190 | * |
||
| 1191 | * @param string $previewPath path to the preview |
||
| 1192 | */ |
||
| 1193 | private function storeMaxPreview($previewPath) { |
||
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Limits a dimension to the maximum dimension provided as argument |
||
| 1221 | * |
||
| 1222 | * @param int $dim |
||
| 1223 | * @param int $maxDim |
||
| 1224 | * @param string $dimName |
||
| 1225 | * |
||
| 1226 | * @return integer |
||
| 1227 | */ |
||
| 1228 | private function limitMaxDim($dim, $maxDim, $dimName) { |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * @param array $args |
||
| 1243 | */ |
||
| 1244 | public static function post_write($args) { |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * @param array $args |
||
| 1250 | */ |
||
| 1251 | public static function prepare_delete_files($args) { |
||
| 1254 | |||
| 1255 | /** |
||
| 1256 | * @param array $args |
||
| 1257 | * @param string $prefix |
||
| 1258 | */ |
||
| 1259 | public static function prepare_delete(array $args, $prefix = '') { |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * @param string $absolutePath |
||
| 1281 | * @param \OCP\Files\FileInfo $info |
||
| 1282 | */ |
||
| 1283 | private static function addPathToDeleteFileMapper($absolutePath, $info) { |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * @param \OC\Files\View $view |
||
| 1289 | * @param string $path |
||
| 1290 | * |
||
| 1291 | * @return array |
||
| 1292 | */ |
||
| 1293 | private static function getAllChildren($view, $path) { |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * @param array $args |
||
| 1319 | */ |
||
| 1320 | public static function post_delete_files($args) { |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * @param array $args |
||
| 1326 | */ |
||
| 1327 | public static function post_delete_versions($args) { |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * @param array $args |
||
| 1333 | * @param string $prefix |
||
| 1334 | */ |
||
| 1335 | public static function post_delete($args, $prefix = '') { |
||
| 1341 | |||
| 1342 | } |
||
| 1343 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.