2013-09-01 21:47:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Node;
|
|
|
|
|
2013-09-10 21:44:23 +04:00
|
|
|
use OCP\Files\NotPermittedException;
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2013-09-10 21:44:23 +04:00
|
|
|
class File extends Node implements \OCP\Files\File {
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
|
|
|
* @return string
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $data
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
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'));
|
|
|
|
$this->view->file_put_contents($this->path, $data);
|
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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'));
|
|
|
|
$this->view->unlink($this->path);
|
|
|
|
$nonExisting = new NonExistingFile($this->root, $this->view, $this->path);
|
|
|
|
$this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
|
|
|
|
$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 $targetPath
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
* @return \OC\Files\Node\Node
|
|
|
|
*/
|
|
|
|
public function copy($targetPath) {
|
|
|
|
$targetPath = $this->normalizePath($targetPath);
|
|
|
|
$parent = $this->root->get(dirname($targetPath));
|
|
|
|
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
|
|
|
|
$nonExisting = new NonExistingFile($this->root, $this->view, $targetPath);
|
|
|
|
$this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
|
|
|
|
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
|
|
|
|
$this->view->copy($this->path, $targetPath);
|
|
|
|
$targetNode = $this->root->get($targetPath);
|
|
|
|
$this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
|
|
|
|
$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
|
|
|
|
return $targetNode;
|
|
|
|
} else {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $targetPath
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
* @return \OC\Files\Node\Node
|
|
|
|
*/
|
|
|
|
public function move($targetPath) {
|
|
|
|
$targetPath = $this->normalizePath($targetPath);
|
|
|
|
$parent = $this->root->get(dirname($targetPath));
|
|
|
|
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
|
|
|
|
$nonExisting = new NonExistingFile($this->root, $this->view, $targetPath);
|
|
|
|
$this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
|
|
|
|
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
|
|
|
|
$this->view->rename($this->path, $targetPath);
|
|
|
|
$targetNode = $this->root->get($targetPath);
|
|
|
|
$this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
|
|
|
|
$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
|
|
|
|
$this->path = $targetPath;
|
2014-12-04 15:27:08 +03:00
|
|
|
$this->fileInfo = null;
|
2013-09-01 21:47:48 +04:00
|
|
|
return $targetNode;
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|