<commit_before><?php

namespace Metrique\Plonk\Repositories;

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Intervention\Image\Exception\NotReadableException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Metrique\Plonk\Eloquent\PlonkAsset;
use Metrique\Plonk\Eloquent\PlonkVariation;
use Metrique\Plonk\Exceptions\PlonkException;
use Metrique\Plonk\Support\PlonkOrientation;
use Metrique\Plonk\Support\PlonkMime;
use Metrique\Plonk\Repositories\PlonkStoreInterface;

// use Symfony\Component\HttpFoundation\File\UploadedFile;

class PlonkStore implements PlonkStoreInterface
{
    /**
     * Uploaded file
     *
     * @var Symfony\Component\HttpFoundation\File\UploadedFile
     */
    protected $file;

    /**
     * Intervention Image
     *
     * @var Intervention\Image
     */
    protected $image;

    /**
    * Intervention Image Manager
    *
    * @var Intervention\Image\ImageManager
    */
    protected $imageManager;

    /**
     * The name of the file input field.
     *
     * @var string
     */
    protected $name = 'file';

    public function validates()
    {
        if (request()->has('data')) {
            $this->makeFileFromData();
        }
        
        return $this->validateFile() & $this->validateImage();
    }

    protected function validateFile()
    {
        if (!isset($this->file)) {
            $this->file = request()->file($this->name);
        }
        
        $validMime = collect(config('plonk.mime'))->contains($this->file->getMimeType());

        $validFile = $this->file->isValid();
        return $validMime & $validFile;
    }
    
    protected function validateImage()
    {
        try {
            $this->imageManager = new ImageManager();
            $this->image = $this->imageManager->make($this->file);
        } catch (NotReadableException $e) {
            return false;
        }

        return true;
    }
    
    public function makeFileFromData()
    {
        $fileContent = base64_decode(
            preg_replace(
                '#^data:image/[^;]+;base64,#',
                '',
                request()->input('data')
            )
        );

        if (!$fileContent) {
            return false;
        }
        
        $file = tempnam(sys_get_temp_dir(), 'Plonk');
        $fileHandler = fopen($file, 'w');
        fwrite($fileHandler, $fileContent);

        $this->file = new UploadedFile(
            $file,
            basename($file),
            finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file),
            filesize($file),
            null,
            true
        );

        request()->files->replace([
            'file' => $this->file
        ]);
        
        fclose($fileHandler);
        unset($file);
    }

    public function store()
    {
        $this->hash = null;
        $this->orientation = null;
        
        if (!$this->validates()) {
            return false;
        }
        
        $images = $this->resizeImages();
        $this->persist($images);
    }
    
    public function storeCli($path, $title, $alt)
    {
        // $this->imageManager = new ImageManager();

        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        
        $uploadedFile = new UploadedFile(
            $path,
            basename($path),
            finfo_file($finfo, $path),
            filesize($path),
            null,
            true
        );
        
        $this->file = $uploadedFile;
        
        request()->merge([
            'title' => $title,<commit_msg>Return hash<commit_after><?php

namespace Metrique\Plonk\Repositories;

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Intervention\Image\Exception\NotReadableException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Metrique\Plonk\Eloquent\PlonkAsset;
use Metrique\Plonk\Eloquent\PlonkVariation;
use Metrique\Plonk\Exceptions\PlonkException;
use Metrique\Plonk\Support\PlonkOrientation;
use Metrique\Plonk\Support\PlonkMime;
use Metrique\Plonk\Repositories\PlonkStoreInterface;

// use Symfony\Component\HttpFoundation\File\UploadedFile;

class PlonkStore implements PlonkStoreInterface
{
    /**
     * Uploaded file
     *
     * @var Symfony\Component\HttpFoundation\File\UploadedFile
     */
    protected $file;

    /**
     * Intervention Image
     *
     * @var Intervention\Image
     */
    protected $image;

    /**
    * Intervention Image Manager
    *
    * @var Intervention\Image\ImageManager
    */
    protected $imageManager;

    /**
     * The name of the file input field.
     *
     * @var string
     */
    protected $name = 'file';

    public function validates()
    {
        if (request()->has('data')) {
            $this->makeFileFromData();
        }
        
        return $this->validateFile() & $this->validateImage();
    }

    protected function validateFile()
    {
        if (!isset($this->file)) {
            $this->file = request()->file($this->name);
        }
        
        $validMime = collect(config('plonk.mime'))->contains($this->file->getMimeType());

        $validFile = $this->file->isValid();
        return $validMime & $validFile;
    }
    
    protected function validateImage()
    {
        try {
            $this->imageManager = new ImageManager();
            $this->image = $this->imageManager->make($this->file);
        } catch (NotReadableException $e) {
            return false;
        }

        return true;
    }
    
    public function makeFileFromData()
    {
        $fileContent = base64_decode(
            preg_replace(
                '#^data:image/[^;]+;base64,#',
                '',
                request()->input('data')
            )
        );

        if (!$fileContent) {
            return false;
        }
        
        $file = tempnam(sys_get_temp_dir(), 'Plonk');
        $fileHandler = fopen($file, 'w');
        fwrite($fileHandler, $fileContent);

        $this->file = new UploadedFile(
            $file,
            basename($file),
            finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file),
            filesize($file),
            null,
            true
        );

        request()->files->replace([
            'file' => $this->file
        ]);
        
        fclose($fileHandler);
        unset($file);
    }

    public function store()
    {
        $this->hash = null;
        $this->orientation = null;
        
        if (!$this->validates()) {
            return false;
        }
        
        $images = $this->resizeImages();
        $this->persist($images);
        
        return $this->getHash();
    }
    
    public function storeCli($path, $title, $alt)
    {
        // $this->imageManager = new ImageManager();

        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        
        $uploadedFile = new UploadedFile(
            $path,
            basename($path),
            finfo_file($finfo, $path),
            filesize($path),
            null,
            true
        );
        
        $this->file = $uploadedFile;
        
        request()->merge([
            'title' => $title,
{'fraction_non_alphanumeric': 0.1307819383259912, 'fraction_numerical': 0.0011013215859030838, 'mean_word_length': 1.8760886777513857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, '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': '4489270', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2146, 'n_words': 360}