diff --git a/lib/private/Files/Storage/LocalTempFileTrait.php b/lib/private/Files/Storage/LocalTempFileTrait.php index 805a67084ccaa..4114081de3942 100644 --- a/lib/private/Files/Storage/LocalTempFileTrait.php +++ b/lib/private/Files/Storage/LocalTempFileTrait.php @@ -1,7 +1,7 @@ stat() . + * This trait caches per-path temporary local copies created from storage streams, + * so repeated local-file access can reuse the same temp file during the instance + * lifetime. */ trait LocalTempFileTrait { /** @var array */ protected array $cachedFiles = []; + /** + * Returns the temporary local file path associated with the specified storage file. + * + * Creates a temp file on first use if necessary. Caches for repeated (instance-level) access. + * + * @param string $path Storage-internal path + * @return string|false Local temp file path, or false if the source cannot be opened/copied + */ protected function getCachedFile(string $path): string|false { if (!isset($this->cachedFiles[$path])) { $this->cachedFiles[$path] = $this->toTmpFile($path); @@ -32,28 +38,49 @@ protected function getCachedFile(string $path): string|false { return $this->cachedFiles[$path]; } + /** + * Invalidate the cached temp local file entry for the specified storage file. + * + * @param string $path Storage-internal path + */ protected function removeCachedFile(string $path): void { unset($this->cachedFiles[$path]); } - protected function toTmpFile(string $path): string|false { //no longer in the storage api, still useful here + /** + * Copies a storage file stream into a temporary local file. + * + * The temporary local file keeps the same extension (if any) as the source file. + * + * @param string $path Storage-internal path + * @return string|false Local temp file path, or false on failure + */ + protected function toTmpFile(string $path): string|false { $source = $this->fopen($path, 'r'); if (!$source) { return false; } - if ($pos = strrpos($path, '.')) { - $extension = substr($path, $pos); - } else { - $extension = ''; - } + + $ext = pathinfo($path, PATHINFO_EXTENSION); + $extension = $ext !== '' ? '.' . $ext : ''; + $tmpFile = Server::get(ITempManager::class)->getTemporaryFile($extension); $target = fopen($tmpFile, 'w'); - $result = stream_copy_to_stream($source, $target); - fclose($target); - if ($result === false) { + if ($target === false) { + fclose($source); return false; } + try { + $result = stream_copy_to_stream($source, $target); + if ($result === false) { + return false; + } + } finally { + fclose($target); + fclose($source); + } + return $tmpFile; } }