Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle file contained inside the uploads folder #32956

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions apps/dav/lib/BackgroundJob/UploadCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use Psr\Log\LoggerInterface;

class UploadCleanup extends TimedJob {
private IRootFolder $rootFolder;
private IJobList $jobList;
private LoggerInterface $logger;

public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobList $jobList) {
public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobList $jobList, LoggerInterface $logger) {
parent::__construct($time);
$this->rootFolder = $rootFolder;
$this->jobList = $jobList;
$this->logger = $logger;

// Run once a day
$this->setInterval(60 * 60 * 24);
Expand All @@ -61,19 +64,27 @@ protected function run($argument) {
$userRoot = $userFolder->getParent();
/** @var Folder $uploads */
$uploads = $userRoot->get('uploads');
/** @var Folder $uploadFolder */
$uploadFolder = $uploads->get($folder);
} catch (NotFoundException | NoUserException $e) {
$this->jobList->remove(self::class, $argument);
return;
}

/** @var File[] $files */
$files = $uploadFolder->getDirectoryListing();

// Remove if all files have an mtime of more than a day
$time = $this->time->getTime() - 60 * 60 * 24;

if (!($uploadFolder instanceof Folder)) {
$this->logger->error("Found a file inside the uploads folder. Uid: " . $uid . ' folder: ' . $folder);
if ($uploadFolder->getMTime() < $time) {
$uploadFolder->delete();
}
$this->jobList->remove(self::class, $argument);
return;
}

/** @var File[] $files */
$files = $uploadFolder->getDirectoryListing();

// The folder has to be more than a day old
$initial = $uploadFolder->getMTime() < $time;

Expand Down