<commit_before><?php

/**
 * Core
 *
 * The core of any web-project by Lowie Huyghe
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */

namespace Core\Models;
use Carbon\Carbon;
use Core\Bases\Models\BaseModel;
use Core\Database\Relations\BelongsToEnum;
use Core\Http\Client\Input;
use Core\Io\Validation\Validation;
use Faker\Generator;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * The model for files
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 *
 * @property int $id Database table-id
 * @property string $name
 * @property string $md5
 * @property string $sha1
 * @property string $original_name
 * @property string $mime
 * @property FileType $type
 * @property int $size
 * @property Carbon $created_at
 * @property Carbon $updated_at
 */
class FileEntry extends BaseModel
{
	/**
	 * The table to store the data
	 * @var string
	 */
	protected $table = 'file_entries';

	/**
	 * Timestamps in the table
	 * @var bool|array
	 */
	public $timestamps = true;

	/**
	 * The attributes that should be mutated to dates.
	 * @var array
	 */
	protected $dates = ['created_at', 'updated_at'];

	/**
	 * The resource from the file
	 * @var resource
	 */
	private $resource;

	/**
	 * The path of the uploaded file
	 * @var UploadedFile
	 */
	private $uploadedFile;

	/**
	 * The name of the last changed resource of the file (to save)
	 * @var string
	 */
	private $lastTouched;

	/**
	 * The class responsible for the type
	 * @var string
	 */
	protected $typeClass = FileType::class;

	/**
	 * Type belonging to this FileEntry
	 * @return BelongsToEnum
	 * @throws \Exception
	 */
	public function type()
	{
		if (!isset($this->typeClass))
		{
			throw new \Exception('The class representing the Types not set');
		}
		return $this->belongsToEnum($this->typeClass);
	}

	/**
	 * Save the file-entry and upload the file
	 * @param array $options
	 * @return bool
	 */
	public function save(array $options = Array())
	{
		//Set the name for the file when saving
		if (!$this->name)
		{
			$this->name = self::getNewName($this->getExtension());
		}

		//First save the file to the storage
		switch($this->lastTouched)
		{
			case 'resource':
				ob_start();
				switch($this->mime)
				{
					case 'image/png':
						imagepng($this->resource);
						break;
					case 'image/pjpeg':
					case 'image/jpeg':
						imagejpeg($this->resource);
						break;
					case 'image/gif':
						imagegif($this->resource);
						break;
					default:
						ob_end_clean();
						return false;
				}
				$contents =  ob_get_contents();
				ob_end_clean();
				$uploadSuccess = self::getDisk()->put($this->name, $contents);
				break;
			case 'uploadedFile':
				$uploadSuccess = self::getDisk()->put($this->name, app('files')->get($this->uploadedFile));
				break;
			default:
				$uploadSuccess = true;
				break;
		}

		//If success save the entry in the database
		if ($uploadSuccess)
		{
			return parent::save($options);
		}

		return false;
	}

	/**
	 * Save the fil-entry as a new entry
	 * @param array $options
	 * @return bool
	 */
	public function saveAs(array $options = Array())
	{
		$this->id = null;
		$this->name = null;<commit_msg>#62: Some props of the file can change when saved. Update them when saving the file.<commit_after><?php

/**
 * Core
 *
 * The core of any web-project by <NAME>
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */

namespace Core\Models;
use Carbon\Carbon;
use Core\Bases\Models\BaseModel;
use Core\Database\Relations\BelongsToEnum;
use Core\Http\Client\Input;
use Core\Io\Validation\Validation;
use Faker\Generator;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * The model for files
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 *
 * @property int $id Database table-id
 * @property string $name
 * @property string $md5
 * @property string $sha1
 * @property string $original_name
 * @property string $mime
 * @property FileType $type
 * @property int $size
 * @property Carbon $created_at
 * @property Carbon $updated_at
 */
class FileEntry extends BaseModel
{
	/**
	 * The table to store the data
	 * @var string
	 */
	protected $table = 'file_entries';

	/**
	 * Timestamps in the table
	 * @var bool|array
	 */
	public $timestamps = true;

	/**
	 * The attributes that should be mutated to dates.
	 * @var array
	 */
	protected $dates = ['created_at', 'updated_at'];

	/**
	 * The resource from the file
	 * @var resource
	 */
	private $resource;

	/**
	 * The path of the uploaded file
	 * @var UploadedFile
	 */
	private $uploadedFile;

	/**
	 * The name of the last changed resource of the file (to save)
	 * @var string
	 */
	private $lastTouched;

	/**
	 * The class responsible for the type
	 * @var string
	 */
	protected $typeClass = FileType::class;

	/**
	 * Type belonging to this FileEntry
	 * @return BelongsToEnum
	 * @throws \Exception
	 */
	public function type()
	{
		if (!isset($this->typeClass))
		{
			throw new \Exception('The class representing the Types not set');
		}
		return $this->belongsToEnum($this->typeClass);
	}

	/**
	 * Save the file-entry and upload the file
	 * @param array $options
	 * @return bool
	 */
	public function save(array $options = Array())
	{
		//Set the name for the file when saving
		if (!$this->name)
		{
			$this->name = self::getNewName($this->getExtension());
		}

		//First save the file to the storage
		switch($this->lastTouched)
		{
			case 'resource':
				ob_start();
				switch($this->mime)
				{
					case 'image/png':
						imagepng($this->resource);
						break;
					case 'image/pjpeg':
					case 'image/jpeg':
						imagejpeg($this->resource);
						break;
					case 'image/gif':
						imagegif($this->resource);
						break;
					default:
						ob_end_clean();
						return false;
				}
				$contents =  ob_get_contents();
				ob_end_clean();
				$uploadSuccess = self::getDisk()->put($this->name, $contents);
				break;
			case 'uploadedFile':
				$uploadSuccess = self::getDisk()->put($this->name, app('files')->get($this->uploadedFile));
				break;
			default:
				$uploadSuccess = true;
				break;
		}

		//If success save the entry in the database
		if ($uploadSuccess)
		{
			//Update some props of the file when changed
			if ($this->lastTouched)
			{
				$path = $this->getPath();
				$size = self::getDisk()->getSize($this->name);
				$md5 = md5_file($path);
				$sha1 = sha1_file($path);
			}

			return parent::save($options);
		}

		return false;
	}

	/**
	 * Save the fil-entry as a new entry
	 * @param array $options
	 * @return bool
	 */
	public function saveAs(array $options = Array())
	{
		$this->id = null;
		$this->name = null;
{'fraction_non_alphanumeric': 0.13555435662260587, 'fraction_numerical': 0.004585918532506069, 'mean_word_length': 3.1657303370786516, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 62, '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': '3283913', 'n_tokens_mistral': 2714, 'n_tokens_neox': 2295, 'n_words': 715}