<?php

namespace App\Repositories\Core;

use Illuminate\Support\Facades\Storage;

use SoftCreatR\MimeDetector\MimeDetector;
use SoftCreatR\MimeDetector\MimeDetectorException;

class File
{
    public static function checkDirOrCreate($dir)
    {
        return self::checkDir($dir) ? true : self::createDir($dir);
    }

    public static function checkDir($dir)
    {
        // File::isDirectory($path)
        return is_dir($dir);
    }

    public static function checkFile($file)
    {
        return file_exists($file);
    }

    public static function createDir($dir)
    {
        Storage::makeDirectory($dir);
        return true;
    }

    public static function deleteDir($dir)
    {
        Storage::deleteDirectory($dir);
        return true;
    }

    public static function createFile($file, $content)
    {
        Storage::put($file, $content);
    }

    public static function getFile($file)
    {
        return Storage::get($file);
    }

    public static function getFilesize($file)
    {
        return Storage::size($file);
    }

    public static function getUrlFile($file)
    {
        return Storage::url($file);
    }

    public static function download($file, $name = false, $headers = false)
    {
        return Storage::download($file, $name, $headers);
    }

    public static function getFileType($file)
    {
        try {
            return (new MimeDetector())->setFile($file)->getFileType();
        } catch (MimeDetectorException $e) {
            die('An error occured while trying to load the given file.');
        }
    }
}
