<commit_before><?php
require_once __DIR__.'/actions.php';
require_once APP_ROOT.'/model/Application.php';
require_once APP_ROOT.'/model/Package.php';
require_once APP_ROOT.'/model/IPAFile.php';

class uploadAction extends apiActions
{
	public function executeUpload()
	{
		$con = null;
		try{
			if(mfwRequest::method()!=='POST'){
				return $this->jsonResponse(
					self::HTTP_405_METHODNOTALLOWED,
					array('error'=>'Method Not Allowed'));
			}

			$api_key = mfwRequest::param('api_key');
			$file_info = mfwRequest::param('file');
			$title = mfwRequest::param('title');
			$description = mfwRequest::param('description');
			$notify = mfwRequest::param('notify');
			$tag_names = explode(',',mfwRequest::param('tags'));
			if(!$api_key||!$file_info||!$title){
				return $this->jsonResponse(
					self::HTTP_400_BADREQUEST,
					array('error'=>'A required field is not present.'));
			}

			$app = ApplicationDb::selectByApiKey($api_key);
			if(!$app){
				return $this->jsonResponse(
					self::HTTP_400_BADREQUEST,
					array('error'=>'Invalid api_key'));
			}

			// ファイルフォーマット確認, 情報抽出
			$file_content = file_get_contents($file_info['tmp_name']);
			list($platform,$ext,$mime) = PackageDb::getPackageInfo(
				$file_info['name'],$file_content,$file_info['type']);
			$ios_identifier = null;
			if($platform===Package::PF_IOS){
				$plist = IPAFile::parseInfoPlist($file_info['tmp_name']);
				$ios_identifier = $plist['CFBundleIdentifier'];
			}

			// DBへ保存
			$con = mfwDBConnection::getPDO();
			$con->beginTransaction();

			$app = ApplicationDb::retrieveByPKForUpdate($app->getId());

			$tags = $app->getTagsByName($tag_names,$con);

			$pkg = PackageDb::insertNewPackage(
				$app->getId(),$platform,$ext,
				$title,$description,$ios_identifier,$tags,$con);

			// S3へアップロード
			$pkg->uploadFile($file_content,$mime);

			$app->updateLastUpload($pkg->getCreated(),$con);

			$con->commit();
		}
		catch(Exception $e){
			if($con) $con->rollback();
			error_log(__METHOD__."$e->getMessage()");
			return $this->jsonResponse(
				self::HTTP_500_INTERNALSERVERERROR,
				array('error'=>$e->getMessage()));
		}

		if($notify){
			try{
				$users = $app->getInstallUsers();
				$users->noticePackageUploaded($pkg);
			}
			catch(Exception $e){
				// アップロード通知が送れなくても許容する<commit_msg>file upload error check<commit_after><?php
require_once __DIR__.'/actions.php';
require_once APP_ROOT.'/model/Application.php';
require_once APP_ROOT.'/model/Package.php';
require_once APP_ROOT.'/model/IPAFile.php';

class uploadAction extends apiActions
{
	public function executeUpload()
	{
		$con = null;
		try{
			if(mfwRequest::method()!=='POST'){
				return $this->jsonResponse(
					self::HTTP_405_METHODNOTALLOWED,
					array('error'=>'Method Not Allowed'));
			}

			$api_key = mfwRequest::param('api_key');
			$file_info = mfwRequest::param('file');
			$title = mfwRequest::param('title');
			$description = mfwRequest::param('description');
			$notify = mfwRequest::param('notify');
			$tag_names = explode(',',mfwRequest::param('tags'));
			if(!$api_key||!$file_info||!$title){
				return $this->jsonResponse(
					self::HTTP_400_BADREQUEST,
					array('error'=>'A required field is not present.'));
			}
			if(!isset($file_info['error'])||$file_info['error']!==UPLOAD_ERR_OK){
				return $this->jsonResponse(
					self::HTTP_400_BADREQUEST,
					array('error'=>'file is not uploaded: $_FILES[file]='.json_encode($file_info)));
			}

			$app = ApplicationDb::selectByApiKey($api_key);
			if(!$app){
				return $this->jsonResponse(
					self::HTTP_400_BADREQUEST,
					array('error'=>'Invalid api_key'));
			}

			// ファイルフォーマット確認, 情報抽出
			$file_content = file_get_contents($file_info['tmp_name']);
			list($platform,$ext,$mime) = PackageDb::getPackageInfo(
				$file_info['name'],$file_content,$file_info['type']);
			$ios_identifier = null;
			if($platform===Package::PF_IOS){
				$plist = IPAFile::parseInfoPlist($file_info['tmp_name']);
				$ios_identifier = $plist['CFBundleIdentifier'];
			}

			// DBへ保存
			$con = mfwDBConnection::getPDO();
			$con->beginTransaction();

			$app = ApplicationDb::retrieveByPKForUpdate($app->getId());

			$tags = $app->getTagsByName($tag_names,$con);

			$pkg = PackageDb::insertNewPackage(
				$app->getId(),$platform,$ext,
				$title,$description,$ios_identifier,$tags,$con);

			// S3へアップロード
			$pkg->uploadFile($file_content,$mime);

			$app->updateLastUpload($pkg->getCreated(),$con);

			$con->commit();
		}
		catch(Exception $e){
			if($con) $con->rollback();
			error_log(__METHOD__.": {$e->getMessage()}");
			return $this->jsonResponse(
				self::HTTP_500_INTERNALSERVERERROR,
				array('error'=>$e->getMessage()));
		}

		if($notify){
			try{
				$users = $app->getInstallUsers();
				$users->noticePackageUploaded($pkg);
			}
			catch(Exception $e){
				// アップロード通知が送れなくても許容する
{'fraction_non_alphanumeric': 0.18928423665701283, 'fraction_numerical': 0.0059991725279271825, 'mean_word_length': 4.947109471094711, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 47, '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': '6426018', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1700, 'n_words': 218}