Skip to content

Commit

Permalink
Refactor Upload::source() for better debug testing
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jun 13, 2024
1 parent 718d6e7 commit 9894d8a
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/Api/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function cleanTmpDirectory(): void
}

/**
* Throws the appropriate translated upload error message
* Throws an exception with the appropriate translated error message
*
* @throws \Exception Any upload error
*/
Expand Down Expand Up @@ -140,24 +140,6 @@ public static function filename(array $upload): string
return basename($upload['name']);
}

/**
* Move the tmp file to a location including the extension,
* for better mime detection
*
* @codeCoverageIgnore
*/
public function move(array $upload, string $source): void
{
if (
$this->debug === false &&
move_uploaded_file($upload['tmp_name'], $source) === false
) {
throw new Exception(
I18n::translate('upload.error.cantMove')
);
}
}

/**
* Upload the files and call closure for each file
*
Expand Down Expand Up @@ -185,11 +167,7 @@ public function process(Closure $callback): array
}

$filename = static::filename($upload);
$source = dirname($upload['tmp_name']);
$source = $source . '/' . uniqid() . '.' . $filename;

// move upload file to tmp location
$this->move($upload, $source);
$source = $this->source($upload['tmp_name'], $filename);

// if the file is uploaded in chunks…
if ($this->api->requestHeaders('Upload-Length')) {
Expand Down Expand Up @@ -314,6 +292,27 @@ public static function response(
];
}

/**
* Move the tmp file to a location including the extension,
* for better mime detection and return updated source path
*
* @codeCoverageIgnore
*/
public function source(string $source, string $filename): string
{
if ($this->debug === true) {
return $source;
}

$target = dirname($source) . '/' . uniqid() . '.' . $filename;

if (move_uploaded_file($source, $target)) {
return $target;
}

throw new Exception(I18n::translate('upload.error.cantMove'));
}

/**
* Returns root of directory used for
* temporarily storing (incomplete) uploads
Expand Down

0 comments on commit 9894d8a

Please sign in to comment.