123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace OC;
- use bantu\IniGetWrapper\IniGetWrapper;
- class LargeFileHelper {
-
- public const POW_2_53 = '9007199254740992';
-
- public const POW_2_53_MINUS_1 = '9007199254740991';
-
- public function __construct() {
- $pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0;
- if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
- throw new \RuntimeException(
- 'This class assumes floats to be double precision or "better".'
- );
- }
- }
-
- public function formatUnsignedInteger(int|float|string $number): string {
- if (is_float($number)) {
-
- return number_format($number, 0, '', '');
- } elseif (is_string($number) && ctype_digit($number)) {
- return $number;
- } elseif (is_int($number)) {
-
- return sprintf('%u', $number);
- } else {
- throw new \UnexpectedValueException(
- 'Expected int, float or base-10 string'
- );
- }
- }
-
- public function getFileSize(string $filename): int|float {
- $fileSize = $this->getFileSizeViaCurl($filename);
- if (!is_null($fileSize)) {
- return $fileSize;
- }
- $fileSize = $this->getFileSizeViaExec($filename);
- if (!is_null($fileSize)) {
- return $fileSize;
- }
- return $this->getFileSizeNative($filename);
- }
-
- public function getFileSizeViaCurl(string $fileName): null|int|float {
- if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
- $encodedFileName = rawurlencode($fileName);
- $ch = curl_init("file:///$encodedFileName");
- curl_setopt($ch, CURLOPT_NOBODY, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data !== false) {
- $matches = [];
- preg_match('/Content-Length: (\d+)/', $data, $matches);
- if (isset($matches[1])) {
- return 0 + $matches[1];
- }
- }
- }
- return null;
- }
-
- public function getFileSizeViaExec(string $filename): null|int|float {
- if (\OCP\Util::isFunctionEnabled('exec')) {
- $os = strtolower(php_uname('s'));
- $arg = escapeshellarg($filename);
- $result = null;
- if (str_contains($os, 'linux')) {
- $result = $this->exec("stat -c %s $arg");
- } elseif (str_contains($os, 'bsd') || str_contains($os, 'darwin')) {
- $result = $this->exec("stat -f %z $arg");
- }
- return $result;
- }
- return null;
- }
-
- public function getFileSizeNative(string $filename): int|float {
- $result = filesize($filename);
- if ($result < 0) {
-
-
-
- return (float)sprintf('%u', $result);
- }
- return $result;
- }
-
- public function getFileMtime(string $fullPath): int {
- try {
- $result = filemtime($fullPath) ?: -1;
- } catch (\Exception $e) {
- $result = - 1;
- }
- if ($result < 0) {
- if (\OCP\Util::isFunctionEnabled('exec')) {
- $os = strtolower(php_uname('s'));
- if (str_contains($os, 'linux')) {
- return (int)($this->exec('stat -c %Y ' . escapeshellarg($fullPath)) ?? -1);
- }
- }
- }
- return $result;
- }
- protected function exec(string $cmd): null|int|float {
- $result = trim(exec($cmd));
- return ctype_digit($result) ? 0 + $result : null;
- }
- }
|