4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-14 13:16:37 +00:00

Use standardized dir creation method to avoid warnings.

This commit is contained in:
Buster "Silver Eagle" Neece 2022-04-10 21:18:15 -05:00
parent e2ee3e0c43
commit 936fba4368
No known key found for this signature in database
GPG Key ID: 9FC8B9E008872109
6 changed files with 36 additions and 21 deletions

View File

@ -6,7 +6,6 @@ namespace App\Assets;
use App\Utilities\File;
use Intervention\Image\Image;
use RuntimeException;
class BrowserIconCustomAsset extends AbstractCustomAsset
{
@ -42,9 +41,7 @@ class BrowserIconCustomAsset extends AbstractCustomAsset
public function upload(Image $image): void
{
$uploadsDir = $this->environment->getUploadsDirectory() . '/browser_icon';
if (!mkdir($uploadsDir) && !is_dir($uploadsDir)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $uploadsDir));
}
File::ensureDirectoryExists($uploadsDir);
$newImage = clone $image;
$newImage->resize(256, 256);

View File

@ -95,8 +95,10 @@ class BackupCommand extends CommandAbstract
$io->section(__('Creating temporary directories...'));
$tmp_dir_mariadb = '/tmp/azuracast_backup_mariadb';
if (!mkdir($tmp_dir_mariadb) && !is_dir($tmp_dir_mariadb)) {
$io->error(__('Directory "%s" was not created', $tmp_dir_mariadb));
try {
Utilities\File::ensureDirectoryExists($tmp_dir_mariadb);
} catch (\Throwable $e) {
$io->error($e->getMessage());
return 1;
}

View File

@ -23,7 +23,6 @@ use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\Visibility;
use OpenApi\Attributes as OA;
use Psr\Http\Message\UriInterface;
use RuntimeException;
use Stringable;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Serializer\Annotation as Serializer;
@ -696,11 +695,8 @@ class Station implements Stringable, IdentifiableEntityInterface
$visibility = (new PortableVisibilityConverter(
defaultForDirectories: Visibility::PUBLIC
))->defaultForDirectories();
if (!mkdir($dirname, $visibility, true) && !is_dir($dirname)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $dirname));
}
clearstatcache(false, $dirname);
File::ensureDirectoryExists($dirname, $visibility);
}
public function getRadioPlaylistsDir(): string

View File

@ -31,6 +31,7 @@ use App\Exception;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Service\Flow\UploadedFile;
use App\Utilities\File;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;
@ -49,9 +50,7 @@ class Flow
): UploadedFile|ResponseInterface {
if (null === $tempDir) {
$tempDir = sys_get_temp_dir() . '/uploads';
if (!mkdir($tempDir) && !is_dir($tempDir)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $tempDir));
}
File::ensureDirectoryExists($tempDir);
}
$params = $request->getParams();
@ -107,9 +106,7 @@ class Flow
}
// the file is stored in a temporary directory
if (!is_dir($chunkBaseDir) && !mkdir($chunkBaseDir, 0777, true) && !is_dir($chunkBaseDir)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $chunkBaseDir));
}
File::ensureDirectoryExists($chunkBaseDir);
if ($file->getSize() !== $currentChunkSize) {
throw new RuntimeException(

View File

@ -104,6 +104,31 @@ class File
return $fullPath;
}
public static function ensureDirectoryExists(string $dirname, int $visibility = 0777): void
{
if (is_dir($dirname)) {
return;
}
error_clear_last();
if (!@mkdir($dirname, $visibility, true)) {
$mkdirError = error_get_last();
}
clearstatcache(true, $dirname);
if (!is_dir($dirname)) {
throw new \RuntimeException(
sprintf(
'Unable to create directory "%s": %s',
$dirname,
$mkdirError['message'] ?? ''
)
);
}
}
/**
* Recursively remove a directory and its contents.
*

View File

@ -7,10 +7,10 @@ namespace App\Webhook;
use App\Entity;
use App\Environment;
use App\Service\NChan;
use App\Utilities\File;
use GuzzleHttp\Client;
use Monolog\Logger;
use Psr\SimpleCache\CacheInterface;
use RuntimeException;
use const JSON_PRETTY_PRINT;
@ -58,9 +58,7 @@ class LocalWebhookHandler
$this->logger->debug('Writing static nowplaying text file...');
$static_np_dir = Environment::getInstance()->getTempDirectory() . '/nowplaying';
if (!is_dir($static_np_dir) && !mkdir($static_np_dir) && !is_dir($static_np_dir)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $static_np_dir));
}
File::ensureDirectoryExists($static_np_dir);
$static_path = $static_np_dir . '/' . $station->getShortName() . '.json';
file_put_contents(