<commit_before><?php namespace Matriphe\Imageupload;

use Config;
use File;
use Log;
use Illuminate\Support\Str;

class Imageupload {

  protected $imagine;
  protected $library;

  public $results = [];

  public function __construct()
  {
    if (!$this->imagine)
    {
      $this->library = Config::get('imageupload.library','gd');
      $this->quality = Config::get('imageupload.quality', 90);
      $this->uploadpath = Config::get('imageupload.path', public_path().'/uploads/images');
      $this->newfilename = Config::get('imageupload.newfilename', 'original');
      $this->dimensions = Config::get('imageupload.dimensions');
      $this->suffix = Config::get('imageupload.suffix',true);
      $this->exif = Config::get('imageupload.exif', false);

      // Now create the instance
      if     ($this->library == 'imagick') $this->imagine = new \Imagine\Imagick\Imagine();
      elseif ($this->library == 'gmagick') $this->imagine = new \Imagine\Gmagick\Imagine();
      elseif ($this->library == 'gd')      $this->imagine = new \Imagine\Gd\Imagine();
      else                                 $this->imagine = new \Imagine\Gd\Imagine();
    }
  }

  private function checkPathIsOk($path,$dir=null)
  {
    $path = rtrim($path,'/') . ($dir ? '/'.trim($dir,'/') : '');

    if (File::isDirectory($path) && File::isWritable($path))
    {
      return true;
    }
    else
    {
      try
      {
        @File::makeDirectory($path, 0777, true);
        return true;
      }
      catch (\Exception $e)
      {
        Log::error('Imageupload: ' . $e->getMessage());
        $this->results['error'] = $e->getMessage();
        return false;
      }
    }
  }

  public function upload($filesource, $newfilename=null, $dir=null)
  {
    $isPathOk = $this->checkPathIsOk($this->uploadpath,$dir);

    if ($isPathOk)
    {
      if ($filesource)
      {
        $this->results['path'] = rtrim($this->uploadpath,'/') . ($dir ? '/'.trim($dir,'/') : '');
        $this->results['dir'] = str_replace(public_path().'/', '', $this->results['path']);
        $this->results['original_filename'] = $filesource->getClientOriginalName();
        $this->results['original_filepath'] = $filesource->getRealPath();
        $this->results['original_extension'] = $filesource->getClientOriginalExtension();
        $this->results['original_filesize'] = $filesource->getSize();
        $this->results['original_mime'] = $filesource->getMimeType();
        $this->results['exif'] = $this->getExif($filesource->getRealPath());
        
        
        switch ($this->newfilename)
        {
          case 'hash':
            $this->results['filename'] = md5($this->results['original_filename'].'.'.$this->results['original_extension'].strtotime('now')).'.'.$this->results['original_extension'];
            break;
          case 'random':
            $this->results['filename'] = Str::random().'.'.$this->results['original_extension'];
          break;
          case 'timestamp':
            $this->results['filename'] = strtotime('now').'.'.$this->results['original_extension'];
            break;
          case 'custom':
            $this->results['filename'] = (!empty($newfilename) ? $newfilename.'.'.$this->results['original_extension'] : $this->results['original_filename'].'.'.$this->results['original_extension']);
            break;
          default:
            $this->results['filename']= $this->results['original_filename'];
        }

        $uploaded = $filesource->move($this->results['path'], $this->results['filename']);<commit_msg>Fix #6 by removing extension for custom filename fallback<commit_after><?php namespace Matriphe\Imageupload;

use Config;
use File;
use Log;
use Illuminate\Support\Str;

class Imageupload {

  protected $imagine;
  protected $library;

  public $results = [];

  public function __construct()
  {
    if (!$this->imagine)
    {
      $this->library = Config::get('imageupload.library','gd');
      $this->quality = Config::get('imageupload.quality', 90);
      $this->uploadpath = Config::get('imageupload.path', public_path().'/uploads/images');
      $this->newfilename = Config::get('imageupload.newfilename', 'original');
      $this->dimensions = Config::get('imageupload.dimensions');
      $this->suffix = Config::get('imageupload.suffix',true);
      $this->exif = Config::get('imageupload.exif', false);

      // Now create the instance
      if     ($this->library == 'imagick') $this->imagine = new \Imagine\Imagick\Imagine();
      elseif ($this->library == 'gmagick') $this->imagine = new \Imagine\Gmagick\Imagine();
      elseif ($this->library == 'gd')      $this->imagine = new \Imagine\Gd\Imagine();
      else                                 $this->imagine = new \Imagine\Gd\Imagine();
    }
  }

  private function checkPathIsOk($path,$dir=null)
  {
    $path = rtrim($path,'/') . ($dir ? '/'.trim($dir,'/') : '');

    if (File::isDirectory($path) && File::isWritable($path))
    {
      return true;
    }
    else
    {
      try
      {
        @File::makeDirectory($path, 0777, true);
        return true;
      }
      catch (\Exception $e)
      {
        Log::error('Imageupload: ' . $e->getMessage());
        $this->results['error'] = $e->getMessage();
        return false;
      }
    }
  }

  public function upload($filesource, $newfilename=null, $dir=null)
  {
    $isPathOk = $this->checkPathIsOk($this->uploadpath,$dir);

    if ($isPathOk)
    {
      if ($filesource)
      {
        $this->results['path'] = rtrim($this->uploadpath,'/') . ($dir ? '/'.trim($dir,'/') : '');
        $this->results['dir'] = str_replace(public_path().'/', '', $this->results['path']);
        $this->results['original_filename'] = $filesource->getClientOriginalName();
        $this->results['original_filepath'] = $filesource->getRealPath();
        $this->results['original_extension'] = $filesource->getClientOriginalExtension();
        $this->results['original_filesize'] = $filesource->getSize();
        $this->results['original_mime'] = $filesource->getMimeType();
        $this->results['exif'] = $this->getExif($filesource->getRealPath());
        
        
        switch ($this->newfilename)
        {
          case 'hash':
            $this->results['filename'] = md5($this->results['original_filename'].'.'.$this->results['original_extension'].strtotime('now')).'.'.$this->results['original_extension'];
            break;
          case 'random':
            $this->results['filename'] = Str::random().'.'.$this->results['original_extension'];
          break;
          case 'timestamp':
            $this->results['filename'] = strtotime('now').'.'.$this->results['original_extension'];
            break;
          case 'custom':
            $this->results['filename'] = (!empty($newfilename) ? $newfilename.'.'.$this->results['original_extension'] : $this->results['original_filename']);
            break;
          default:
            $this->results['filename']= $this->results['original_filename'];
        }

        $uploaded = $filesource->move($this->results['path'], $this->results['filename']);
{'fraction_non_alphanumeric': 0.19661256175017644, 'fraction_numerical': 0.002117148906139732, 'mean_word_length': 2.930116472545757, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7646725', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2073, 'n_words': 319}