Skip to content

Commit

Permalink
Allow specifying of sizes to generate on cli
Browse files Browse the repository at this point in the history
Fixes #78

Signed-off-by: Roeland Jago Douma <[email protected]>
  • Loading branch information
rullzer committed Dec 21, 2017
1 parent 89ec15d commit d94e3ca
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 60 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.0.8 - 2017-12-21

### Fixes
- Do not add file if it already is in the table [#86](https://github.com/rullzer/previewgenerator/pull/86)

### Added
- Allow to specify sizes to be generated [#87](https://github.com/rullzer/previewgenerator/pull/86)

## 1.0.7 - 2017-09-26

### Fixes
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ in the Nextcloud server. Improvements in this area are coming with Nextcloud 12

Yes this happens when the `pre-generate` command crashes. No worries the lock
will be released after 30 minutes of inactivity from the app. So go grab a cookie.

### I don't want to generate all the preview sizes

This is possible since version 1.0.8. Just set the correct values via the command line

```
./occ config:app:set --value="32 64 1024" previewgenerator squareSizes
./occ config:app:set --value="64 128 1024" previewgenerator widthSizes
./occ config:app:set --value="64 256 1024" previewgenerator heightSizes
```

This will only generate:
* square previews ofL 32x32, 64x64 and 1024x1024
* aspect ratio previews with a width of: 64, 128 and 1024
* aspect ratio previews with a height of: 64, 256 and 1024

Note:
* preview sizes are always a power of 2.
* The smallest size is 32
* The max size is determined by your preview settings in config.php
32 changes: 2 additions & 30 deletions lib/Command/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\PreviewGenerator\Command;

use OCA\PreviewGenerator\SizeHelper;
use OCP\Encryption\IManager;
use OCP\Files\File;
use OCP\Files\Folder;
Expand Down Expand Up @@ -107,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->output = $output;

$userId = $input->getArgument('user_id');
$this->calculateSizes();
$this->sizes = SizeHelper::calculateSizes($this->config);

if ($userId === null) {
$this->userManager->callForSeenUsers(function (IUser $user) {
Expand All @@ -123,35 +124,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
return 0;
}

private function calculateSizes() {
$this->sizes = [
'square' => [],
'height' => [],
'width' => [],
];

$maxW = (int)$this->config->getSystemValue('preview_max_x', 2048);
$maxH = (int)$this->config->getSystemValue('preview_max_y', 2048);

$s = 32;
while ($s <= $maxW || $s <= $maxH) {
$this->sizes['square'][] = $s;
$s *= 2;
}

$w = 32;
while ($w <= $maxW) {
$this->sizes['width'][] = $w;
$w *= 2;
}

$h = 32;
while ($h <= $maxH) {
$this->sizes['height'][] = $h;
$h *= 2;
}
}

/**
* @param IUser $user
*/
Expand Down
32 changes: 2 additions & 30 deletions lib/Command/PreGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace OCA\PreviewGenerator\Command;

use OCA\PreviewGenerator\SizeHelper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Encryption\IManager;
use OCP\Files\File;
Expand Down Expand Up @@ -132,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->updateLastActivity();
$this->output = $output;

$this->calculateSizes();
$this->sizes = SizeHelper::calculateSizes($this->config);
$this->startProcessing();

$this->clearLastActivity();
Expand Down Expand Up @@ -230,35 +231,6 @@ private function processFile(File $file) {
$this->updateLastActivity();
}

private function calculateSizes() {
$this->sizes = [
'square' => [],
'height' => [],
'width' => [],
];

$maxW = (int)$this->config->getSystemValue('preview_max_x', 2048);
$maxH = (int)$this->config->getSystemValue('preview_max_y', 2048);

$s = 32;
while($s <= $maxW || $s <= $maxH) {
$this->sizes['square'][] = $s;
$s *= 2;
}

$w = 32;
while($w <= $maxW) {
$this->sizes['width'][] = $w;
$w *= 2;
}

$h = 32;
while($h <= $maxH) {
$this->sizes['height'][] = $h;
$h *= 2;
}
}

private function updateLastActivity() {
$this->config->setAppValue($this->appName, 'lastActivity', $this->time->getTime());
}
Expand Down
100 changes: 100 additions & 0 deletions lib/SizeHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* @copyright Copyright (c) 2017, Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\PreviewGenerator;

use OCP\IConfig;

class SizeHelper {

public static function calculateSizes(IConfig $config) {
/*
* First calculate the systems max sizes
*/

$sizes = [
'square' => [],
'height' => [],
'width' => [],
];

$maxW = (int)$config->getSystemValue('preview_max_x', 2048);
$maxH = (int)$config->getSystemValue('preview_max_y', 2048);

$s = 32;
while($s <= $maxW || $s <= $maxH) {
$sizes['square'][] = $s;
$s *= 2;
}

$w = 32;
while($w <= $maxW) {
$sizes['width'][] = $w;
$w *= 2;
}

$h = 32;
while($h <= $maxH) {
$sizes['height'][] = $h;
$h *= 2;
}


/*
* Now calculate the user provided max sizes
* Note that only powers of 2 matter but if users supply different
* stuff it is their own fault and we just ignore it
*/
$getCustomSizes = function(IConfig $config, $key) {
$TXT = $config->getAppValue('previewgenerator', $key, '');
$values = [];
if ($TXT !== '') {
foreach (explode(' ', $TXT) as $value) {
if (ctype_digit($value)) {
$values[] = (int)$value;
}
}
}

return $values;
};

$squares = $getCustomSizes($config, 'squareSizes');
$widths = $getCustomSizes($config, 'widthSizes');
$heights = $getCustomSizes($config, 'heightSizes');

if ($squares !== []) {
$sizes['square'] = array_intersect($sizes['square'], $squares);
}

if ($widths !== []) {
$sizes['width'] = array_intersect($sizes['width'], $widths);
}

if ($heights !== []) {
$sizes['height'] = array_intersect($sizes['height'], $heights);
}

return $sizes;
}
}

0 comments on commit d94e3ca

Please sign in to comment.