Skip to content
Draft
Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions apps/files_trashbin/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,67 @@
return $result;
}

public static function getTrashFile(string $dir, string $user, string $name): ?FileInfo {
$timestamp = null;

$view = new View('/' . $user . '/files_trashbin/files');

Check failure on line 98 in apps/files_trashbin/lib/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InternalMethod

apps/files_trashbin/lib/Helper.php:98:11: InternalMethod: Constructor OC\Files\View::__construct is internal to OC but called from OCA\Files_Trashbin\Helper::getTrashFile (see https://psalm.dev/175)

Check failure on line 98 in apps/files_trashbin/lib/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InternalClass

apps/files_trashbin/lib/Helper.php:98:11: InternalClass: OC\Files\View is internal to OC but called from OCA\Files_Trashbin\Helper (see https://psalm.dev/174)

if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {

Check failure on line 100 in apps/files_trashbin/lib/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InternalMethod

apps/files_trashbin/lib/Helper.php:100:42: InternalMethod: The method OC\Files\View::is_dir is internal to OC but called from OCA\Files_Trashbin\Helper::getTrashFile (see https://psalm.dev/175)
throw new \Exception('Directory does not exists');
}

$mount = $view->getMount($dir);

Check failure on line 104 in apps/files_trashbin/lib/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InternalMethod

apps/files_trashbin/lib/Helper.php:104:19: InternalMethod: The method OC\Files\View::getMount is internal to OC but called from OCA\Files_Trashbin\Helper::getTrashFile (see https://psalm.dev/175)
$storage = $mount->getStorage();
$absoluteDir = $view->getAbsolutePath($dir);

Check failure on line 106 in apps/files_trashbin/lib/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InternalMethod

apps/files_trashbin/lib/Helper.php:106:25: InternalMethod: The method OC\Files\View::getAbsolutePath is internal to OC but called from OCA\Files_Trashbin\Helper::getTrashFile (see https://psalm.dev/175)
$internalPath = $mount->getInternalPath($absoluteDir);

$extraData = Trashbin::getExtraData($user);
$entry = $storage->getCache()->get($mount->getInternalPath($view->getAbsolutePath($dir . '/' . $name)));

Check failure on line 110 in apps/files_trashbin/lib/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InternalMethod

apps/files_trashbin/lib/Helper.php:110:69: InternalMethod: The method OC\Files\View::getAbsolutePath is internal to OC but called from OCA\Files_Trashbin\Helper::getTrashFile (see https://psalm.dev/175)
if ($entry === null) {
return null;
}
$entryName = $entry->getName();
$name = $entryName;
if ($dir === '' || $dir === '/') {
$pathparts = pathinfo($entryName);
$timestamp = substr($pathparts['extension'], 1);
$name = $pathparts['filename'];
} elseif ($timestamp === null) {
// for subfolders we need to calculate the timestamp only once
$parts = explode('/', ltrim($dir, '/'));
$timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
}
$originalPath = '';
$originalName = substr($entryName, 0, -strlen($timestamp) - 2);
if (isset($extraData[$originalName][$timestamp]['location'])) {
$originalPath = $extraData[$originalName][$timestamp]['location'];
if (substr($originalPath, -1) === '/') {
$originalPath = substr($originalPath, 0, -1);
}
}
$type = $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file';
$i = [
'name' => $name,
'mtime' => $timestamp,
'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : Server::get(IMimeTypeDetector::class)->detectPath($name),
'type' => $type,
'directory' => ($dir === '/') ? '' : $dir,
'size' => $entry->getSize(),
'etag' => '',
'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
'fileid' => $entry->getId(),
];
if ($originalPath) {
if ($originalPath !== '.') {
$i['extraData'] = $originalPath . '/' . $originalName;
} else {
$i['extraData'] = $originalName;
}
}
$i['deletedBy'] = $extraData[$originalName][$timestamp]['deletedBy'] ?? null;
return new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
}

/**
* Format file infos for JSON
*
Expand Down
9 changes: 9 additions & 0 deletions apps/files_trashbin/lib/Trash/ITrashManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function registerBackend(string $storageType, ITrashBackend $backend);
#[\Override]
public function listTrashRoot(IUser $user): array;

/**
* Get a specific item in the root of the trashbin
*
* @param IUser $user
* @return ?ITrashItem
* @since 35.0.0
*/
public function getTrashRootItem(IUser $user, string $name): ?ITrashItem;

/**
* Temporally prevent files from being moved to the trash
*
Expand Down
5 changes: 5 additions & 0 deletions apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function listTrashRoot(IUser $user): array {
return $this->mapTrashItems($entries, $user);
}

public function getTrashRootItem(IUser $user, string $name): ?ITrashItem {
$entry = Helper::getTrashFile('/', $user->getUID(), $name);
return $this->mapTrashItems([$entry], $user)[0];
}

#[\Override]
public function listTrashFolder(ITrashItem $folder): array {
$user = $folder->getUser();
Expand Down
19 changes: 19 additions & 0 deletions apps/files_trashbin/lib/Trash/TrashManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public function listTrashRoot(IUser $user): array {
return $items;
}

#[\Override]
public function getTrashRootItem(IUser $user, string $name): ?ITrashItem {
foreach ($this->getBackends() as $backend) {
if (method_exists($backend, 'getTrashRootItem')) {
$item = $backend->getTrashRootItem($user, $name);
if ($item !== null) {
return $item;
}
}
$items = $backend->listTrashRoot($user);
foreach ($items as $item) {
if ($item->getName() === $name) {
return $item;
}
}
}
return null;
}

private function getBackendForItem(ITrashItem $item) {
return $item->getTrashBackend();
}
Expand Down
Loading