<commit_before><?php

namespace Unisharp\FileApi;

use League\Flysystem\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileApi
{
    protected $basepath;

    public function __construct($basepath = '/')
    {
        if (mb_substr($basepath, -1, 1, 'utf8') != '/') {
            $basepath .= '/';
        }

        if (mb_substr($basepath, 0, 1, 'utf8') == '/') {
            $basepath = mb_substr($basepath, 1, null, 'utf8');
        }

        $this->basepath = $basepath;
    }

    public function save(UploadedFile $upload_file, $cus_name = null)
    {
        $file = $this->moveFile($upload_file, $cus_name);
        return $file;
    }

    private function moveFile($upload_file, $cus_name)
    {
        $suffix = $upload_file->getClientOriginalExtension();
        $filename = uniqid() . '.' . $suffix;
        if (!empty($cus_name)) {
            $filename = $cus_name . '.' .$suffix;
        }
        \Storage::put(
            $this->basepath . $filename,
            file_get_contents($upload_file->getRealPath())
        );

        \File::delete($upload_file->getRealPath());
        return $filename;
    }

    public function getPath($filename)
    {
        if (mb_substr($this->basepath, -1, 1, 'utf8') != '/') {
            $this->basepath .= '/';
        }

        if (preg_match('/^\//', $filename)) {
            return $this->basepath . mb_substr($filename, 1, null, 'utf8');
        }

        return $this->basepath . $filename;
    }

    public function getUrl($filename)<commit_msg>加上jpeg圖片旋轉的處理<commit_after><?php

namespace Unisharp\FileApi;

use League\Flysystem\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileApi
{
    protected $basepath;

    public function __construct($basepath = '/')
    {
        if (mb_substr($basepath, -1, 1, 'utf8') != '/') {
            $basepath .= '/';
        }

        if (mb_substr($basepath, 0, 1, 'utf8') == '/') {
            $basepath = mb_substr($basepath, 1, null, 'utf8');
        }

        $this->basepath = $basepath;
    }

    public function save(UploadedFile $upload_file, $cus_name = null)
    {
        $file = $this->moveFile($upload_file, $cus_name);
        return $file;
    }

    private function moveFile($upload_file, $cus_name)
    {
        $suffix = $upload_file->getClientOriginalExtension();
        $filename = uniqid() . '.' . $suffix;
        if (!empty($cus_name)) {
            $filename = $cus_name . '.' .$suffix;
        }

        switch (\File::mimeType($upload_file)) {
            case 'image/jpeg':
            case 'image/jpg':
                $img = imagecreatefromjpeg($upload_file->getRealPath());
                $exif = read_exif_data($upload_file->getRealPath());
                if (isset($exif['Orientation'])) {
                    switch ($exif['Orientation']) {
                        case 8:
                            $img = imagerotate($img, 90, 0);
                            break;
                        case 3:
                            $img = imagerotate($img, 180, 0);
                            break;
                        case 6:
                            $img = imagerotate($img, -90, 0);
                            break;
                    }
                }

                imagejpeg($img, $upload_file->getRealPath());
        }

        \Storage::put(
            $this->basepath . $filename,
            file_get_contents($upload_file->getRealPath())
        );

        \File::delete($upload_file->getRealPath());
        return $filename;
    }

    public function getPath($filename)
    {
        if (mb_substr($this->basepath, -1, 1, 'utf8') != '/') {
            $this->basepath .= '/';
        }

        if (preg_match('/^\//', $filename)) {
            return $this->basepath . mb_substr($filename, 1, null, 'utf8');
        }

        return $this->basepath . $filename;
    }

    public function getUrl($filename)
{'fraction_non_alphanumeric': 0.15289672544080604, 'fraction_numerical': 0.00982367758186398, 'mean_word_length': 1.7633959638135004, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '1823808', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1181, 'n_words': 212}