Improve typing in OC\Archive classes

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2022-02-24 12:03:36 +01:00
parent 6da8a6d62a
commit 7b1a044131
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
3 changed files with 50 additions and 18 deletions

View file

@ -31,7 +31,7 @@ namespace OC\Archive;
abstract class Archive {
/**
* @param $source
* @param string $source
*/
abstract public function __construct($source);
/**
@ -80,14 +80,14 @@ abstract class Archive {
/**
* get the content of a file
* @param string $path
* @return string
* @return string|false
*/
abstract public function getFile($path);
/**
* extract a single file from the archive
* @param string $path
* @param string $dest
* @return bool
* @return bool success
*/
abstract public function extractFile($path, $dest);
/**
@ -119,6 +119,7 @@ abstract class Archive {
* add a folder and all its content
* @param string $path
* @param string $source
* @return void
*/
public function addRecursive($path, $source) {
$dh = opendir($source);

View file

@ -39,13 +39,23 @@ class TAR extends Archive {
public const GZIP = 1;
public const BZIP = 2;
private $fileList;
private $cachedHeaders;
/**
* @var string[]|false
*/
private $fileList = false;
/**
* @var array|false
*/
private $cachedHeaders = false;
/**
* @var \Archive_Tar tar
* @var \Archive_Tar
*/
private $tar = null;
/**
* @var string
*/
private $path;
/**
@ -154,6 +164,7 @@ class TAR extends Archive {
/**
* @param string $file
* @return array|null
*/
private function getHeader($file) {
if (!$this->cachedHeaders) {
@ -244,10 +255,15 @@ class TAR extends Archive {
* get the content of a file
*
* @param string $path
* @return string
* @return string|false
*/
public function getFile($path) {
return $this->tar->extractInString($path);
$string = $this->tar->extractInString($path);
if (is_string($string)) {
return $string;
} else {
return false;
}
}
/**
@ -364,6 +380,9 @@ class TAR extends Archive {
/**
* write back temporary files
* @param string $tmpFile
* @param string $path
* @return void
*/
public function writeBack($tmpFile, $path) {
$this->addFile($path, $tmpFile);
@ -373,7 +392,7 @@ class TAR extends Archive {
/**
* reopen the archive to ensure everything is written
*/
private function reopen() {
private function reopen(): void {
if ($this->tar) {
$this->tar->_close();
$this->tar = null;

View file

@ -33,12 +33,17 @@ namespace OC\Archive;
use Icewind\Streams\CallbackWrapper;
use OCP\ILogger;
use Psr\Log\LoggerInterface;
class ZIP extends Archive {
/**
* @var \ZipArchive zip
*/
private $zip = null;
private $zip;
/**
* @var string
*/
private $path;
/**
@ -49,7 +54,7 @@ class ZIP extends Archive {
$this->zip = new \ZipArchive();
if ($this->zip->open($source, \ZipArchive::CREATE)) {
} else {
\OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
\OC::$server->get(LoggerInterface::class)->warning('Error while opening archive '.$source, ['app' => 'files_archive']);
}
}
/**
@ -82,12 +87,12 @@ class ZIP extends Archive {
* rename a file or folder in the archive
* @param string $source
* @param string $dest
* @return boolean|null
* @return bool
*/
public function rename($source, $dest) {
$source = $this->stripPath($source);
$dest = $this->stripPath($dest);
$this->zip->renameName($source, $dest);
return $this->zip->renameName($source, $dest);
}
/**
* get the uncompressed size of a file in the archive
@ -139,7 +144,7 @@ class ZIP extends Archive {
/**
* get the content of a file
* @param string $path
* @return string
* @return string|false
*/
public function getFile($path) {
return $this->zip->getFromName($path);
@ -148,11 +153,14 @@ class ZIP extends Archive {
* extract a single file from the archive
* @param string $path
* @param string $dest
* @return boolean|null
* @return bool
*/
public function extractFile($path, $dest) {
$fp = $this->zip->getStream($path);
file_put_contents($dest, $fp);
if ($fp === false) {
return false;
}
return file_put_contents($dest, $fp) !== false;
}
/**
* extract the archive
@ -195,8 +203,9 @@ class ZIP extends Archive {
//since we can't directly get a writable stream,
//make a temp copy of the file and put it back
//in the archive when the stream is closed
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
$lastPoint = strrpos($path, '.');
if ($lastPoint !== false) {
$ext = substr($path, $lastPoint);
} else {
$ext = '';
}
@ -213,6 +222,9 @@ class ZIP extends Archive {
/**
* write back temporary files
* @param string $tmpFile
* @param string $path
* @return void
*/
public function writeBack($tmpFile, $path) {
$this->addFile($path, $tmpFile);