diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-04-03 21:06:41 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-04-03 21:06:41 +0300 |
commit | a7596caa7cbe2e260cfefa31ace59d0def06ed76 (patch) | |
tree | ac86171551d66f4d9d296efc1e7dd776dcdcfdf6 /controller | |
parent | 74c710a2b4ba26d53f308fb3b96305cab831a936 (diff) |
Move the controller over to the node API
The app should use the official node api. As a nice side effect this
fixes https://github.com/nextcloud/server/issues/4185
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'controller')
-rw-r--r-- | controller/filehandlingcontroller.php | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/controller/filehandlingcontroller.php b/controller/filehandlingcontroller.php index b39bff2..e5ae4a2 100644 --- a/controller/filehandlingcontroller.php +++ b/controller/filehandlingcontroller.php @@ -23,13 +23,13 @@ namespace OCA\Files_Texteditor\Controller; -use OC\Files\View; use OC\HintException; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\Files\File; +use OCP\Files\Folder; use OCP\Files\ForbiddenException; -use OCP\Files\StorageNotAvailableException; use OCP\IL10N; use OCP\ILogger; use OCP\IRequest; @@ -40,30 +40,30 @@ class FileHandlingController extends Controller{ /** @var IL10N */ private $l; - /** @var View */ - private $view; - /** @var ILogger */ private $logger; + /** @var Folder */ + private $userFolder; + /** * @NoAdminRequired * * @param string $AppName * @param IRequest $request * @param IL10N $l10n - * @param View $view * @param ILogger $logger + * @param Folder $userFolder */ public function __construct($AppName, IRequest $request, IL10N $l10n, - View $view, - ILogger $logger) { + ILogger $logger, + Folder $userFolder) { parent::__construct($AppName, $request); $this->l = $l10n; - $this->view = $view; $this->logger = $logger; + $this->userFolder = $userFolder; } /** @@ -79,22 +79,30 @@ class FileHandlingController extends Controller{ try { if (!empty($filename)) { $path = $dir . '/' . $filename; + + /** @var File $file */ + $file = $this->userFolder->get($path); + + if ($file instanceof Folder) { + return new DataResponse(['message' => $this->l->t('You can not open a folder')], Http::STATUS_BAD_REQUEST); + } + // default of 4MB $maxSize = 4194304; - if ($this->view->filesize($path) > $maxSize) { + if ($file->getSize() > $maxSize) { return new DataResponse(['message' => (string)$this->l->t('This file is too big to be opened. Please download the file instead.')], Http::STATUS_BAD_REQUEST); } - $fileContents = $this->view->file_get_contents($path); + $fileContents = $file->getContent(); if ($fileContents !== false) { - $writable = $this->view->isUpdatable($path); - $mime = $this->view->getMimeType($path); - $mTime = $this->view->filemtime($path); - $encoding = mb_detect_encoding($fileContents . "a", "UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII", true); - if ($encoding == "") { + $writable = $file->isUpdateable(); + $mime = $file->getMimeType(); + $mTime = $file->getMTime(); + $encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true); + if ($encoding === '') { // set default encoding if it couldn't be detected $encoding = 'ISO-8859-15'; } - $fileContents = iconv($encoding, "UTF-8", $fileContents); + $fileContents = iconv($encoding, 'UTF-8', $fileContents); return new DataResponse( [ 'filecontents' => $fileContents, @@ -137,9 +145,17 @@ class FileHandlingController extends Controller{ */ public function save($path, $filecontents, $mtime) { try { - if($path !== '' && (is_integer($mtime) && $mtime > 0)) { + if($path !== '' && (is_int($mtime) && $mtime > 0)) { + + /** @var File $file */ + $file = $this->userFolder->get($path); + + if ($file instanceof Folder) { + return new DataResponse(['message' => $this->l->t('You can not write to a folder')], Http::STATUS_BAD_REQUEST); + } + // Get file mtime - $filemtime = $this->view->filemtime($path); + $filemtime = $file->getMTime(); if($mtime !== $filemtime) { // Then the file has changed since opening $this->logger->error('File: ' . $path . ' modified since opening.', @@ -149,10 +165,10 @@ class FileHandlingController extends Controller{ Http::STATUS_BAD_REQUEST); } else { // File same as when opened, save file - if($this->view->isUpdatable($path)) { - $filecontents = iconv(mb_detect_encoding($filecontents), "UTF-8", $filecontents); + if($file->isUpdateable()) { + $filecontents = iconv(mb_detect_encoding($filecontents), 'UTF-8', $filecontents); try { - $this->view->file_put_contents($path, $filecontents); + $file->putContent($filecontents); } catch (LockedException $e) { $message = (string) $this->l->t('The file is locked.'); return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); @@ -162,8 +178,8 @@ class FileHandlingController extends Controller{ // Clear statcache clearstatcache(); // Get new mtime - $newmtime = $this->view->filemtime($path); - $newsize = $this->view->filesize($path); + $newmtime = $file->getMTime(); + $newsize = $file->getSize(); return new DataResponse(['mtime' => $newmtime, 'size' => $newsize], Http::STATUS_OK); } else { // Not writeable! |