Skip to content

Commit

Permalink
refactor(files_external): Add Storage parameter strong types
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Oct 1, 2024
1 parent c8087f2 commit d7aa284
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 175 deletions.
54 changes: 25 additions & 29 deletions apps/files_external/lib/Lib/Storage/AmazonS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ public function __construct($parameters) {
$this->logger = Server::get(LoggerInterface::class);
}

/**
* @param string $path
* @return string correctly encoded path
*/
private function normalizePath($path): string {
private function normalizePath(string $path): string {
$path = trim($path, '/');

if (!$path) {
Expand All @@ -73,11 +69,11 @@ private function normalizePath($path): string {
return $path;
}

private function isRoot($path): bool {
private function isRoot(string $path): bool {
return $path === '.';
}

private function cleanKey($path): string {
private function cleanKey(string $path): string {
if ($this->isRoot($path)) {
return '/';
}
Expand All @@ -90,7 +86,7 @@ private function clearCache(): void {
$this->filesCache = new CappedMemoryCache();
}

private function invalidateCache($key): void {
private function invalidateCache(string $key): void {
unset($this->objectCache[$key]);
$keys = array_keys($this->objectCache->getData());
$keyLength = strlen($key);
Expand Down Expand Up @@ -143,7 +139,7 @@ private function headObject(string $key): array|false {
*
* @throws \Exception
*/
private function doesDirectoryExist($path): bool {
private function doesDirectoryExist(string $path): bool {
if ($path === '.' || $path === '') {
return true;
}
Expand Down Expand Up @@ -185,7 +181,7 @@ private function doesDirectoryExist($path): bool {
return false;
}

protected function remove($path): bool {
protected function remove(string $path): bool {
// remember fileType to reduce http calls
$fileType = $this->filetype($path);
if ($fileType === 'dir') {
Expand All @@ -197,7 +193,7 @@ protected function remove($path): bool {
}
}

public function mkdir($path): bool {
public function mkdir(string $path): bool {
$path = $this->normalizePath($path);

if ($this->is_dir($path)) {
Expand Down Expand Up @@ -225,12 +221,12 @@ public function mkdir($path): bool {
return true;
}

public function file_exists($path): bool {
public function file_exists(string $path): bool {
return $this->filetype($path) !== false;
}


public function rmdir($path): bool {
public function rmdir(string $path): bool {
$path = $this->normalizePath($path);

if ($this->isRoot($path)) {
Expand All @@ -250,7 +246,7 @@ protected function clearBucket(): bool {
return $this->batchDelete();
}

private function batchDelete($path = null): bool {
private function batchDelete(?string $path = null): bool {
// TODO explore using https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.BatchDelete.html
$params = [
'Bucket' => $this->bucket
Expand Down Expand Up @@ -290,7 +286,7 @@ private function batchDelete($path = null): bool {
return true;
}

public function opendir($path) {
public function opendir(string $path) {
try {
$content = iterator_to_array($this->getDirectoryContent($path));
return IteratorDirectory::wrap(array_map(function (array $item) {
Expand All @@ -301,7 +297,7 @@ public function opendir($path) {
}
}

public function stat($path): array|false {
public function stat(string $path): array|false {
$path = $this->normalizePath($path);

if ($this->is_dir($path)) {
Expand All @@ -324,7 +320,7 @@ public function stat($path): array|false {
* When the information is already present (e.g. opendir has been called before)
* this value is return. Otherwise a headObject is emitted.
*/
private function getContentLength($path): int {
private function getContentLength(string $path): int {
if (isset($this->filesCache[$path])) {
return (int)$this->filesCache[$path]['ContentLength'];
}
Expand All @@ -343,7 +339,7 @@ private function getContentLength($path): int {
* When the information is already present (e.g. opendir has been called before)
* this value is return. Otherwise a headObject is emitted.
*/
private function getLastModified($path): string {
private function getLastModified(string $path): string {
if (isset($this->filesCache[$path])) {
return $this->filesCache[$path]['LastModified'];
}
Expand All @@ -356,7 +352,7 @@ private function getLastModified($path): string {
return 'now';
}

public function is_dir($path): bool {
public function is_dir(string $path): bool {
$path = $this->normalizePath($path);

if (isset($this->filesCache[$path])) {
Expand All @@ -374,7 +370,7 @@ public function is_dir($path): bool {
}
}

public function filetype($path): string|false {
public function filetype(string $path): string|false {
$path = $this->normalizePath($path);

if ($this->isRoot($path)) {
Expand Down Expand Up @@ -402,15 +398,15 @@ public function filetype($path): string|false {
return false;
}

public function getPermissions($path): int {
public function getPermissions(string $path): int {
$type = $this->filetype($path);
if (!$type) {
return 0;
}
return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
}

public function unlink($path): bool {
public function unlink(string $path): bool {
$path = $this->normalizePath($path);

if ($this->is_dir($path)) {
Expand All @@ -431,7 +427,7 @@ public function unlink($path): bool {
return true;
}

public function fopen($path, $mode) {
public function fopen(string $path, string $mode) {
$path = $this->normalizePath($path);

switch ($mode) {
Expand Down Expand Up @@ -489,7 +485,7 @@ public function fopen($path, $mode) {
return false;
}

public function touch($path, $mtime = null): bool {
public function touch(string $path, ?int $mtime = null): bool {
if (is_null($mtime)) {
$mtime = time();
}
Expand Down Expand Up @@ -524,7 +520,7 @@ public function touch($path, $mtime = null): bool {
return true;
}

public function copy($source, $target, $isFile = null): bool {
public function copy(string $source, string $target, ?bool $isFile = null): bool {
$source = $this->normalizePath($source);
$target = $this->normalizePath($target);

Expand Down Expand Up @@ -567,7 +563,7 @@ public function copy($source, $target, $isFile = null): bool {
return true;
}

public function rename($source, $target): bool {
public function rename(string $source, string $target): bool {
$source = $this->normalizePath($source);
$target = $this->normalizePath($target);

Expand Down Expand Up @@ -605,7 +601,7 @@ public function getId(): string {
return $this->id;
}

public function writeBack($tmpFile, $path): bool {
public function writeBack(string $tmpFile, string $path): bool {
try {
$source = fopen($tmpFile, 'r');
$this->writeObject($path, $source, $this->mimeDetector->detectPath($path));
Expand All @@ -629,7 +625,7 @@ public static function checkDependencies(): bool {
return true;
}

public function getDirectoryContent($directory): \Traversable {
public function getDirectoryContent(string $directory): \Traversable {
$path = $this->normalizePath($directory);

if ($this->isRoot($path)) {
Expand Down Expand Up @@ -726,7 +722,7 @@ protected function getVersioningStatusFromBucket(): bool {
}
}

public function hasUpdated($path, $time): bool {
public function hasUpdated(string $path, int $time): bool {
// for files we can get the proper mtime
if ($path !== '' && $object = $this->headObject($path)) {
$stat = $this->objectToMetaData($object);
Expand Down
37 changes: 17 additions & 20 deletions apps/files_external/lib/Lib/Storage/FTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getId(): string {
return 'ftp::' . $this->username . '@' . $this->host . '/' . $this->root;
}

protected function buildPath($path): string {
protected function buildPath(string $path): string {
return rtrim($this->root . '/' . $path, '/');
}

Expand All @@ -94,7 +94,7 @@ public static function checkDependencies(): array|bool {
}
}

public function filemtime($path): int|false {
public function filemtime(string $path): int|false {
$result = $this->getConnection()->mdtm($this->buildPath($path));

if ($result === -1) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public function filemtime($path): int|false {
}
}

public function filesize($path): false|int|float {
public function filesize(string $path): false|int|float {
$result = $this->getConnection()->size($this->buildPath($path));
if ($result === -1) {
return false;
Expand All @@ -135,7 +135,7 @@ public function filesize($path): false|int|float {
}
}

public function rmdir($path): bool {
public function rmdir(string $path): bool {
if ($this->is_dir($path)) {
$result = $this->getConnection()->rmdir($this->buildPath($path));
// recursive rmdir support depends on the ftp server
Expand All @@ -151,10 +151,7 @@ public function rmdir($path): bool {
}
}

/**
* @param string $path
*/
private function recursiveRmDir($path): bool {
private function recursiveRmDir(string $path): bool {
$contents = $this->getDirectoryContent($path);
$result = true;
foreach ($contents as $content) {
Expand All @@ -177,7 +174,7 @@ public function test(): bool {
}
}

public function stat($path): array|false {
public function stat(string $path): array|false {
if (!$this->file_exists($path)) {
return false;
}
Expand All @@ -187,14 +184,14 @@ public function stat($path): array|false {
];
}

public function file_exists($path): bool {
public function file_exists(string $path): bool {
if ($path === '' || $path === '.' || $path === '/') {
return true;
}
return $this->filetype($path) !== false;
}

public function unlink($path): bool {
public function unlink(string $path): bool {
switch ($this->filetype($path)) {
case 'dir':
return $this->rmdir($path);
Expand All @@ -205,19 +202,19 @@ public function unlink($path): bool {
}
}

public function opendir($path) {
public function opendir(string $path) {
$files = $this->getConnection()->nlist($this->buildPath($path));
return IteratorDirectory::wrap($files);
}

public function mkdir($path): bool {
public function mkdir(string $path): bool {
if ($this->is_dir($path)) {
return false;
}
return $this->getConnection()->mkdir($this->buildPath($path)) !== false;
}

public function is_dir($path): bool {
public function is_dir(string $path): bool {
if ($path === '') {
return true;
}
Expand All @@ -229,11 +226,11 @@ public function is_dir($path): bool {
}
}

public function is_file($path): bool {
public function is_file(string $path): bool {
return $this->filesize($path) !== false;
}

public function filetype($path): string|false {
public function filetype(string $path): string|false {
if ($this->is_dir($path)) {
return 'dir';
} elseif ($this->is_file($path)) {
Expand All @@ -243,7 +240,7 @@ public function filetype($path): string|false {
}
}

public function fopen($path, $mode) {
public function fopen(string $path, string $mode) {
$useExisting = true;
switch ($mode) {
case 'r':
Expand Down Expand Up @@ -309,7 +306,7 @@ public function readStream(string $path) {
return $stream;
}

public function touch($path, $mtime = null): bool {
public function touch(string $path, ?int $mtime = null): bool {
if ($this->file_exists($path)) {
return false;
} else {
Expand All @@ -318,12 +315,12 @@ public function touch($path, $mtime = null): bool {
}
}

public function rename($source, $target): bool {
public function rename(string $source, string $target): bool {
$this->unlink($target);
return $this->getConnection()->rename($this->buildPath($source), $this->buildPath($target));
}

public function getDirectoryContent($directory): \Traversable {
public function getDirectoryContent(string $directory): \Traversable {
$files = $this->getConnection()->mlsd($this->buildPath($directory));
$mimeTypeDetector = \OC::$server->getMimeTypeDetector();

Expand Down
Loading

0 comments on commit d7aa284

Please sign in to comment.