<commit_before><?php
/**
 * File utility and database storage.
 * @author gizmore
 * @version 5.0
 * @sinve 3.0
 * 
 * @see GDO_File
 */
class GWF_File extends GDO
{
	###########
	### GDO ###
	###########
	public function gdoColumns()
	{
		return array(
			GDO_AutoInc::make('file_id')->label('id'),
			GDO_String::make('file_name')->notNull(),
			GDO_String::make('file_type')->ascii()->caseS()->notNull()->max(96),
			GDO_Filesize::make('file_size')->notNull(),
			GDO_Int::make('file_width')->unsigned(),
			GDO_Int::make('file_height')->unsigned(),
			GDO_Int::make('file_bitrate')->unsigned(),
			GDO_Duration::make('file_duration'),
		);
	}
	
	public function getID() { return $this->getVar('file_id'); }
	public function getName() { return $this->getVar('file_name'); }
	public function getSize() { return $this->getVar('file_size'); }
	public function getType() { return $this->getVar('file_type'); }
	public function displaySize() { return self::humanFilesize($this->getSize()); }
	
	private $path;
	public function tempPath(string $path=null)
	{
		$this->path = $path;
		return $this;
	}
	
	public function getPath() { return $this->path ? $this->path : $this->getDestPath(); }
	public function getDestPath() { return self::filesDir() . $this->getID(); }
	
	public function renderCell() { return GWF_Template::mainPHP('cell/file.php', ['gdo'=>$this]); }
	
	public function delete()
	{
		@unlink($this->getDestPath());
		return parent::delete();
	}
	
	###############
	### Factory ###
	###############
	public static function filesDir()
	{
		return GWF_PATH . 'dbimg/files/';
	}
	
	/**
	 * @param array $values
	 * @return GWF_File
	 */
	public static function fromForm(array $values)
	{
		return self::blank(array(
			'file_name' => $values['name'],
			'file_size' => $values['size'],
			'file_type' => $values['mime']
		))->tempPath($values['path']);
	}
	
	public function copy()
	{
		$this->insert();
		copy($this->path, $this->getDestPath());
		$this->path = null;
		return $this;
	}
	
	/**
	 * @param string $contents
	 * @return GWF_File
	 */
	public static function fromString(string $name, string $content)
	{
		# Create temp dir
		$tempDir = GWF_PATH . 'temp/file';
		GWF_File::createDir($tempDir);
		# Copy content to temp file
		$tempPath = $tempDir . '/' . md5(md5($name).md5($content));
		file_put_contents($tempPath, $content);
		# Return fresh GWF_File.
		$values = array(
			'name' => $name,
			'size' => strlen($content),
			'mime' => mime_content_type($tempPath),
			'path' => $tempPath,
		);
		return self::fromForm($values);
	}

	############
	### Util ###
	############<commit_msg>Create file dir before copy<commit_after><?php
/**
 * File utility and database storage.
 * @author gizmore
 * @version 5.0
 * @sinve 3.0
 * 
 * @see GDO_File
 */
class GWF_File extends GDO
{
	###########
	### GDO ###
	###########
	public function gdoColumns()
	{
		return array(
			GDO_AutoInc::make('file_id')->label('id'),
			GDO_String::make('file_name')->notNull(),
			GDO_String::make('file_type')->ascii()->caseS()->notNull()->max(96),
			GDO_Filesize::make('file_size')->notNull(),
			GDO_Int::make('file_width')->unsigned(),
			GDO_Int::make('file_height')->unsigned(),
			GDO_Int::make('file_bitrate')->unsigned(),
			GDO_Duration::make('file_duration'),
		);
	}
	
	public function getID() { return $this->getVar('file_id'); }
	public function getName() { return $this->getVar('file_name'); }
	public function getSize() { return $this->getVar('file_size'); }
	public function getType() { return $this->getVar('file_type'); }
	public function displaySize() { return self::humanFilesize($this->getSize()); }
	
	private $path;
	public function tempPath(string $path=null)
	{
		$this->path = $path;
		return $this;
	}
	
	public function getPath() { return $this->path ? $this->path : $this->getDestPath(); }
	public function getDestPath() { return self::filesDir() . $this->getID(); }
	
	public function renderCell() { return GWF_Template::mainPHP('cell/file.php', ['gdo'=>$this]); }
	
	public function delete()
	{
		@unlink($this->getDestPath());
		return parent::delete();
	}
	
	###############
	### Factory ###
	###############
	public static function filesDir()
	{
		return GWF_PATH . 'dbimg/files/';
	}
	
	/**
	 * @param array $values
	 * @return GWF_File
	 */
	public static function fromForm(array $values)
	{
		return self::blank(array(
			'file_name' => $values['name'],
			'file_size' => $values['size'],
			'file_type' => $values['mime']
		))->tempPath($values['path']);
	}
	
	public function copy()
	{
	    self::createDir(self::filesDir());
		copy($this->path, $this->getDestPath());
		$this->insert();
		$this->path = null;
		return $this;
	}
	
	/**
	 * @param string $contents
	 * @return GWF_File
	 */
	public static function fromString(string $name, string $content)
	{
		# Create temp dir
		$tempDir = GWF_PATH . 'temp/file';
		GWF_File::createDir($tempDir);
		# Copy content to temp file
		$tempPath = $tempDir . '/' . md5(md5($name).md5($content));
		file_put_contents($tempPath, $content);
		# Return fresh GWF_File.
		$values = array(
			'name' => $name,
			'size' => strlen($content),
			'mime' => mime_content_type($tempPath),
			'path' => $tempPath,
		);
		return self::fromForm($values);
	}

	############
	### Util ###
	############
{'fraction_non_alphanumeric': 0.21739954510993176, 'fraction_numerical': 0.003411675511751327, 'mean_word_length': 4.479750778816199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '1429017', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1808, 'n_words': 353}