Commit 843d57f1 authored by Richard Weinhold's avatar Richard Weinhold 🎩

fixes some minor issues

- check for strict timestamp conversion in flysystem getTime()
- unknown $destination in Disk::writeFromStream()
- adapts codeformat to vscode csfixer
parent ced08bcc
Pipeline #5779 passed with stages
in 22 seconds
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem;
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Enum\Hash;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Helper\DirectoryIterator;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Directory;
use ricwein\FileSystem\Directory;
......@@ -161,8 +163,11 @@ class Command extends Directory
$this->lastCommand = $cmd;
switch (true) {
case $this->storage instanceof Storage\Disk: $path = $this->storage->path()->real; break;
default: throw new RuntimeException(sprintf('unsupported storage system for Command-Execution: %s', get_class($this->storage)), 500);
case $this->storage instanceof Storage\Disk:
$path = $this->storage->path()->real;
break;
default:
throw new RuntimeException(sprintf('unsupported storage system for Command-Execution: %s', get_class($this->storage)), 500);
}
if (!chdir($path)) {
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Enum;
/**
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class AccessDeniedException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class ConstraintsException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* Base of all FileSystem Exceptions
*/
class Exception extends \Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class FileNotFoundException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class NotAFileException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class RuntimeException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class UnexpectedValueException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Exceptions;
/**
* the selected file was not found (mostly for disk-storage)
*/
class UnsupportedException extends Exception
{
}
{ }
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem;
use ricwein\FileSystem\Exceptions\ConstraintsException;
......@@ -124,10 +126,10 @@ class File extends FileSystem
}
/**
* @param Storage &$destination mutable
* @param int|null $constraints
* @return bool success
* @throws AccessDeniedException|FileNotFoundException
* @param Storage &$destination mutable
* @param int|null $constraints
* @return bool success
* @throws AccessDeniedException|FileNotFoundException
*/
protected function copyFileTo(Storage &$destination, ?int $constraints = null): bool
{
......@@ -260,11 +262,11 @@ class File extends FileSystem
}
/**
* @param bool $ifNewOnly
* @param null|int $time last-modified time
* @param null|int $atime last-access time
* @return bool
* @throws AccessDeniedException
* @param bool $ifNewOnly
* @param null|int $time last-modified time
* @param null|int $atime last-access time
* @return bool
* @throws AccessDeniedException
*/
public function touch(bool $ifNewOnly = false, ?int $time = null, ?int $atime = null): bool
{
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\File;
use Intervention\Image\Constraint as IConstraint;
......@@ -172,7 +174,7 @@ class Image extends File
}
}
throw new RuntimeException(sprintf('unable to reduce filesize to less than %1.2f MB', $filesize / (2**20), 2), 400);
throw new RuntimeException(sprintf('unable to reduce filesize to less than %1.2f MB', $filesize / (2 ** 20), 2), 400);
});
}
}
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\File;
use ZipArchive;
......@@ -390,10 +392,17 @@ class Zip extends File
// add file to archive
switch (true) {
case $storage instanceof Storage\Disk: $name = $this->addFileFromDisk($storage, $name); break;
case $storage instanceof Storage\Memory: $name = $this->addFileFromMemory($storage, $name); break;
case $storage instanceof Storage\Flysystem: $name = $this->addFileFromFlysystem($storage, $name); break;
default: throw new UnexpectedValueException(sprintf('invalid type for 1 Argument in %s(), expected instance of Storage or File, but "%s" given', __METHOD__, is_object($storage) ? get_class($storage) : gettype($storage)), 500);
case $storage instanceof Storage\Disk:
$name = $this->addFileFromDisk($storage, $name);
break;
case $storage instanceof Storage\Memory:
$name = $this->addFileFromMemory($storage, $name);
break;
case $storage instanceof Storage\Flysystem:
$name = $this->addFileFromFlysystem($storage, $name);
break;
default:
throw new UnexpectedValueException(sprintf('invalid type for 1 Argument in %s(), expected instance of Storage or File, but "%s" given', __METHOD__, is_object($storage) ? get_class($storage) : gettype($storage)), 500);
}
// set custom compression algorithm
......@@ -516,7 +525,7 @@ class Zip extends File
* @param string|null $forFile
* @return self
*/
public function setComment(string $comment, ?string $forFile = null):self
public function setComment(string $comment, ?string $forFile = null): self
{
if (!$this->isOpen) {
$this->openArchive();
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem;
use ricwein\FileSystem\Enum\Hash;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Helper;
use ricwein\FileSystem\Exceptions\ConstraintsException;
......@@ -92,7 +94,7 @@ class Constraint
// iterative exception chaining:
if (($this->failedFor & $constraint) === $constraint) {
$previous = new ConstraintsException('[' . $constraint . '] - constraint failed'. (isset($this->errors[$constraint]) ? (': ' . $this->errors[$constraint]) : ''), 500, $previous);
$previous = new ConstraintsException('[' . $constraint . '] - constraint failed' . (isset($this->errors[$constraint]) ? (': ' . $this->errors[$constraint]) : ''), 500, $previous);
}
}
......@@ -133,8 +135,7 @@ class Constraint
($this->constraints & self::IN_SAFEPATH) === self::IN_SAFEPATH
&& (
(file_exists($path->raw) && $path->raw !== $path->real && strpos($path->real, $path->safepath) !== 0)
|| (!file_exists($path->raw) && strpos($path->raw, $path->safepath) !== 0)
)
|| (!file_exists($path->raw) && strpos($path->raw, $path->safepath) !== 0))
) {
$this->failedFor |= self::IN_SAFEPATH;
$this->errors[self::IN_SAFEPATH] = sprintf('the given path (%s) is not within the safepath (%s)', $path->raw, $path->safepath);
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Helper;
use ricwein\FileSystem\Storage;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Helper;
/**
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Helper;
use ricwein\FileSystem\Storage;
......@@ -61,9 +63,9 @@ class Path
protected $filename = null;
/**
* name of a directory or file without file-extension
* e.g.: test
* @var string
* name of a directory or file without file-extension
* e.g.: test
* @var string
*/
protected $basename;
......@@ -105,8 +107,8 @@ class Path
/**
* parse each path-component and extract path-info
* @return string[]
* @throws UnexpectedValueException
* @return string[]
* @throws UnexpectedValueException
*/
protected function normalizePathComponents(): array
{
......@@ -170,7 +172,6 @@ class Path
case (is_object($component) && method_exists($component, '__toString')):
return (string) $component;
}
throw new UnexpectedValueException(sprintf('invalid path-component of type \'%s\'', is_object($component) ? get_class($component) : gettype($component)), 500);
......@@ -267,8 +268,8 @@ class Path
}
/**
* @return \SplFileInfo
*/
* @return \SplFileInfo
*/
public function fileInfo(): \SplFileInfo
{
if (!$this->loaded) {
......@@ -283,7 +284,7 @@ class Path
* check if path is in open_basedir restrictions
* @return bool
*/
public function isInOpenBasedir() : bool
public function isInOpenBasedir(): bool
{
if (!$this->loaded) {
$this->resolvePath();
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Helper;
use ricwein\FileSystem\Storage;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem;
use ricwein\FileSystem\Helper\Constraint;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage;
use ricwein\FileSystem\Storage;
......@@ -279,9 +281,14 @@ class Disk extends Storage
// try to remove files/dirs/links
switch ($splFile->getType()) {
case 'dir': rmdir($splFile->getRealPath()); break;
case 'link': unlink($splFile->getPathname()); break;
default: unlink($splFile->getRealPath());
case 'dir':
rmdir($splFile->getRealPath());
break;
case 'link':
unlink($splFile->getPathname());
break;
default:
unlink($splFile->getRealPath());
}
}
......@@ -359,10 +366,14 @@ class Disk extends Storage
}
switch ($mode) {
case Hash::CONTENT: return hash_file($algo, $this->path->real, false);
case Hash::FILENAME: return hash($algo, $this->path->filename, false);
case Hash::FILEPATH: return hash($algo, $this->path->real, false);
default: throw new RuntimeException('unknown hashing-mode', 500);
case Hash::CONTENT:
return hash_file($algo, $this->path->real, false);
case Hash::FILENAME:
return hash($algo, $this->path->filename, false);
case Hash::FILEPATH:
return hash($algo, $this->path->real, false);
default:
throw new RuntimeException('unknown hashing-mode', 500);
}
}
......@@ -471,7 +482,7 @@ class Disk extends Storage
public function writeFromStream($stream): bool
{
if (!is_resource($stream)) {
throw new RuntimeException(sprintf('file-handle must be of type \'resource\' but \'%s\' given', is_object($handle) ? get_class($handle) : gettype($handle)), 500);
throw new RuntimeException(sprintf('file-handle must be of type \'resource\' but \'%s\' given', is_object($stream) ? get_class($stream) : gettype($stream)), 500);
}
$destStream = $this->getStream('w');
......@@ -480,7 +491,6 @@ class Disk extends Storage
return false;
}
$destination->path()->reload();
return true;
} finally {
\fclose($destStream);
......@@ -494,7 +504,7 @@ class Disk extends Storage
{
switch (true) {
// copy file from disk to disk
// copy file from disk to disk
case $destination instanceof Disk:
if (!\copy($this->path->real, $destination->path()->raw)) {
return false;
......@@ -505,7 +515,11 @@ class Disk extends Storage
case $destination instanceof Flysystem:
$readStream = $this->getStream('r');
try {
return $destination->writeFromStream($readStream);
if ($destination->writeFromStream($readStream) === true) {
$destination->path()->reload();
return true;
}
return false;
} finally {
\fclose($readStream);
}
......@@ -517,13 +531,13 @@ class Disk extends Storage
}
/**
* @inheritDoc
* @inheritDoc
*/
public function moveFileTo(Storage $destination): bool
{
switch (true) {
// copy file from disk to disk
// copy file from disk to disk
case $destination instanceof Disk:
if (!\rename($this->path->real, $destination->path()->raw)) {
return false;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Disk;
use ricwein\FileSystem\FileSystem;
......@@ -26,10 +28,7 @@ class Current extends Disk
// check if our first (left) pathcomponent references to root (/),
// only inject current working directory if this is not the case
if (
empty($path) ||
(is_string($fistComponent) && strpos($fistComponent, DIRECTORY_SEPARATOR) !== 0) ||
($fistComponent instanceof Path && strpos($fistComponent->raw, DIRECTORY_SEPARATOR) !== 0) ||
($fistComponent instanceof FileSystem && strpos($fistComponent->path()->raw, DIRECTORY_SEPARATOR) !== 0)
empty($path) || (is_string($fistComponent) && strpos($fistComponent, DIRECTORY_SEPARATOR) !== 0) || ($fistComponent instanceof Path && strpos($fistComponent->raw, DIRECTORY_SEPARATOR) !== 0) || ($fistComponent instanceof FileSystem && strpos($fistComponent->path()->raw, DIRECTORY_SEPARATOR) !== 0)
) {
array_unshift($path, getcwd());
}
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Disk;
use ricwein\FileSystem\Helper\Constraint;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Disk;
use ricwein\FileSystem\Helper\Path;
......@@ -120,13 +122,13 @@ class Uploaded extends Disk
}
/**
* @inheritDoc
* @inheritDoc
*/
public function moveFileTo(Storage $destination): bool
{
switch (true) {
// use native safe-move function for uploaded files
// use native safe-move function for uploaded files
case $destination instanceof Disk:
if (!\move_uploaded_file($this->path->real, $destination->path()->raw)) {
return false;
......@@ -134,7 +136,7 @@ class Uploaded extends Disk
$destination->path()->reload();
return true;
// use a temp-file for safe-move before moving file into destination-storage
// use a temp-file for safe-move before moving file into destination-storage
case $destination instanceof Storage\Flysystem:
case $destination instanceof Storage\Memory:
default:
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Extensions;
use ricwein\FileSystem\Storage;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Extensions\Binary;
use ricwein\FileSystem\Storage\Disk as DiskStorage;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Extensions\Binary;
use ricwein\FileSystem\Storage\Memory as MemoryStorage;
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage;
use League\Flysystem\Filesystem as FlyFilesystem;
......@@ -299,18 +301,26 @@ class Flysystem extends Storage
}
switch ($mode) {
case Hash::CONTENT: return (new Stream($this->getStream()))->closeOnFree()->hash($algo);
case Hash::FILENAME: return hash($algo, $this->path, false);
default: throw new UnsupportedException('filepath-hashes are not supported by flysystem adapters', 500);
case Hash::CONTENT:
return (new Stream($this->getStream()))->closeOnFree()->hash($algo);
case Hash::FILENAME:
return hash($algo, $this->path, false);
default:
throw new UnsupportedException('filepath-hashes are not supported by flysystem adapters', 500);
}
}
/**
* @inheritDoc
* @throws RuntimeException
*/
public function getTime(): int
{
return $this->flysystem->getTimestamp($this->path);
if (false !== $timestamp = $this->flysystem->getTimestamp($this->path)) {
return (int) $timestamp;
}
throw new RuntimeException('unable to fetch timestamp', 500);
}
/**
......@@ -419,7 +429,7 @@ class Flysystem extends Storage
}
/**
* @inheritDoc
* @inheritDoc
*/
public function moveFileTo(Storage $destination): bool
{
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage;
use ricwein\FileSystem\Helper\Stream;
......@@ -51,7 +53,7 @@ class Memory extends Storage
/**
* @inheritDoc
*/
public function isDir():bool
public function isDir(): bool
{
return true;
}
......@@ -130,7 +132,7 @@ class Memory extends Storage
/**
* @inheritDoc
*/
public function removeFile():bool
public function removeFile(): bool
{
$this->content = null;
return true;
......@@ -158,9 +160,13 @@ class Memory extends Storage
public function getFileHash(int $mode = Hash::CONTENT, string $algo = 'sha256'): string
{
switch ($mode) {
case Hash::CONTENT: return hash($algo, $this->content ?? '', false);
case Hash::FILENAME: case Hash::FILEPATH: throw new RuntimeException('unable to calculate filepath/name hash for in-memory-files', 500);
default: throw new RuntimeException('unknown hashing-mode', 500);
case Hash::CONTENT:
return hash($algo, $this->content ?? '', false);
case Hash::FILENAME:
case Hash::FILEPATH:
throw new RuntimeException('unable to calculate filepath/name hash for in-memory-files', 500);
default:
throw new RuntimeException('unknown hashing-mode', 500);
}
}
......@@ -204,7 +210,7 @@ class Memory extends Storage
*/
public function getStream(string $mode = 'r+')
{
if (!in_array($mode, ['r+','w','w+','a+','x','x+','c+',true])) {
if (!in_array($mode, ['r+', 'w', 'w+', 'a+', 'x', 'x+', 'c+', true])) {
throw new RuntimeException(sprintf('unable to open stream for memory-storage in non-write mode "%s"', $mode), 500);
}
......@@ -255,7 +261,7 @@ class Memory extends Storage
}
/**
* @inheritDoc
* @inheritDoc
*/
public function moveFileTo(Storage $destination): bool
{
......
<?php
/**
* @author Richard Weinhold
*/
namespace ricwein\FileSystem\Storage\Memory;
use ricwein\FileSystem\Storage\Memory;