Skip to content

Commit

Permalink
Fix timestamp detection on external FTP
Browse files Browse the repository at this point in the history
Context: #31510
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
solracsf authored and kesselb committed Dec 21, 2022
1 parent cb086a0 commit da4d799
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions apps/files_external/lib/Lib/Storage/FTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ public function __construct($params) {
$this->username = $params['user'];
$this->password = $params['password'];
if (isset($params['secure'])) {
if (is_string($params['secure'])) {
$this->secure = ($params['secure'] === 'true');
} else {
$this->secure = (bool)$params['secure'];
}
$this->secure = is_string($params['secure']) ? ($params['secure'] === 'true') : (bool) $params['secure'];
} else {
$this->secure = false;
}
Expand Down Expand Up @@ -123,7 +119,7 @@ public function filemtime($path) {
return $item['type'] === 'cdir';
}));
if ($currentDir) {
$time = \DateTime::createFromFormat('YmdHis', $currentDir['modify'] ?? '');
$time = $this->parseTimeVal($currentDir['modify'] ?? '');
if ($time === false) {
throw new \Exception("Invalid date format for directory: $currentDir");
}
Expand Down Expand Up @@ -355,10 +351,12 @@ public function getDirectoryContent($directory): \Traversable {

$data = [];
$data['mimetype'] = $isDir ? FileInfo::MIMETYPE_FOLDER : $mimeTypeDetector->detectPath($name);
$data['mtime'] = \DateTime::createFromFormat('YmdGis', $file['modify'])->getTimestamp();
if ($data['mtime'] === false) {
$modifyDate = $this->parseTimeVal($file['modify'] ?? '');
if ($modifyDate === false) {
$data['mtime'] = time();
}
} else {
$data['mtime'] = $modifyDate->getTimestamp();
}
if ($isDir) {
$data['size'] = -1; //unknown
} elseif (isset($file['size'])) {
Expand All @@ -374,4 +372,19 @@ public function getDirectoryContent($directory): \Traversable {
yield $data;
}
}

private function parseTimeVal(string $timeVal): \DateTimeImmutable|false {
if ($timeVal === '') {
return false;
}

// https://www.rfc-editor.org/rfc/rfc3659#section-2.3
if (strlen($timeVal) === 14) {
$format = 'YmdGis';
} else {
$format = 'YmdGis.u';
}

return \DateTimeImmutable::createFromFormat($format, $timeVal);
}
}

0 comments on commit da4d799

Please sign in to comment.