0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-05-17 19:52:39 +00:00
nextcloud_server/lib/private/Files/Storage/LocalTempFileTrait.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.5 KiB
PHP
Raw Normal View History

2015-04-02 14:44:25 +02:00
<?php
2015-04-02 14:44:25 +02:00
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
2015-04-02 14:44:25 +02:00
*/
namespace OC\Files\Storage;
/**
* Storage backend class for providing common filesystem operation methods
* which are not storage-backend specific.
*
* \OC\Files\Storage\Common is never used directly; it is extended by all other
* storage backends, where its methods may be overridden, and additional
* (backend-specific) methods are defined.
*
* Some \OC\Files\Storage\Common methods call functions which are first defined
* in classes which extend it, e.g. $this->stat() .
*/
trait LocalTempFileTrait {
/** @var array<string,string|false> */
protected array $cachedFiles = [];
2015-04-02 14:44:25 +02:00
protected function getCachedFile(string $path): string|false {
2015-04-02 14:44:25 +02:00
if (!isset($this->cachedFiles[$path])) {
$this->cachedFiles[$path] = $this->toTmpFile($path);
}
return $this->cachedFiles[$path];
}
protected function removeCachedFile(string $path): void {
2015-04-02 14:44:25 +02:00
unset($this->cachedFiles[$path]);
}
protected function toTmpFile(string $path): string|false { //no longer in the storage api, still useful here
2015-04-02 14:44:25 +02:00
$source = $this->fopen($path, 'r');
if (!$source) {
return false;
}
if ($pos = strrpos($path, '.')) {
$extension = substr($path, $pos);
} else {
$extension = '';
}
2015-12-18 11:25:33 +01:00
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
2015-04-02 14:44:25 +02:00
$target = fopen($tmpFile, 'w');
\OC_Helper::streamCopy($source, $target);
fclose($target);
return $tmpFile;
}
}