<commit_before><?php

namespace Transformatika\Utility;

class File
{
    private static $instance;

    private $rootDir;

    public function __construct($rootDir)
    {
        $this->rootDir = $rootDir;
    }

    public static function init($rootDir = '')
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($rootDir);
        }
        return self::$instance;
    }

    public function createDirectory($path, $includeFileName = false)
    {
        $dirpath = str_replace('/', DIRECTORY_SEPARATOR, rawurldecode($path));
        $dirpath = str_replace($this->rootDir, '', $dirpath);
        $dir = explode(DIRECTORY_SEPARATOR, $dirpath);
        // Array direktori
        $total = (int)count($dir);
        // Total array
        if ($includeFileName == true) {
            unset($dir[($total - 1)]);
            // Unset array terakhir (filename)
        }
        $currentDirectory = $this->rootDir;
        $error = 0;
        foreach ($dir as $key) {// Membuat direktori
            if ($key != '' && !is_dir($currentDirectory . $key)) {
                $oldumask = umask(0);
                $m = mkdir($currentDirectory . $key, 0777);
                if (!$m) {
                    $error++;
                }
                umask($oldumask);
            }
            $currentDirectory .= $key . DIRECTORY_SEPARATOR;
        }
        if ($error > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function removeDirectory($dirPath)
    {
        $error = 0;
        if (is_dir($dirPath)) {
            $objects = scandir($dirPath);
            foreach ($objects as $object) {
                if ($object != "." && $object != "..") {
                    if (filetype($dirPath . DIRECTORY_SEPARATOR . $object) == "dir") {
                        $del = self::removeDirectory($dirPath . DIRECTORY_SEPARATOR . $object);
                        if (!$del) {
                            $error++;
                        }
                    } else {
                        $del = unlink($dirPath . DIRECTORY_SEPARATOR . $object);
                        if (!$del) {
                            $error++;
                        }
                    }
                }
            }
            reset($objects);
            $del = rmdir($dirPath);
            if (!$del) {
                $error++;
            }
        } else {
            $del = unlink($dirPath);
            if (!$del) {
                $error++;
            }
        }
        if ($error > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function imageDecode($imageString, $outputFile)
    {
        $this->createDirectory($outputFile, true);
        $splitImageString = explode('base64', $imageString);
        $sanitizeOutputFile = str_replace('/', DIRECTORY_SEPARATOR, $outputFile);
        $fullPathOutputFile = $this->rootDir . DIRECTORY_SEPARATOR . $sanitizeOutputFile;
        if (file_put_contents($fullPathOutputFile, base64_decode($splitImageString[1]))) {
            return true;
        } else {
            return false;
        }
    }

    public function imageEncode($inputFile = '', $filetype = 'jpeg')
    {
        if ($inputFile) {
            $inputFile = $this->rootDir . str_replace('/', DS, $inputFile);
            $imgbinary = fread(fopen($inputFile, "r"), filesize($inputFile));
            return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
        }
    }

    public function readFile($filePath, $mimeContentType = '')
    {
        if (file_exists($filePath)) {
            if (empty($mimeContentType)) {
                header('Content-Type: ' . $this->mimeContentType($filePath));
            } else {
                header('Content-Type: ' . $mimeContentType);
            }

            header('Content-Length: ' . filesize($filePath));
            readfile($filePath);
        }
    }

    public function openFile($filePath, $mimeContentType = '')
    {
        $this->readFile($filePath, $mimeContentType);
    }

    /** Get Mime Content Type
    * Original source: http://stackoverflow.com/questions/1263957/why-is-mime-content-type-deprecated-in-php
    */
    public function mimeContentType($file)
    {
        $result = new finfo();

        if (is_resource($result) === true) {
            return $result->file($filename, FILEINFO_MIME_TYPE);
        }

        return false;<commit_msg>fix fileinfo<commit_after><?php

namespace Transformatika\Utility;

class File
{
    private static $instance;

    private $rootDir;

    public function __construct($rootDir)
    {
        $this->rootDir = $rootDir;
    }

    public static function init($rootDir = '')
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($rootDir);
        }
        return self::$instance;
    }

    public function createDirectory($path, $includeFileName = false)
    {
        $dirpath = str_replace('/', DIRECTORY_SEPARATOR, rawurldecode($path));
        $dirpath = str_replace($this->rootDir, '', $dirpath);
        $dir = explode(DIRECTORY_SEPARATOR, $dirpath);
        // Array direktori
        $total = (int)count($dir);
        // Total array
        if ($includeFileName == true) {
            unset($dir[($total - 1)]);
            // Unset array terakhir (filename)
        }
        $currentDirectory = $this->rootDir;
        $error = 0;
        foreach ($dir as $key) {// Membuat direktori
            if ($key != '' && !is_dir($currentDirectory . $key)) {
                $oldumask = umask(0);
                $m = mkdir($currentDirectory . $key, 0777);
                if (!$m) {
                    $error++;
                }
                umask($oldumask);
            }
            $currentDirectory .= $key . DIRECTORY_SEPARATOR;
        }
        if ($error > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function removeDirectory($dirPath)
    {
        $error = 0;
        if (is_dir($dirPath)) {
            $objects = scandir($dirPath);
            foreach ($objects as $object) {
                if ($object != "." && $object != "..") {
                    if (filetype($dirPath . DIRECTORY_SEPARATOR . $object) == "dir") {
                        $del = self::removeDirectory($dirPath . DIRECTORY_SEPARATOR . $object);
                        if (!$del) {
                            $error++;
                        }
                    } else {
                        $del = unlink($dirPath . DIRECTORY_SEPARATOR . $object);
                        if (!$del) {
                            $error++;
                        }
                    }
                }
            }
            reset($objects);
            $del = rmdir($dirPath);
            if (!$del) {
                $error++;
            }
        } else {
            $del = unlink($dirPath);
            if (!$del) {
                $error++;
            }
        }
        if ($error > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function imageDecode($imageString, $outputFile)
    {
        $this->createDirectory($outputFile, true);
        $splitImageString = explode('base64', $imageString);
        $sanitizeOutputFile = str_replace('/', DIRECTORY_SEPARATOR, $outputFile);
        $fullPathOutputFile = $this->rootDir . DIRECTORY_SEPARATOR . $sanitizeOutputFile;
        if (file_put_contents($fullPathOutputFile, base64_decode($splitImageString[1]))) {
            return true;
        } else {
            return false;
        }
    }

    public function imageEncode($inputFile = '', $filetype = 'jpeg')
    {
        if ($inputFile) {
            $inputFile = $this->rootDir . str_replace('/', DS, $inputFile);
            $imgbinary = fread(fopen($inputFile, "r"), filesize($inputFile));
            return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
        }
    }

    public function readFile($filePath, $mimeContentType = '')
    {
        if (file_exists($filePath)) {
            if (empty($mimeContentType)) {
                header('Content-Type: ' . $this->mimeContentType($filePath));
            } else {
                header('Content-Type: ' . $mimeContentType);
            }

            header('Content-Length: ' . filesize($filePath));
            readfile($filePath);
        }
    }

    public function openFile($filePath, $mimeContentType = '')
    {
        $this->readFile($filePath, $mimeContentType);
    }

    /** Get Mime Content Type
    * Original source: http://stackoverflow.com/questions/1263957/why-is-mime-content-type-deprecated-in-php
    */
    public function mimeContentType($file)
    {
        $result = new \finfo();

        if (is_resource($result) === true) {
            return $result->file($filename, FILEINFO_MIME_TYPE);
        }

        return false;
{'fraction_non_alphanumeric': 0.13640419362034353, 'fraction_numerical': 0.005799687709123355, 'mean_word_length': 1.5656652360515022, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '506179', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2466, 'n_words': 472}