Skip to content

Commit

Permalink
Merge pull request #19493 from nextcloud/simplefile-new-lazy
Browse files Browse the repository at this point in the history
Create SimpleFile only when writing the content
  • Loading branch information
rullzer authored Feb 28, 2020
2 parents 0f08acf + 245125d commit de34786
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 81 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@
'OC\\Files\\Search\\SearchComparison' => $baseDir . '/lib/private/Files/Search/SearchComparison.php',
'OC\\Files\\Search\\SearchOrder' => $baseDir . '/lib/private/Files/Search/SearchOrder.php',
'OC\\Files\\Search\\SearchQuery' => $baseDir . '/lib/private/Files/Search/SearchQuery.php',
'OC\\Files\\SimpleFS\\NewSimpleFile' => $baseDir . '/lib/private/Files/SimpleFS/NewSimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFile' => $baseDir . '/lib/private/Files/SimpleFS/SimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFolder' => $baseDir . '/lib/private/Files/SimpleFS/SimpleFolder.php',
'OC\\Files\\Storage\\Common' => $baseDir . '/lib/private/Files/Storage/Common.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Files\\Search\\SearchComparison' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchComparison.php',
'OC\\Files\\Search\\SearchOrder' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchOrder.php',
'OC\\Files\\Search\\SearchQuery' => __DIR__ . '/../../..' . '/lib/private/Files/Search/SearchQuery.php',
'OC\\Files\\SimpleFS\\NewSimpleFile' => __DIR__ . '/../../..' . '/lib/private/Files/SimpleFS/NewSimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFile' => __DIR__ . '/../../..' . '/lib/private/Files/SimpleFS/SimpleFile.php',
'OC\\Files\\SimpleFS\\SimpleFolder' => __DIR__ . '/../../..' . '/lib/private/Files/SimpleFS/SimpleFolder.php',
'OC\\Files\\Storage\\Common' => __DIR__ . '/../../..' . '/lib/private/Files/Storage/Common.php',
Expand Down
10 changes: 8 additions & 2 deletions lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,21 @@ public function newFolder($path) {

/**
* @param string $path
* @param string | resource | null $content
* @return \OC\Files\Node\File
* @throws \OCP\Files\NotPermittedException
*/
public function newFile($path) {
public function newFile($path, $content = null) {
if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
$fullPath = $this->getFullPath($path);
$nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
$this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
if (!$this->view->touch($fullPath)) {
if ($content !== null) {
$result = $this->view->file_put_contents($fullPath, $content);
} else {
$result = $this->view->touch($fullPath);
}
if (!$result) {
throw new NotPermittedException('Could not create path');
}
$node = new File($this->root, $this->view, $fullPath);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/LazyRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function newFolder($path) {
/**
* @inheritDoc
*/
public function newFile($path) {
public function newFile($path, $content = null) {
return $this->__call(__FUNCTION__, func_get_args());
}

Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/NonExistingFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function newFolder($path) {
throw new NotFoundException();
}

public function newFile($path) {
public function newFile($path, $content = null) {
throw new NotFoundException();
}

Expand Down
221 changes: 221 additions & 0 deletions lib/private/Files/SimpleFS/NewSimpleFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Files\SimpleFS;

use Icewind\Streams\CallbackWrapper;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;

class NewSimpleFile implements ISimpleFile {
private $parentFolder;
private $name;
/** @var File|null */
private $file = null;

/**
* File constructor.
*
* @param File $file
*/
public function __construct(Folder $parentFolder, string $name) {
$this->parentFolder = $parentFolder;
$this->name = $name;
}

/**
* Get the name
*
* @return string
*/
public function getName() {
return $this->name;
}

/**
* Get the size in bytes
*
* @return int
*/
public function getSize() {
if ($this->file) {
return $this->file->getSize();
} else {
return 0;
}
}

/**
* Get the ETag
*
* @return string
*/
public function getETag() {
if ($this->file) {
return $this->file->getEtag();
} else {
return '';
}
}

/**
* Get the last modification time
*
* @return int
*/
public function getMTime() {
if ($this->file) {
return $this->file->getMTime();
} else {
return time();
}
}

/**
* Get the content
*
* @return string
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getContent() {
if ($this->file) {
$result = $this->file->getContent();

if ($result === false) {
$this->checkFile();
}

return $result;
} else {
return '';
}
}

/**
* Overwrite the file
*
* @param string|resource $data
* @throws NotPermittedException
* @throws NotFoundException
*/
public function putContent($data) {
try {
if ($this->file) {
$this->file->putContent($data);
} else {
$this->file = $this->parentFolder->newFile($this->name, $data);
}
} catch (NotFoundException $e) {
$this->checkFile();
}
}

/**
* Sometimes there are some issues with the AppData. Most of them are from
* user error. But we should handle them gracefull anyway.
*
* If for some reason the current file can't be found. We remove it.
* Then traverse up and check all folders if they exists. This so that the
* next request will have a valid appdata structure again.
*
* @throws NotFoundException
*/
private function checkFile() {
$cur = $this->file;

while ($cur->stat() === false) {
$parent = $cur->getParent();
try {
$cur->delete();
} catch (NotFoundException $e) {
// Just continue then
}
$cur = $parent;
}

if ($cur !== $this->file) {
throw new NotFoundException('File does not exist');
}
}


/**
* Delete the file
*
* @throws NotPermittedException
*/
public function delete() {
if ($this->file) {
$this->file->delete();
}
}

/**
* Get the MimeType
*
* @return string
*/
public function getMimeType() {
if ($this->file) {
return $this->file->getMimeType();
} else {
return 'text/plain';
}
}

/**
* Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
*
* @return resource
* @throws \OCP\Files\NotPermittedException
* @since 14.0.0
*/
public function read() {
if ($this->file) {
return $this->file->fopen('r');
} else {
return fopen('php://temp', 'r');
}
}

/**
* Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
*
* @return resource
* @throws \OCP\Files\NotPermittedException
* @since 14.0.0
*/
public function write() {
if ($this->file) {
return $this->file->fopen('w');
} else {
$source = fopen('php://temp', 'w+');
return CallbackWrapper::wrap($source, null, null, null, null, function () use ($source) {
rewind($source);
$this->putContent($source);
});
}
}
}
12 changes: 8 additions & 4 deletions lib/private/Files/SimpleFS/SimpleFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ public function getFile($name) {
return new SimpleFile($file);
}

public function newFile($name) {
$file = $this->folder->newFile($name);

return new SimpleFile($file);
public function newFile($name, $content = null) {
if ($content === null) {
// delay creating the file until it's written to
return new NewSimpleFile($this->folder, $name);
} else {
$file = $this->folder->newFile($name, $content);
return new SimpleFile($file);
}
}
}
3 changes: 2 additions & 1 deletion lib/public/Files/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ public function newFolder($path);
* Create a new file
*
* @param string $path relative path of the new file
* @param string|resource|null $content content for the new file, since 19.0.0
* @return \OCP\Files\File
* @throws \OCP\Files\NotPermittedException
* @since 6.0.0
*/
public function newFile($path);
public function newFile($path, $content = null);

/**
* search for files with the name matching $query
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Files/SimpleFS/ISimpleFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ public function getFile($name);
* Creates a new file with $name in the folder
*
* @param string $name
* @param string|resource|null $content @since 19.0.0
* @return ISimpleFile
* @throws NotPermittedException
* @since 11.0.0
*/
public function newFile($name);
public function newFile($name, $content = null);

/**
* Remove the folder and all the files in it
Expand Down
Loading

0 comments on commit de34786

Please sign in to comment.