Skip to content

Commit

Permalink
[5.2] Refactor all instances of File to use framework (#43362)
Browse files Browse the repository at this point in the history
* Refactor all occurences of File to use framework

* Refactoring some error handling

* Codestyle

* Fix error handling

* Fix FormattedtextLogger.php

* Chaining exceptions
  • Loading branch information
Hackwar authored Aug 26, 2024
1 parent 321b153 commit 9833057
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Joomla\CMS\Event\Installer\BeforeInstallationEvent;
use Joomla\CMS\Event\Installer\BeforeInstallerEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Installer\InstallerHelper;
use Joomla\CMS\Language\Text;
Expand All @@ -23,6 +22,8 @@
use Joomla\CMS\Router\Route;
use Joomla\CMS\Updater\Update;
use Joomla\CMS\Uri\Uri;
use Joomla\Filesystem\Exception\FilesystemException;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -327,7 +328,13 @@ protected function _getPackageFromUpload()
$tmp_src = $userfile['tmp_name'];

// Move uploaded file.
File::upload($tmp_src, $tmp_dest, false, true);
try {
File::upload($tmp_src, $tmp_dest, false, true);
} catch (FilesystemException $exception) {
Factory::getApplication()->enqueueMessage(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 'error');

return false;
}

// Unpack the downloaded package file.
$package = InstallerHelper::unpack($tmp_dest, true);
Expand Down
64 changes: 39 additions & 25 deletions administrator/components/com_joomlaupdate/src/Model/UpdateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Extension\ExtensionHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File as FileCMS;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Http\Http;
use Joomla\CMS\Http\HttpFactory;
Expand All @@ -30,6 +29,7 @@
use Joomla\CMS\User\UserHelper;
use Joomla\CMS\Version;
use Joomla\Database\ParameterType;
use Joomla\Filesystem\Exception\FilesystemException;
use Joomla\Filesystem\File;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
Expand Down Expand Up @@ -508,7 +508,11 @@ protected function downloadPackage($url, $target)

// Make sure the target does not exist.
if (is_file($target)) {
File::delete($target);
try {
File::delete($target);
} catch (FilesystemException $exception) {
return false;
}
}

// Download the package
Expand All @@ -526,9 +530,9 @@ protected function downloadPackage($url, $target)
$body = $result->body;

// Write the file to disk
$result = File::write($target, $body);

if (!$result) {
try {
File::write($target, $body);
} catch (FilesystemException $exception) {
return false;
}

Expand Down Expand Up @@ -611,14 +615,18 @@ public function createUpdateFile($basename = null): bool
$configpath = JPATH_COMPONENT_ADMINISTRATOR . '/update.php';

if (is_file($configpath)) {
File::delete($configpath);
try {
File::delete($configpath);
} catch (FilesystemException $exception) {
return false;
}
}

// Write new file. First try with File.
$result = File::write($configpath, $data);

// In case File failed but direct access could help.
if (!$result) {
try {
$result = File::write($configpath, $data);
} catch (FilesystemException $exception) {
// In case File failed but direct access could help.
$fp = @fopen($configpath, 'wt');

if ($fp !== false) {
Expand Down Expand Up @@ -889,18 +897,21 @@ public function cleanUp()

$file = $app->getUserState('com_joomlaupdate.file', null);

if (is_file($tempdir . '/' . $file)) {
File::delete($tempdir . '/' . $file);
}
try {
if (is_file($tempdir . '/' . $file)) {
File::delete($tempdir . '/' . $file);
}

// Remove the update.php file used in Joomla 4.0.3 and later.
if (is_file(JPATH_COMPONENT_ADMINISTRATOR . '/update.php')) {
File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/update.php');
}
// Remove the update.php file used in Joomla 4.0.3 and later.
if (is_file(JPATH_COMPONENT_ADMINISTRATOR . '/update.php')) {
File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/update.php');
}

// Remove joomla.xml from the site's root.
if (is_file(JPATH_ROOT . '/joomla.xml')) {
File::delete(JPATH_ROOT . '/joomla.xml');
// Remove joomla.xml from the site's root.
if (is_file(JPATH_ROOT . '/joomla.xml')) {
File::delete(JPATH_ROOT . '/joomla.xml');
}
} catch (FilesystemException $exception) {
}

// Unset the update filename from the session.
Expand Down Expand Up @@ -983,10 +994,10 @@ public function upload()
$tmp_src = $userfile['tmp_name'];

// Move uploaded file.
$result = FileCMS::upload($tmp_src, $tmp_dest, false, true);

if (!$result) {
throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500);
try {
File::upload($tmp_src, $tmp_dest, false);
} catch (FilesystemException $exception) {
throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500, $exception);
}

Factory::getApplication()->setUserState('com_joomlaupdate.temp_file', $tmp_dest);
Expand Down Expand Up @@ -1061,7 +1072,10 @@ public function removePackageFiles()

foreach ($files as $file) {
if ($file !== null && is_file($file)) {
File::delete($file);
try {
File::delete($file);
} catch (FilesystemException $exception) {
}
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions libraries/src/Log/Logger/FormattedtextLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace Joomla\CMS\Log\Logger;

use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Log\LogEntry;
use Joomla\CMS\Log\Logger;
use Joomla\CMS\Version;
use Joomla\Filesystem\Exception\FilesystemException;
use Joomla\Filesystem\File;
use Joomla\Utilities\IpHelper;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -136,8 +137,10 @@ public function __destruct()
// Format all lines and write to file.
$lines = array_map([$this, 'formatLine'], $this->deferredEntries);

if (!File::append($this->path, implode("\n", $lines) . "\n")) {
throw new \RuntimeException('Cannot write to log file.');
try {
File::write($this->path, implode("\n", $lines) . "\n", false, true);
} catch (FilesystemException $exception) {
throw new \RuntimeException('Cannot write to log file.', 500, $exception);
}
}

Expand Down Expand Up @@ -165,8 +168,10 @@ public function addEntry(LogEntry $entry)
$line = $this->formatLine($entry);
$line .= "\n";

if (!File::append($this->path, $line)) {
throw new \RuntimeException('Cannot write to log file.');
try {
File::write($this->path, $line, false, true);
} catch (FilesystemException $exception) {
throw new \RuntimeException('Cannot write to log file.', 500, $exception);
}
}
}
Expand Down Expand Up @@ -269,8 +274,10 @@ protected function initFile()
// Build the log file header.
$head = $this->generateFileHeader();

if (!File::write($this->path, $head)) {
throw new \RuntimeException('Cannot write to log file.');
try {
File::write($this->path, $head);
} catch (FilesystemException $exception) {
throw new \RuntimeException('Cannot write to log file.', 500, $exception);
}
}

Expand Down

0 comments on commit 9833057

Please sign in to comment.