Skip to content

Commit b191c9d

Browse files
Roman HaninRoman Hanin
authored andcommitted
B2B-1610: Upgrade to Flysystem 2.0
- fixed caching and metadata retrieval
1 parent 4520417 commit b191c9d

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -445,28 +445,22 @@ public function isDirectory($path): bool
445445

446446
$path = $this->normalizeRelativePath($path, true);
447447

448-
try {
449-
return $this->isTypeDirectory($path);
450-
} catch (UnableToRetrieveMetadata $e) {
451-
try {
452-
return iterator_count($this->adapter->listContents($path, false)) > 0;
453-
} catch (\Throwable $e) {
454-
// catch closed iterator
455-
return false;
456-
}
457-
}
448+
return $this->isTypeDirectory($path);
458449
}
459450

460451
/**
461452
* Check is given path a directory in metadata.
462453
*
463454
* @param string $path
464455
* @return bool
465-
* @throws UnableToRetrieveMetadata
466456
*/
467457
private function isTypeDirectory($path)
468458
{
469-
$meta = $this->metadataProvider->getMetadata($path);
459+
try {
460+
$meta = $this->metadataProvider->getMetadata($path);
461+
} catch (UnableToRetrieveMetadata $e) {
462+
return false;
463+
}
470464
if (isset($meta['type']) && $meta['type'] === self::TYPE_DIR) {
471465
return true;
472466
}

app/code/Magento/RemoteStorage/Driver/Adapter/Cache/Generic.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,18 @@ public function getMetadata(string $path): ?array
188188
{
189189
if (isset($this->cacheData[$path]['type'])) {
190190
return $this->cacheData[$path];
191+
} else {
192+
$meta = $this->cacheAdapter->load($this->prefix . $path);
193+
if (!$meta) {
194+
return null;
195+
}
196+
$meta = $this->serializer->unserialize($meta);
197+
if (!$meta[$path]) {
198+
return null;
199+
}
200+
$this->cacheData[$path] = $meta[$path];
201+
return $this->cacheData[$path];
191202
}
192-
193-
return null;
194203
}
195204

196205
/**

app/code/Magento/RemoteStorage/Driver/Adapter/MetadataProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public function __construct(
4141
$this->cache = $cache;
4242
}
4343

44+
/**
45+
* Check is the given path an existing directory.
46+
*
47+
* @param string $path
48+
* @return bool
49+
*/
50+
private function isDirectory($path): bool
51+
{
52+
try {
53+
return iterator_count($this->adapter->listContents($path, false)) > 0;
54+
} catch (\Throwable $e) {
55+
// catch closed iterator
56+
return false;
57+
}
58+
}
59+
4460
/**
4561
* @inheritdoc
4662
*/
@@ -54,6 +70,27 @@ public function getMetadata(string $path): array
5470
}
5571
try {
5672
$meta = $this->adapter->lastModified($path);
73+
} catch (UnableToRetrieveMetadata $e) {
74+
if ($this->isDirectory($path)) {
75+
$data = [
76+
'path' => $path,
77+
'type' => 'dir',
78+
'size' => null,
79+
'timestamp' => null,
80+
'visibility' => null,
81+
'mimetype' => null,
82+
'dirname' => dirname($path),
83+
'basename' => basename($path),
84+
];
85+
$this->cache->updateMetadata($path, $data, true);
86+
return $data;
87+
} else {
88+
throw new UnableToRetrieveMetadata(
89+
"Unable to retrieve metadata for file at location: {$path}. {$e->getMessage()}",
90+
0,
91+
$e
92+
);
93+
}
5794
} catch (\InvalidArgumentException | FilesystemException $e) {
5895
throw new UnableToRetrieveMetadata(
5996
"Unable to retrieve metadata for file at location: {$path}. {$e->getMessage()}",

0 commit comments

Comments
 (0)