Skip to content

Commit

Permalink
task: introduce file utility to create folders (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott authored Nov 7, 2023
1 parent fd57aaf commit cef8357
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
18 changes: 3 additions & 15 deletions admin/upload/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once '../../lib/boot.php';

use Photobooth\Service\LanguageService;
use Photobooth\Utility\FileUtility;
use Photobooth\Utility\ImageUtility;
use Photobooth\Utility\PathUtility;

Expand Down Expand Up @@ -30,22 +31,9 @@
$btnClass = 'w-full h-12 rounded-full bg-brand-1 text-white flex items-center justify-center relative ml-auto border-2 border-solid border-brand-1 hover:bg-white hover:text-brand-1 transition font-bold px-4';

if (isset($_POST['submit'])) {

$folderName = $_POST['folder_name'];
$parentDirectory = $config['foldersAbs']['private'] . DIRECTORY_SEPARATOR . 'images';
// Check if the parent directory exists
if (!is_dir($parentDirectory)) {
mkdir($parentDirectory, 0755, true);
} else {
chmod($parentDirectory, 0755);
}

// Check if the folder already exists
$folderPath = $parentDirectory . '/' . $folderName;
if (!is_dir($folderPath)) {
// Create the folder if it doesn't exist
mkdir($folderPath, 0755, true);
}
$folderPath = $config['foldersAbs']['private'] . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folderName;
FileUtility::createDirectory($folderPath);

if (is_writable($folderPath)) {
// Process uploaded images
Expand Down
14 changes: 5 additions & 9 deletions lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Photobooth\Photobooth;
use Photobooth\Helper;
use Photobooth\Utility\ArrayUtility;
use Photobooth\Utility\FileUtility;
use Photobooth\Utility\PathUtility;

$photobooth = new Photobooth();
Expand Down Expand Up @@ -148,19 +149,14 @@
$path = PathUtility::getAbsolutePath($config['folders']['data'] . DIRECTORY_SEPARATOR . $folder);
$config['foldersPublic'][$key] = PathUtility::getPublicPath($path);
}

if (!file_exists($path)) {
if (!mkdir($path, 0755, true)) {
die("Abort. Could not create $folder.");
}
} elseif (!is_writable($path)) {
die("Abort. The folder $folder is not writable.");
}

FileUtility::createDirectory($path);
$config['foldersAbs'][$key] = PathUtility::getAbsolutePath($path);
}

FileUtility::createDirectory(PathUtility::getAbsolutePath('var/log'));
FileUtility::createDirectory(PathUtility::getAbsolutePath('var/run'));
$config['foldersAbs']['var'] = PathUtility::getAbsolutePath('var');

$config['foldersPublic']['api'] = PathUtility::getPublicPath('api');
$config['foldersPublic']['chroma'] = PathUtility::getPublicPath('chroma');

Expand Down
21 changes: 21 additions & 0 deletions src/Utility/FileUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Photobooth\Utility;

class FileUtility
{
public const DIRECTORY_PERMISSIONS = 0755;

public static function createDirectory(string $directory): void
{
if (!file_exists($directory) && !is_dir($directory)) {
if (!mkdir($directory, self::DIRECTORY_PERMISSIONS, true)) {
throw new \Exception('Failed to create directory: ' . $directory);
}
} elseif (!is_writable($directory)) {
if (!chmod($directory, self::DIRECTORY_PERMISSIONS)) {
throw new \Exception('Failed to change permissions of directory: ' . $directory);
}
}
}
}

0 comments on commit cef8357

Please sign in to comment.