2013-09-01 21:47:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
namespace OC\Files\Node;
|
|
|
|
|
2018-03-08 23:58:11 +03:00
|
|
|
use OCP\Files\GenericFileException;
|
2013-09-10 21:44:23 +04:00
|
|
|
use OCP\Files\NotPermittedException;
|
2019-11-28 16:17:15 +03:00
|
|
|
use OCP\Lock\LockedException;
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2013-09-10 21:44:23 +04:00
|
|
|
class File extends Node implements \OCP\Files\File {
|
2016-11-14 20:29:11 +03:00
|
|
|
/**
|
|
|
|
* Creates a Folder that represents a non-existing path
|
|
|
|
*
|
|
|
|
* @param string $path path
|
|
|
|
* @return string non-existing node class
|
|
|
|
*/
|
|
|
|
protected function createNonExistingNode($path) {
|
|
|
|
return new NonExistingFile($this->root, $this->view, $path);
|
|
|
|
}
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
2019-11-28 16:17:15 +03:00
|
|
|
* @throws NotPermittedException
|
|
|
|
* @throws LockedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function getContent() {
|
2014-11-25 18:28:41 +03:00
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage;
|
|
|
|
*/
|
|
|
|
return $this->view->file_get_contents($this->path);
|
|
|
|
} else {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-26 12:07:41 +03:00
|
|
|
* @param string|resource $data
|
2019-11-28 16:17:15 +03:00
|
|
|
* @throws NotPermittedException
|
2018-03-08 23:58:11 +03:00
|
|
|
* @throws \OCP\Files\GenericFileException
|
2019-11-28 16:17:15 +03:00
|
|
|
* @throws LockedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function putContent($data) {
|
2014-11-25 18:28:41 +03:00
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->sendHooks(array('preWrite'));
|
2018-03-08 23:58:11 +03:00
|
|
|
if ($this->view->file_put_contents($this->path, $data) === false) {
|
|
|
|
throw new GenericFileException('file_put_contents failed');
|
|
|
|
}
|
2014-12-04 15:27:08 +03:00
|
|
|
$this->fileInfo = null;
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->sendHooks(array('postWrite'));
|
|
|
|
} else {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $mode
|
|
|
|
* @return resource
|
2019-11-28 16:17:15 +03:00
|
|
|
* @throws NotPermittedException
|
|
|
|
* @throws LockedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function fopen($mode) {
|
|
|
|
$preHooks = array();
|
|
|
|
$postHooks = array();
|
2014-11-25 18:28:41 +03:00
|
|
|
$requiredPermissions = \OCP\Constants::PERMISSION_READ;
|
2013-09-01 21:47:48 +04:00
|
|
|
switch ($mode) {
|
|
|
|
case 'r+':
|
|
|
|
case 'rb+':
|
|
|
|
case 'w+':
|
|
|
|
case 'wb+':
|
|
|
|
case 'x+':
|
|
|
|
case 'xb+':
|
|
|
|
case 'a+':
|
|
|
|
case 'ab+':
|
|
|
|
case 'w':
|
|
|
|
case 'wb':
|
|
|
|
case 'x':
|
|
|
|
case 'xb':
|
|
|
|
case 'a':
|
|
|
|
case 'ab':
|
|
|
|
$preHooks[] = 'preWrite';
|
|
|
|
$postHooks[] = 'postWrite';
|
2014-11-25 18:28:41 +03:00
|
|
|
$requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
|
2013-09-01 21:47:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->checkPermissions($requiredPermissions)) {
|
|
|
|
$this->sendHooks($preHooks);
|
|
|
|
$result = $this->view->fopen($this->path, $mode);
|
|
|
|
$this->sendHooks($postHooks);
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:17:15 +03:00
|
|
|
/**
|
|
|
|
* @throws NotPermittedException
|
|
|
|
* @throws \OCP\Files\InvalidPathException
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
2013-09-01 21:47:48 +04:00
|
|
|
public function delete() {
|
2014-11-25 18:28:41 +03:00
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->sendHooks(array('preDelete'));
|
2015-12-01 15:22:58 +03:00
|
|
|
$fileInfo = $this->getFileInfo();
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->view->unlink($this->path);
|
2015-12-01 15:22:58 +03:00
|
|
|
$nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
|
2019-09-03 13:30:10 +03:00
|
|
|
$this->sendHooks(['postDelete'], [$nonExisting]);
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->exists = false;
|
2014-12-04 15:27:08 +03:00
|
|
|
$this->fileInfo = null;
|
2013-09-01 21:47:48 +04:00
|
|
|
} else {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param bool $raw
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function hash($type, $raw = false) {
|
|
|
|
return $this->view->hash($type, $this->path, $raw);
|
|
|
|
}
|
2016-01-29 23:50:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getChecksum() {
|
2016-02-08 14:23:48 +03:00
|
|
|
return $this->getFileInfo()->getChecksum();
|
2016-01-29 23:50:48 +03:00
|
|
|
}
|
2018-10-29 17:23:33 +03:00
|
|
|
|
|
|
|
public function getExtension(): string {
|
|
|
|
return $this->getFileInfo()->getExtension();
|
|
|
|
}
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|