Skip to content

Commit 7c3c545

Browse files
committed
fix: Correctly filter out current version from expiration
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 9e98b68 commit 7c3c545

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/Versions/ExpireManager.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function __construct(
3939
/**
4040
* Get list of files we want to expire
4141
*
42-
* @param IVersion[] $versions
43-
* @return IVersion[]
42+
* @param GroupVersion[] $versions
43+
* @return GroupVersion[]
4444
*/
4545
protected function getAutoExpireList(int $time, array $versions): array {
4646
if (!$versions) {
@@ -49,14 +49,20 @@ protected function getAutoExpireList(int $time, array $versions): array {
4949

5050
$toDelete = []; // versions we want to delete
5151

52-
// ensure the versions are sorted newest first
53-
usort($versions, fn (IVersion $a, IVersion $b): int => $b->getTimestamp() <=> $a->getTimestamp());
52+
// Ensure the versions are sorted current first, then newest first
53+
usort($versions, function (GroupVersion $a, GroupVersion $b): int {
54+
if ($a->isCurrentVersion()) {
55+
return 1;
56+
} elseif ($b->isCurrentVersion()) {
57+
return -1;
58+
}
59+
return $b->getTimestamp() <=> $a->getTimestamp();
60+
});
5461

5562
$interval = 1;
5663
$step = self::MAX_VERSIONS_PER_INTERVAL[$interval]['step'];
5764
$nextInterval = $time - self::MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'];
5865

59-
/** @var IVersion $firstVersion */
6066
$firstVersion = array_shift($versions);
6167
$prevTimestamp = $firstVersion->getTimestamp();
6268
$nextVersion = $firstVersion->getTimestamp() - $step;
@@ -67,7 +73,7 @@ protected function getAutoExpireList(int $time, array $versions): array {
6773
if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
6874
if ($version->getTimestamp() > $nextVersion) {
6975
// Do not expire versions with a label.
70-
if (!($version instanceof IMetadataVersion) || $version->getMetadataValue('label') === null || $version->getMetadataValue('label') === '') {
76+
if ((!($version instanceof IMetadataVersion) || $version->getMetadataValue('label') === null || $version->getMetadataValue('label') === '') && !$version->isCurrentVersion()) {
7177
//distance between two version too small, mark to delete
7278
$toDelete[] = $version;
7379
}
@@ -110,9 +116,9 @@ public function getExpiredVersion(array $versions, int $time, bool $quotaExceede
110116
$versionsLeft = array_udiff($versions, $autoExpire, fn (IVersion $a, IVersion $b): int => ($a->getRevisionId() <=> $b->getRevisionId())
111117
* ($a->getSourceFile()->getId() <=> $b->getSourceFile()->getId()));
112118

113-
$expired = array_filter($versionsLeft, function (IVersion $version) use ($quotaExceeded): bool {
119+
$expired = array_filter($versionsLeft, function (GroupVersion $version) use ($quotaExceeded): bool {
114120
// Do not expire current version.
115-
if ($version->getTimestamp() === $version->getSourceFile()->getMtime()) {
121+
if ($version->isCurrentVersion()) {
116122
return false;
117123
}
118124

0 commit comments

Comments
 (0)