Skip to content

Commit

Permalink
reuse l10n and request in dav folder listing
Browse files Browse the repository at this point in the history
instead of having to query those once for every node

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Aug 18, 2023
1 parent 2ea6c53 commit 36d53c6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 67 deletions.
14 changes: 10 additions & 4 deletions apps/dav/lib/Connector/Sabre/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@
use OC\Files\Mount\MoveableMount;
use OC\Files\View;
use OC\Metadata\FileMetadata;
use OCA\DAV\AppInfo\Application;
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCA\DAV\Upload\FutureFile;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\ForbiddenException;
use OCP\Files\InvalidPathException;
use OCP\Files\NotPermittedException;
use OCP\Files\StorageNotAvailableException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\L10N\IFactory;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -203,7 +206,7 @@ public function createDirectory($name) {
* @throws \Sabre\DAV\Exception\NotFound
* @throws \Sabre\DAV\Exception\ServiceUnavailable
*/
public function getChild($name, $info = null) {
public function getChild($name, $info = null, IRequest $request = null, IL10N $l10n = null) {
if (!$this->info->isReadable()) {
// avoid detecting files through this way
throw new NotFound();
Expand All @@ -230,7 +233,7 @@ public function getChild($name, $info = null) {
if ($info->getMimeType() === FileInfo::MIMETYPE_FOLDER) {
$node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager);
} else {
$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager);
$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager, $request, $l10n);
}
if ($this->tree) {
$this->tree->cacheNode($node);
Expand Down Expand Up @@ -265,8 +268,11 @@ public function getChildren() {
}

$nodes = [];
$request = \OC::$server->get(IRequest::class);
$l10nFactory = \OC::$server->get(IFactory::class);
$l10n = $l10nFactory->get(Application::APP_ID);
foreach ($folderContent as $info) {
$node = $this->getChild($info->getName(), $info);
$node = $this->getChild($info->getName(), $info, $request, $l10n);
$nodes[] = $node;
}
$this->dirContent = $nodes;
Expand Down
72 changes: 43 additions & 29 deletions apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
use OCP\Files\Storage;
use OCP\Files\StorageNotAvailableException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\L10N\IFactory as IL10NFactory;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
Expand All @@ -77,8 +78,7 @@
use Sabre\DAV\IFile;

class File extends Node implements IFile {
protected $request;

protected IRequest $request;
protected IL10N $l10n;

/** @var array<string, FileMetadata> */
Expand All @@ -89,21 +89,26 @@ class File extends Node implements IFile {
*
* @param \OC\Files\View $view
* @param \OCP\Files\FileInfo $info
* @param \OCP\Share\IManager $shareManager
* @param \OC\AppFramework\Http\Request $request
* @param ?\OCP\Share\IManager $shareManager
* @param ?IRequest $request
* @param ?IL10N $l10n
*/
public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) {
public function __construct(View $view, FileInfo $info, IManager $shareManager = null, IRequest $request = null, IL10N $l10n = null) {
parent::__construct($view, $info, $shareManager);

// Querying IL10N directly results in a dependency loop
/** @var IL10NFactory $l10nFactory */
$l10nFactory = \OC::$server->get(IL10NFactory::class);
$this->l10n = $l10nFactory->get(Application::APP_ID);
if ($l10n) {
$this->l10n = $l10n;
} else {
// Querying IL10N directly results in a dependency loop
/** @var IL10NFactory $l10nFactory */
$l10nFactory = \OC::$server->get(IL10NFactory::class);
$this->l10n = $l10nFactory->get(Application::APP_ID);
}

if (isset($request)) {
$this->request = $request;
} else {
$this->request = \OC::$server->getRequest();
$this->request = \OC::$server->get(IRequest::class);
}
}

Expand Down Expand Up @@ -149,7 +154,8 @@ public function put($data) {
$this->verifyPath();

// chunked handling
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
$chunkedHeader = $this->request->getHeader('oc-chunked');
if ($chunkedHeader) {
try {
return $this->createFileChunked($data);
} catch (\Exception $e) {
Expand Down Expand Up @@ -272,8 +278,9 @@ public function put($data) {

if ($result === false) {
$expected = -1;
if (isset($_SERVER['CONTENT_LENGTH'])) {
$expected = (int)$_SERVER['CONTENT_LENGTH'];
$lengthHeader = $this->request->getHeader('content-length');
if ($lengthHeader) {
$expected = (int)$lengthHeader;
}
if ($expected !== 0) {
throw new Exception(
Expand All @@ -291,8 +298,9 @@ public function put($data) {
// if content length is sent by client:
// double check if the file was fully received
// compare expected and actual size
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
$expected = (int)$_SERVER['CONTENT_LENGTH'];
$lengthHeader = $this->request->getHeader('content-length');
if ($lengthHeader && $this->request->getMethod() === 'PUT') {
$expected = (int)$lengthHeader;
if ($count !== $expected) {
throw new BadRequest(
$this->l10n->t(
Expand Down Expand Up @@ -374,8 +382,9 @@ public function put($data) {
}

// allow sync clients to send the mtime along in a header
if (isset($this->request->server['HTTP_X_OC_MTIME'])) {
$mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']);
$mtimeHeader = $this->request->getHeader('x-oc-mtime');
if ($mtimeHeader !== '') {
$mtime = $this->sanitizeMtime($mtimeHeader);
if ($this->fileView->touch($this->path, $mtime)) {
$this->header('X-OC-MTime: accepted');
}
Expand All @@ -386,8 +395,9 @@ public function put($data) {
];

// allow sync clients to send the creation time along in a header
if (isset($this->request->server['HTTP_X_OC_CTIME'])) {
$ctime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_CTIME']);
$ctimeHeader = $this->request->getHeader('x-oc-ctime');
if ($ctimeHeader) {
$ctime = $this->sanitizeMtime($ctimeHeader);
$fileInfoUpdate['creation_time'] = $ctime;
$this->header('X-OC-CTime: accepted');
}
Expand All @@ -400,8 +410,9 @@ public function put($data) {

$this->refreshInfo();

if (isset($this->request->server['HTTP_OC_CHECKSUM'])) {
$checksum = trim($this->request->server['HTTP_OC_CHECKSUM']);
$checksumHeader = $this->request->getHeader('oc-checksum');
if ($checksumHeader) {
$checksum = trim($checksumHeader);
$this->setChecksum($checksum);
} elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') {
$this->setChecksum('');
Expand Down Expand Up @@ -557,7 +568,7 @@ public function getContentType() {
$mimeType = $this->info->getMimetype();

// PROPFIND needs to return the correct mime type, for consistency with the web UI
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') {
if ($this->request->getMethod() === 'PROPFIND') {
return $mimeType;
}
return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType);
Expand Down Expand Up @@ -599,9 +610,10 @@ private function createFileChunked($data) {
$bytesWritten = $chunk_handler->store($info['index'], $data);

//detect aborted upload
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
if (isset($_SERVER['CONTENT_LENGTH'])) {
$expected = (int)$_SERVER['CONTENT_LENGTH'];
if ($this->request->getMethod() === 'PUT') {
$lengthHeader = $this->request->getHeader('content-length');
if ($lengthHeader) {
$expected = (int)$lengthHeader;
if ($bytesWritten !== $expected) {
$chunk_handler->remove($info['index']);
throw new BadRequest(
Expand Down Expand Up @@ -667,8 +679,9 @@ private function createFileChunked($data) {
}

// allow sync clients to send the mtime along in a header
if (isset($this->request->server['HTTP_X_OC_MTIME'])) {
$mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']);
$mtimeHeader = $this->request->getHeader('x-oc-mtime');
if ($mtimeHeader !== '') {
$mtime = $this->sanitizeMtime($mtimeHeader);
if ($targetStorage->touch($targetInternalPath, $mtime)) {
$this->header('X-OC-MTime: accepted');
}
Expand All @@ -684,8 +697,9 @@ private function createFileChunked($data) {
// FIXME: should call refreshInfo but can't because $this->path is not the of the final file
$info = $this->fileView->getFileInfo($targetPath);

if (isset($this->request->server['HTTP_OC_CHECKSUM'])) {
$checksum = trim($this->request->server['HTTP_OC_CHECKSUM']);
$checksumHeader = $this->request->getHeader('oc-checksum');
if ($checksumHeader) {
$checksum = trim($checksumHeader);
$this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]);
} elseif ($info->getChecksum() !== null && $info->getChecksum() !== '') {
$this->fileView->putFileInfo($this->path, ['checksum' => '']);
Expand Down

0 comments on commit 36d53c6

Please sign in to comment.