2016-07-27 09:57:00 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-07-27 09:57:00 -04:00
|
|
|
/**
|
2024-05-30 14:13:41 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-07-27 09:57:00 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\WorkflowEngine\Check;
|
|
|
|
|
|
2019-09-05 09:52:11 -04:00
|
|
|
use OCA\WorkflowEngine\Entity\File;
|
2016-08-01 11:56:33 -04:00
|
|
|
use OCP\IL10N;
|
2016-07-27 09:57:00 -04:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
use OCP\Util;
|
|
|
|
|
use OCP\WorkflowEngine\ICheck;
|
|
|
|
|
|
|
|
|
|
class FileSize implements ICheck {
|
|
|
|
|
|
2025-09-16 08:31:21 -04:00
|
|
|
protected int|float|null $size = null;
|
2016-07-27 09:57:00 -04:00
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
2025-09-16 08:31:21 -04:00
|
|
|
protected readonly IL10N $l,
|
|
|
|
|
protected readonly IRequest $request,
|
2024-10-18 06:04:22 -04:00
|
|
|
) {
|
2016-07-27 09:57:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $operator
|
|
|
|
|
* @param string $value
|
|
|
|
|
*/
|
2025-09-16 08:31:21 -04:00
|
|
|
public function executeCheck($operator, $value): bool {
|
2016-07-27 09:57:00 -04:00
|
|
|
$size = $this->getFileSizeFromHeader();
|
2025-09-16 08:31:21 -04:00
|
|
|
if ($size === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-07-27 09:57:00 -04:00
|
|
|
|
|
|
|
|
$value = Util::computerFileSize($value);
|
2025-09-16 08:31:21 -04:00
|
|
|
return match ($operator) {
|
|
|
|
|
'less' => $size < $value,
|
|
|
|
|
'!less' => $size >= $value,
|
|
|
|
|
'greater' => $size > $value,
|
|
|
|
|
'!greater' => $size <= $value,
|
|
|
|
|
default => false,
|
|
|
|
|
};
|
2016-07-27 09:57:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $operator
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
|
*/
|
2025-09-16 08:31:21 -04:00
|
|
|
public function validateCheck($operator, $value): void {
|
2016-07-27 09:57:00 -04:00
|
|
|
if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) {
|
2016-08-01 11:56:33 -04:00
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
|
2016-07-27 09:57:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) {
|
2016-08-01 11:56:33 -04:00
|
|
|
throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2);
|
2016-07-27 09:57:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 08:31:21 -04:00
|
|
|
protected function getFileSizeFromHeader(): int|float|false {
|
2016-07-27 09:57:00 -04:00
|
|
|
if ($this->size !== null) {
|
|
|
|
|
return $this->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$size = $this->request->getHeader('OC-Total-Length');
|
2018-01-12 08:15:12 -05:00
|
|
|
if ($size === '') {
|
2016-07-27 09:57:00 -04:00
|
|
|
if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
|
|
|
|
|
$size = $this->request->getHeader('Content-Length');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 08:31:21 -04:00
|
|
|
if ($size === '' || !is_numeric($size)) {
|
2016-07-27 09:57:00 -04:00
|
|
|
$size = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 08:31:21 -04:00
|
|
|
$this->size = Util::numericToNumber($size);
|
2016-07-27 09:57:00 -04:00
|
|
|
return $this->size;
|
|
|
|
|
}
|
2019-09-05 09:52:11 -04:00
|
|
|
|
|
|
|
|
public function supportedEntities(): array {
|
|
|
|
|
return [ File::class ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isAvailableForScope(int $scope): bool {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-07-27 09:57:00 -04:00
|
|
|
}
|