Skip to content

Commit

Permalink
Dedupe path checks
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberiaResurrection committed Aug 28, 2022
1 parent 82aca55 commit efa5d30
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,7 @@ public function putStream($stream, string $filePath = null): bool

$destinationPath = $this->getLocalRootPath().'/'.pathinfo($filePath, PATHINFO_DIRNAME).'/';

if (
! FileHelper::isDirectory($destinationPath) &&
! FileHelper::makeDirectory($destinationPath, 0777, true, true) &&
! FileHelper::isDirectory($destinationPath)
) {
if ($this->checkPath($destinationPath)) {
$error = error_get_last();
if (null !== $error) {
trigger_error($error['message'], E_USER_WARNING);
Expand Down Expand Up @@ -512,11 +508,7 @@ public function putFile(string $sourcePath, string $filePath = null): bool
$destinationPath = $this->getLocalRootPath().'/'.pathinfo($filePath, PATHINFO_DIRNAME).'/';
$destinationPath = str_replace('/', DIRECTORY_SEPARATOR, $destinationPath);

if (
! FileHelper::isDirectory($destinationPath) &&
! FileHelper::makeDirectory($destinationPath, 0777, true, true) &&
! FileHelper::isDirectory($destinationPath)
) {
if ($this->checkPath($destinationPath)) {
$lastError = error_get_last();

if (null !== $lastError) {
Expand Down Expand Up @@ -827,4 +819,15 @@ private static function str_base_convert($str, $fromBase = 10, $toBase = 36)

return $s;
}

/**
* @param $destinationPath
* @return bool
*/
protected function checkPath($destinationPath): bool
{
return !FileHelper::isDirectory($destinationPath) &&
!FileHelper::makeDirectory($destinationPath, 0777, true, true) &&
!FileHelper::isDirectory($destinationPath);
}
}

0 comments on commit efa5d30

Please sign in to comment.