Skip to content

Commit 0f9d58d

Browse files
solracsfkesselb
authored andcommitted
Fix timestamp detection on external FTP
Context: #31510 Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 5b64b81 commit 0f9d58d

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

  • apps/files_external/lib/Lib/Storage

apps/files_external/lib/Lib/Storage/FTP.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
namespace OCA\Files_External\Lib\Storage;
2323

24+
use DateTimeImmutable;
2425
use Icewind\Streams\CallbackWrapper;
2526
use Icewind\Streams\CountWrapper;
2627
use Icewind\Streams\IteratorDirectory;
@@ -50,11 +51,7 @@ public function __construct($params) {
5051
$this->username = $params['user'];
5152
$this->password = $params['password'];
5253
if (isset($params['secure'])) {
53-
if (is_string($params['secure'])) {
54-
$this->secure = ($params['secure'] === 'true');
55-
} else {
56-
$this->secure = (bool)$params['secure'];
57-
}
54+
$this->secure = is_string($params['secure']) ? ($params['secure'] === 'true') : (bool) $params['secure'];
5855
} else {
5956
$this->secure = false;
6057
}
@@ -123,7 +120,7 @@ public function filemtime($path) {
123120
return $item['type'] === 'cdir';
124121
}));
125122
if ($currentDir) {
126-
$time = \DateTime::createFromFormat('YmdHis', $currentDir['modify'] ?? '');
123+
$time = $this->parseTimeVal($currentDir['modify'] ?? '');
127124
if ($time === false) {
128125
throw new \Exception("Invalid date format for directory: $currentDir");
129126
}
@@ -355,10 +352,12 @@ public function getDirectoryContent($directory): \Traversable {
355352

356353
$data = [];
357354
$data['mimetype'] = $isDir ? FileInfo::MIMETYPE_FOLDER : $mimeTypeDetector->detectPath($name);
358-
$data['mtime'] = \DateTime::createFromFormat('YmdGis', $file['modify'])->getTimestamp();
359-
if ($data['mtime'] === false) {
355+
$time = $this->parseTimeVal($file['modify'] ?? '');
356+
if ($time === false) {
360357
$data['mtime'] = time();
361-
}
358+
} else {
359+
$data['mtime'] = $time->getTimestamp();
360+
}
362361
if ($isDir) {
363362
$data['size'] = -1; //unknown
364363
} elseif (isset($file['size'])) {
@@ -374,4 +373,23 @@ public function getDirectoryContent($directory): \Traversable {
374373
yield $data;
375374
}
376375
}
376+
377+
/**
378+
* @param string $timeVal
379+
* @return DateTimeImmutable|false
380+
*/
381+
private function parseTimeVal(string $timeVal) {
382+
if ($timeVal === '') {
383+
return false;
384+
}
385+
386+
// https://www.rfc-editor.org/rfc/rfc3659#section-2.3
387+
if (strlen($timeVal) === 14) {
388+
$format = 'YmdGis';
389+
} else {
390+
$format = 'YmdGis.u';
391+
}
392+
393+
return DateTimeImmutable::createFromFormat($format, $timeVal);
394+
}
377395
}

0 commit comments

Comments
 (0)