<commit_before><?php

/**
 * Upload Controller
 *
 * @author     <NAME> https://github.com/kenjis
 * @copyright  2012 <NAME>
 * @license    AGPL 3.0 http://opensource.org/licenses/AGPL-3.0
 */
class Controller_Upload extends Controller
{
	public function get_index()
	{
		return Response::forge(View::forge('upload/index'));
	}
	
	public function post_index()
	{
		Upload::process();
		
		if (Upload::is_valid())
		{
			Upload::save();
		}
		
		foreach (Upload::get_errors() as $file)
		{
			//Debug::dump($file);
			
			$error = $file['errors']['0']['message'];
			
			$view = View::forge('upload/index');
			$view->set('error', $error);
			
			return Response::forge($view);
		}
		
		$results = Upload::get_files();
		$file = $results[0]['saved_as'];
		
		$epub = $this->create_kepub($file);
		
		// remove uploaded epub file
		unlink($results[0]['saved_to'] . $file);
		
		$epub->download();  // exit
	}
	
	protected function create_kepub($file)
	{
		! ini_get('safe_mode') and set_time_limit(0);
		
		$kepub_dir = APPPATH . 'tmp/kepub/' . getmypid();
		
		$epub = new \Model_Epub();
		$epub->set_epub_dir(APPPATH . 'tmp');
		$epub->set_kepub_dir($kepub_dir);
		$epub->set_filename($file);
		$epub->set_prefix(date('Ymd_'));
		$epub->set_image_max_size(600, 800);
		$epub->build_kepub();
		
		return $epub;
	}
}<commit_msg>Add logging of uploading<commit_after><?php

/**
 * Upload Controller
 *
 * @author     <NAME> https://github.com/kenjis
 * @copyright  2012 <NAME>
 * @license    AGPL 3.0 http://opensource.org/licenses/AGPL-3.0
 */
class Controller_Upload extends Controller
{
	public function get_index()
	{
		return Response::forge(View::forge('upload/index'));
	}
	
	public function post_index()
	{
		Upload::process();
		
		if (Upload::is_valid())
		{
			Upload::save();
		}
		
		foreach (Upload::get_errors() as $file)
		{
			//Debug::dump($file);
			
			$error = $file['errors']['0']['message'];
			
			$view = View::forge('upload/index');
			$view->set('error', $error);
			
			return Response::forge($view);
		}
		
		$results = Upload::get_files();
		$file = $results[0]['saved_as'];
		
		static::log('uploaded: ' . $file, __METHOD__);
		
		$epub = $this->create_kepub($file);
		
		// remove uploaded epub file
		unlink($results[0]['saved_to'] . $file);
		
		$epub->download();  // exit
	}
	
	protected function create_kepub($file)
	{
		! ini_get('safe_mode') and set_time_limit(0);
		
		$kepub_dir = APPPATH . 'tmp/kepub/' . getmypid();
		
		$epub = new \Model_Epub();
		$epub->set_epub_dir(APPPATH . 'tmp');
		$epub->set_kepub_dir($kepub_dir);
		$epub->set_filename($file);
		$epub->set_prefix(date('Ymd_'));
		$epub->set_image_max_size(600, 800);
		$epub->build_kepub();
		
		return $epub;
	}
	
	protected static function log($msg, $method)
	{
		$uri   = \Input::uri();
		$ip    = \Input::ip();
		$agent = \Input::user_agent();
	
		$msg = $msg . ' [' . $ip . ' "' . $agent . '"]';
	
		\Log::write('EPUB', $msg, $method);
	}
}
{'fraction_non_alphanumeric': 0.20264765784114053, 'fraction_numerical': 0.012219959266802444, 'mean_word_length': 3.5478395061728394, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '510480', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1090, 'n_words': 182}