summaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/lib
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2018-09-19 19:00:58 +0200
committerRobin Appelman <robin@icewind.nl>2018-10-17 14:56:48 +0200
commitd38163e89574e9d2a12be0cdf8f72e1e9bb4b75f (patch)
treebc07f70d769ed6a1e654b236b51c8499a1bc787c /apps/files_trashbin/lib
parent4adac445dc57d1ccc7f26e21018e1e731e5b1654 (diff)
downloadnextcloud-server-d38163e89574e9d2a12be0cdf8f72e1e9bb4b75f.tar.gz
nextcloud-server-d38163e89574e9d2a12be0cdf8f72e1e9bb4b75f.zip
fix trashbin previews for modular api
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps/files_trashbin/lib')
-rw-r--r--apps/files_trashbin/lib/Controller/PreviewController.php65
-rw-r--r--apps/files_trashbin/lib/Trash/ITrashBackend.php12
-rw-r--r--apps/files_trashbin/lib/Trash/LegacyTrashBackend.php25
-rw-r--r--apps/files_trashbin/lib/Trash/TrashManager.php16
4 files changed, 78 insertions, 40 deletions
diff --git a/apps/files_trashbin/lib/Controller/PreviewController.php b/apps/files_trashbin/lib/Controller/PreviewController.php
index 8a1b31703bb..59c4e508154 100644
--- a/apps/files_trashbin/lib/Controller/PreviewController.php
+++ b/apps/files_trashbin/lib/Controller/PreviewController.php
@@ -22,27 +22,34 @@ declare(strict_types=1);
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
+
namespace OCA\Files_Trashbin\Controller;
+use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\File;
+use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IPreview;
use OCP\IRequest;
+use OCP\IUser;
+use OCP\IUserSession;
class PreviewController extends Controller {
-
/** @var IRootFolder */
private $rootFolder;
- /** @var string */
- private $userId;
+ /** @var ITrashManager */
+ private $trashManager;
+
+ /** @var IUserSession */
+ private $userSession;
/** @var IMimeTypeDetector */
private $mimeTypeDetector;
@@ -53,17 +60,21 @@ class PreviewController extends Controller {
/** @var ITimeFactory */
private $time;
- public function __construct(string $appName,
- IRequest $request,
- IRootFolder $rootFolder,
- string $userId,
- IMimeTypeDetector $mimeTypeDetector,
- IPreview $previewManager,
- ITimeFactory $time) {
+ public function __construct(
+ string $appName,
+ IRequest $request,
+ IRootFolder $rootFolder,
+ ITrashManager $trashManager,
+ IUserSession $userSession,
+ IMimeTypeDetector $mimeTypeDetector,
+ IPreview $previewManager,
+ ITimeFactory $time
+ ) {
parent::__construct($appName, $request);
+ $this->trashManager = $trashManager;
$this->rootFolder = $rootFolder;
- $this->userId = $userId;
+ $this->userSession = $userSession;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->previewManager = $previewManager;
$this->time = $time;
@@ -86,39 +97,25 @@ class PreviewController extends Controller {
}
try {
- $userFolder = $this->rootFolder->getUserFolder($this->userId);
- /** @var Folder $trash */
- $trash = $userFolder->getParent()->get('files_trashbin/files');
- $trashFiles = $trash->getById($fileId);
-
- if (empty($trashFiles)) {
- throw new NotFoundException();
- }
-
- $trashFile = array_pop($trashFiles);
-
- if ($trashFile instanceof Folder) {
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
+ $file = $this->trashManager->getTrashNodeById($this->userSession->getUser(), $fileId);
+ if ($file === null || $file instanceof Folder) {
+ return new DataResponse([], Http::STATUS_NOT_FOUND);
}
+ $pathParts = pathinfo($file->getName());
+ $extension = $pathParts['extension'];
+ $fileName = $pathParts['filename'];
/*
* Files in the root of the trashbin are timetamped.
* So we have to strip that in order to properly detect the mimetype of the file.
*/
- if ($trashFile->getParent()->getPath() === $trash->getPath()) {
- /** @var File $trashFile */
- $fileName = $trashFile->getName();
- $i = strrpos($fileName, '.');
- if ($i !== false) {
- $fileName = substr($fileName, 0, $i);
- }
-
+ if (preg_match('/d\d+/', $extension)) {
$mimeType = $this->mimeTypeDetector->detectPath($fileName);
} else {
- $mimeType = $this->mimeTypeDetector->detectPath($trashFile->getName());
+ $mimeType = $this->mimeTypeDetector->detectPath($file->getName());
}
- $f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType);
+ $f = $this->previewManager->getPreview($file, $x, $y, true, IPreview::MODE_FILL, $mimeType);
$response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
// Cache previews for 24H
diff --git a/apps/files_trashbin/lib/Trash/ITrashBackend.php b/apps/files_trashbin/lib/Trash/ITrashBackend.php
index 35b16a00c22..90d3a29613d 100644
--- a/apps/files_trashbin/lib/Trash/ITrashBackend.php
+++ b/apps/files_trashbin/lib/Trash/ITrashBackend.php
@@ -22,6 +22,8 @@
namespace OCA\Files_Trashbin\Trash;
use OCP\Files\FileInfo;
+use OCP\Files\Node;
+use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
@@ -61,7 +63,6 @@ interface ITrashBackend {
*
* @param IUser $user
* @param ITrashItem $item
- * @return
* @since 15.0.0
*/
public function removeItem(IUser $user, ITrashItem $item);
@@ -74,5 +75,12 @@ interface ITrashBackend {
* @return boolean whether or not the file was moved to trash, if false then the file should be deleted normally
* @since 15.0.0
*/
- public function moveToTrash(IStorage $storage, string $internalPath);
+ public function moveToTrash(IStorage $storage, string $internalPath): bool;
+
+ /**
+ * @param IUser $user
+ * @param int $fileId
+ * @return Node|null
+ */
+ public function getTrashNodeById(IUser $user, int $fileId);
}
diff --git a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
index 210c84228f7..04e3b284775 100644
--- a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
+++ b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
@@ -27,6 +27,8 @@ use OCA\Files_Trashbin\Helper;
use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trashbin;
use OCP\Files\FileInfo;
+use OCP\Files\IRootFolder;
+use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
@@ -34,6 +36,13 @@ class LegacyTrashBackend implements ITrashBackend {
/** @var array */
private $deletedFiles = [];
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ public function __construct(IRootFolder $rootFolder) {
+ $this->rootFolder = $rootFolder;
+ }
+
/**
* @param array $items
* @param IUser $user
@@ -78,7 +87,7 @@ class LegacyTrashBackend implements ITrashBackend {
}
- public function moveToTrash(IStorage $storage, string $internalPath) {
+ public function moveToTrash(IStorage $storage, string $internalPath): bool {
if (!$storage instanceof Storage) {
return false;
}
@@ -99,4 +108,18 @@ class LegacyTrashBackend implements ITrashBackend {
return $result;
}
+
+ public function getTrashNodeById(IUser $user, int $fileId) {
+ try {
+ $userFolder = $this->rootFolder->getUserFolder($user->getUID());
+ $trash = $userFolder->getParent()->get('files_trashbin/files');
+ $trashFiles = $trash->getById($fileId);
+ if (!$trashFiles) {
+ return null;
+ }
+ return $trashFiles ? array_pop($trashFiles) : null;
+ } catch (NotFoundException $e) {
+ return null;
+ }
+ }
}
diff --git a/apps/files_trashbin/lib/Trash/TrashManager.php b/apps/files_trashbin/lib/Trash/TrashManager.php
index 3870271cc7e..cae4f2473ad 100644
--- a/apps/files_trashbin/lib/Trash/TrashManager.php
+++ b/apps/files_trashbin/lib/Trash/TrashManager.php
@@ -76,9 +76,9 @@ class TrashManager implements ITrashManager {
*/
public function getBackendForStorage(IStorage $storage): ITrashBackend {
$fullType = get_class($storage);
- $foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($fullType) {
+ $foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) {
if (
- is_subclass_of($fullType, $registeredType) &&
+ $storage->instanceOfStorage($registeredType) &&
($type === '' || is_subclass_of($registeredType, $type))
) {
return $registeredType;
@@ -93,7 +93,7 @@ class TrashManager implements ITrashManager {
}
}
- public function moveToTrash(IStorage $storage, string $internalPath) {
+ public function moveToTrash(IStorage $storage, string $internalPath): bool {
if ($this->trashPaused) {
return false;
}
@@ -105,6 +105,16 @@ class TrashManager implements ITrashManager {
}
}
+ public function getTrashNodeById(IUser $user, int $fileId) {
+ foreach ($this->backends as $backend) {
+ $item = $backend->getTrashNodeById($user, $fileId);
+ if ($item !== null) {
+ return $item;
+ }
+ }
+ return null;
+ }
+
public function pauseTrash() {
$this->trashPaused = true;
}