fix trashbin previews for modular api

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2018-09-19 19:00:58 +02:00
parent 4adac445dc
commit d38163e895
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
4 changed files with 78 additions and 40 deletions

View File

@ -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,
public function __construct(
string $appName,
IRequest $request,
IRootFolder $rootFolder,
string $userId,
ITrashManager $trashManager,
IUserSession $userSession,
IMimeTypeDetector $mimeTypeDetector,
IPreview $previewManager,
ITimeFactory $time) {
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

View File

@ -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);
}

View File

@ -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;
}
}
}

View File

@ -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;
}