Python2.7+php5.4結合對安卓apk包進行分包處理-2
###PHP段
#PHP端主要是處理分包和上傳OSS阿里雲
<?php namespace Org\Util; require('./Expand/cos/include.php'); use Qcloud_cos\Auth; use Qcloud_cos\Cosapi; use OSS\Core\OssException; class AppSubpackage {
private $info = null;
private $gameInfo;
private $sourFile;
private $subpackagePath;
private $chID;
private $chName;
private $appLocationAddress;
public function __construct($gameID, $chID = null) { $this->chID = $chID; //根據GID查詢AppProvider表的資訊 $appProviderResult=M('AppProvider',Null,'DB_CONFIG2')->field("id,app_name,app_name_cn,gid,main_package_ver,main_package_old_ver,main_package,mark_banben")->where("gid='".$gameID."'")->find(); // 拿到主包的所有的資訊 if (! empty($appProviderResult)) { $this->gameInfo = $appProviderResult; $this->sourFile = $appProviderResult['main_package']; } else { die('嚴重錯誤,遊戲資訊拉取失敗'); } }
public function empty_is(){
}
// 改版後的分包 public function create_apk(){
//建立目錄 $this->createFolder(); $coreChannelOne=M('CoreChannelOneInfo',Null,'DB_CONFIG3')->where("ch_id='".$this->chID."'")->find(); if(empty($coreChannelOne)){ $coreChannelTwo =M('CoreChannelTwoInfo',Null,'DB_CONFIG3')->where("ch2_id='".$this->chID."'")->find(); }
//賦值渠道名稱 if (!empty($coreChannelOne)) { $this->chName = $coreChannelOne['ch_uname']; }else if(!empty($coreChannelTwo)){ $this->chName = $coreChannelTwo['ch2_uname']; }
$AppSubpackageWhere['gid']=$this->gameInfo['gid']; $AppSubpackageWhere['chid']=$this->chID; $appSubpackage=M('AppSubpackage',Null,'DB_CONFIG3')->where($AppSubpackageWhere)->find(); // 如果為null。那就是第一次生成 if(empty($appSubpackage)){ $Subpackage=$this->create_apks($this->chID); if(!empty($Subpackage)){
//上傳OSS阿里雲 $cos_url=$this->AliUpdate1($Subpackage,$this->chID); if(!empty($cos_url)){ @unlink($Subpackage); }else{ return "沒有oss地址"; } // if($isset_apk_file==true){ $appSubpackageData=array(); $appSubpackageData['app_name'] = $this->gameInfo['app_name']; $appSubpackageData['app_name_cn'] = $this->gameInfo['app_name_cn']; $appSubpackageData['gid']= $this->gameInfo['gid']; $appSubpackageData['subpackage_ver'] = $this->gameInfo['main_package_ver']; $appSubpackageData['subpackage_old_ver'] = $this->gameInfo['main_package_old_ver']; $appSubpackageData['chid']= $this->chID; $appSubpackageData['subpackage'] =""; $appSubpackageData['cos_url']=$cos_url; $appSubpackageData['create_time'] = time(); $appSubpackageData['cos_flag'] = 'no'; $appSubpackageData['up_flag'] = 3; $result=M('AppSubpackage',Null,'DB_CONFIG3')->data($appSubpackageData)->add(); if($result>0){ return $cos_url; }else{ return "修改失敗"; } } }else{ $wheres['id']=$appSubpackage['id']; $Subpackage=$this->create_apks($this->chID); if(!empty($Subpackage)){
//上傳OSS阿里雲 $cos_url=$this->AliUpdate1($Subpackage,$this->chID,$appSubpackageResult['cos_url']); // $isset_apk_file=""; // $isset_apk_file=$this->isset_ossapk($cos_url); if(!empty($cos_url)){ @unlink($Subpackage); }else{ return "沒有oss地址"; } // if($isset_apk_file==true){ $appSubpackageData=array(); $appSubpackageData['app_name'] = $this->gameInfo['app_name']; $appSubpackageData['app_name_cn'] = $this->gameInfo['app_name_cn']; $appSubpackageData['gid']= $this->gameInfo['gid']; $appSubpackageData['subpackage_ver'] = $this->gameInfo['main_package_ver']; $appSubpackageData['subpackage_old_ver'] = $this->gameInfo['main_package_old_ver']; $appSubpackageData['chid']= $this->chID; $appSubpackageData['subpackage'] =""; $appSubpackageData['cos_url']=$cos_url; $appSubpackageData['create_time'] = time(); $appSubpackageData['cos_flag'] = 'no'; $appSubpackageData['up_flag'] = 3; $result=M('AppSubpackage',Null,'DB_CONFIG3')->where($wheres)->data($appSubpackageData)->save(); if($result>0){ return $cos_url; }else{ return "修改失敗"; } } } }
private function create_apks($chid) { // 新包存放的路徑E:upload\subpackage\mhyj\mhyjCH1146713009311_test.apk全路徑 $newFile = $this->subpackagePath .'/'.$this->gameInfo['app_name'] . $chid . '_' . $this->chName . '.apk'; $this->appLocationAddress = $newFile; // 獲取到子包的本地地址 if (!copy($this->sourFile, $newFile)) { die('無法建立檔案打包失敗'); } $string = null; $channelName = "META-INF/AY_" . $chid; $zip = new \ZipArchive();
//拼接絕對路徑,生成渠道包地址 $OrlFilePath1=$newFile; //拼接成打包簽名後的新包名 $NewFilePath2=$this->subpackagePath."/".$this->gameInfo['app_name']."_".$chid."_".$this->chName."_".$this->gameInfo['mark_banben'].".apk"; if ($zip->open($OrlFilePath1) == true) {
//刪除簽名檔案 $zip->deleteName("META-INF/MANIFEST.MF"); $zip->deleteName("META-INF/CERT.SF"); $zip->deleteName("META-INF/CERT.RSA"); $zip->deleteName("META-INF/COSPLAY.SF"); $zip->deleteName("META-INF/COSPLAY.RSA");
$flag = $zip->addFromString($channelName, null); if (!$flag){ $zip->addFromString($channelName, null); } $zip->close(); try{ $ShellBat="/usr/java/jdk1.8.0_65/bin/signapk.sh ".$OrlFilePath1." ".$NewFilePath2; $message=exec($ShellBat); }catch(Exception $e) { $orig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $chid . '_' . $this->chName . '.apk.orig'; $sig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $chid . '_' . $this->chName . '.apk.sig'; @unlink($orig); @unlink($sig); return false; }
$orig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $chid . '_' . $this->chName . '.apk.orig'; $sig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $chid . '_' . $this->chName . '.apk.sig'; if(trim($message)=="ok"){ // //刪除衝突殘留檔案 @unlink($orig); @unlink($sig); @unlink($OrlFilePath1); // dump($NewFilePath2);die; // unlink($NewFilePath2); return $NewFilePath2; die; }else{ //如果簽名失敗,刪除分包 unlink($NewFilePath2); unlink($OrlFilePath1); @unlink($orig); @unlink($sig); return false; } } else { //如果開啟失敗,就刪除複製過來的分包 @unlink($orig); unlink($newFile); return false; } }
public function addSubpackage() { $this->createFolder();
// 根據傳進來的chID判斷是不是進行單個的更新,然後組裝成陣列 if (empty($this->chID)) { $arrChannel = $this->getArrChannel(); } else { $arrChannel = array( $this->chID ); } $this->info = 0; $time1 = time(); if (!empty($arrChannel)) { foreach ($arrChannel as $value) { $this->info ++;
// $temp = trim($value); // 將CHID渠道名字進行處理 if (!empty($this->chID)) { $tempCHID = $this->chID; } else { $tempCHID = $temp; } // 獲取渠道的名字,進行apk檔案的命名 // $coreChannelOneInfo = CoreChannelOneInfoModel::model()->findByAttributes(array( // 'ch_id' => $tempCHID // )); $coreChannelOne=M('CoreChannelOneInfo',Null,'DB_CONFIG3')->where("ch_id='".$tempCHID."'")->find();
//如果CoreChannelOneInfo一級渠道表沒有想過資訊 //那麼就查詢二級渠道表 if(empty($coreChannelOne)){ $coreChannelTwo =M('CoreChannelTwoInfo',Null,'DB_CONFIG3')->where("ch2_id='".$tempCHID."'")->find(); } //賦值渠道名稱 if (!empty($coreChannelOne)) { $this->chName = $coreChannelOne['ch_uname']; }else if(!empty($coreChannelTwo)){ $this->chName = $coreChannelTwo['ch2_uname']; } // 如果都為空,那麼渠道名字就是aooyou if (empty($coreChannelOne) && empty($coreChannelTwo)) { $this->chName = 'aooyou'; } // // 去掉生成包的重複 $AppSubpackageWhere['gid']=$this->gameInfo['gid']; $AppSubpackageWhere['chid']=$temp; $appSubpackage=M('AppSubpackage',Null,'DB_CONFIG3')->where($AppSubpackageWhere)->find(); //開啟事物機制 // M()->startTrans(); if (empty($appSubpackage)){ return false; }else{ vendor('aliyun.autoload'); $accessKeyId = "xxx"; $accessKeySecret = "xxx";//去阿里雲後臺獲取祕鑰 $endpoint = "xxx";//你的阿里雲OSS地址 $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucket= "xxx";//oss中的檔案上傳空間
$Where1['id']=$appSubpackage['id']; // try{ $Subpackage=$this->createSubpackage($temp);
$cos_url=$this->AliUpdate($Subpackage,$temp,$appSubpackage['cos_url']); if(!empty($cos_url) && $ossClient->doesObjectExist($bucket, $cos_url)){ // @unlink($Subpackage); $appSubpackageData['app_name'] = $this->gameInfo['app_name']; $appSubpackageData['app_name_cn'] = $this->gameInfo['app_name_cn']; $appSubpackageData['gid']= $this->gameInfo['gid']; $appSubpackageData['subpackage_ver'] = $this->gameInfo['main_package_ver']; $appSubpackageData['subpackage_old_ver'] = $this->gameInfo['main_package_old_ver']; $appSubpackageData['chid']= $temp; $appSubpackageData['subpackage'] =$Subpackage; $appSubpackageData['cos_url']=$cos_url; $appSubpackageData['create_time'] = time(); $appSubpackageData['cos_flag'] = 'no'; $appSubpackageData['up_flag'] = 3; $result=M('AppSubpackage',Null,'DB_CONFIG3')->where($Where1)->data($appSubpackageData)->save(); if($result>0){ $where11['gid']=$this->gameInfo['gid']; $where11['chid']=$temp; //刪除APP_dsrw資料庫裡面的連結 M('AppDsrw')->where($where11)->delete(); // echo M('AppDsrw')->getLastSql(); // dump($result111); // M()->commit(); @unlink($Subpackage); return $cos_url; }else{ // M()->rollback(); return false; } // }catch(Exception $e){ // } } // if($Subpackage==false){ // $appSubpackageData['app_name'] = $this->gameInfo['app_name']; // $appSubpackageData['app_name_cn'] = $this->gameInfo['app_name_cn']; // $appSubpackageData['gid']= $this->gameInfo['gid']; // $appSubpackageData['subpackage_ver'] = $this->gameInfo['main_package_ver']; // $appSubpackageData['subpackage_old_ver'] = $this->gameInfo['main_package_old_ver']; // $appSubpackageData['chid']= $temp; // $appSubpackageData['subpackage'] =$Subpackage; // $appSubpackageData['cos_url']= ""; // $appSubpackageData['create_time'] = time(); // $appSubpackageData['cos_flag'] = 'no'; // $appSubpackageData['up_flag'] = 0; // M('AppSubpackage',Null,'DB_CONFIG3')->where($Where1)->data($appSubpackageData)->save(); // return false; // }
} } }else{ return false; } }
// 建立子包,返回本地的儲存地址 private function createSubpackage($channel) {
// 新包存放的路徑E:upload\subpackage\mhyj\mhyjCH1146713009311_test.apk全路徑 $newFile = $this->subpackagePath .'/'.$this->gameInfo['app_name'] . $channel . '_' . $this->chName . '.apk'; // dump($newFile);die; $this->appLocationAddress = $newFile; // 獲取到子包的本地地址 $NewFilePath2=$this->subpackagePath."/".$this->gameInfo['app_name']."_".$channel."_".$this->chName.".apk";
//檢測是否正在簽名或者分包? $sign_apks=$newFile.".sig"; if(file_exists($newFile) || file_exists($sign_apks)){ return false; } //檢測檔案是否存在? if(file_exists($NewFilePath2)) { return $NewFilePath2; } // 如果是更新,APK包存在的話,那麼就先刪除伺服器上的APK包 // @unlink($this->subpackagePath."/".$this->gameInfo['app_name']."_".$channel."_".$this->chName.".apk"); if (!copy($this->sourFile, $newFile)) { die('無法建立檔案打包失敗'); } $string = null; $channelName = "META-INF/AY_" . $channel; $zip = new \ZipArchive();
//拼接絕對路徑,生成渠道包地址 $OrlFilePath1=$newFile; //拼接成打包簽名後的新包名 // $NewFilePath2=$this->subpackagePath."/".$this->gameInfo['app_name']."_".$channel."_".$this->chName.".apk";
if ($zip->open($OrlFilePath1) == true) {
//刪除簽名檔案 $zip->deleteName("META-INF/MANIFEST.MF"); $zip->deleteName("META-INF/CERT.SF"); $zip->deleteName("META-INF/CERT.RSA"); $zip->deleteName("META-INF/COSPLAY.SF"); $zip->deleteName("META-INF/COSPLAY.RSA");
$flag = $zip->addFromString($channelName, null); if (!$flag){ $zip->addFromString($channelName, null); } $zip->close(); try{ $ShellBat="/usr/java/jdk1.8.0_65/bin/signapk.sh ".$OrlFilePath1." ".$NewFilePath2; $message=exec($ShellBat); // dump($message);die; }catch(Exception $e) { $orig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $channel . '_' . $this->chName . '.apk.orig'; $sig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $channel . '_' . $this->chName . '.apk.sig'; @unlink($orig); @unlink($sig); return false; }
$orig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $channel . '_' . $this->chName . '.apk.orig'; $sig=$this->subpackagePath .'/'.$this->gameInfo['app_name'] . $channel . '_' . $this->chName . '.apk.sig'; if(trim($message)=="ok"){ // //刪除衝突殘留檔案 @unlink($orig); @unlink($sig); @unlink($OrlFilePath1); // unlink($NewFilePath2); return $NewFilePath2; die; }else{ //如果簽名失敗,刪除分包 unlink($NewFilePath2); unlink($OrlFilePath1); @unlink($orig); @unlink($sig); return false; } } else { //如果開啟失敗,就刪除複製過來的分包 @unlink($orig); unlink($newFile); return false; } } private function createFolder() { // upload目錄:/upload/ $uploadPath ='./Public/'; // res目錄:/upload/subpackage/ $resPath = $uploadPath . 'subpackage/'; // 遊戲代號目錄:/upload/subpackage/app_name $this->subpackagePath = $resPath . $this->gameInfo['app_name']; // 通過遞迴的方式進行檔案目錄的建立 if (file_exists($uploadPath)) { if (file_exists($resPath)) { if (file_exists($this->subpackagePath)) { return true; } else { if (mkdir($this->subpackagePath)) { $this->createFolder(); } else { return false; } } } else { if (mkdir($resPath)) { $this->createFolder(); } else { return false; } } } else { if (mkdir($uploadPath)) { $this->createFolder(); } else { return false; } } }
private function AliUpdate1($filePath,$Chid,$oldfile="") {
vendor('aliyun.autoload'); $accessKeyId = "xxx"; $accessKeySecret = "xxx";//去阿里雲後臺獲取祕鑰 $endpoint = "xxx";//你的阿里雲OSS地址
$uploadPath ='Public/'; $resPath = $uploadPath . 'subpackage/';
$ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucket= "xxx";//oss中的檔案上傳空間 $rand=rand(100,999); $object=""; $object = $resPath.$this->gameInfo['app_name'].'/'.$this->gameInfo['app_name']."_".$this->chName."_".$this->gameInfo['mark_banben'].".apk";//想要儲存檔案的名稱 // $object=$resPath.$this->gameInfo['app_name'].'/'.$this->gameInfo['app_name']."_".$Chid."_".$this->chName.".apk";//想要儲存檔案的名稱 $file=""; $file = $filePath;//檔案路徑,必須是本地的。 $options=array(); /* * 上傳之前先刪除檔案 */ if(!empty($oldfile)){ //檢測檔案是否存在 // try{ $exist = $ossClient->doesObjectExist($bucket, $oldfile);
// } catch(OssException $e) { // return false; // } //true為存在,否則跳過 if($exist){ $ossClient->deleteObject($bucket,$oldfile,$options); } }
//先分片上傳 try{
$ossClient->multiuploadFile($bucket,$object,$file,$options); } catch(OssException $e) { $dir="./Public/oss_error_log.txt"; $myfile=file_put_contents($dir,"\n111-日期:".date("Y-m-d H:i:s",time())."---".$e->getMessage(),FILE_APPEND); return false; }
$exist_obj = $ossClient->doesObjectExist($bucket, $object);
if($exist_obj==true){ //上傳成功,返回地址 return $object; }else{ return false; } }
public function isset_ossapk($object){ vendor('aliyun.autoload'); $accessKeyId = "xxx"; $accessKeySecret = "xxx";//去阿里雲後臺獲取祕鑰 $endpoint = "oss-cn-shenzhen-internal.aliyuncs.com";//你的阿里雲OSS地址 $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucket= "xxx";//oss中的檔案上傳空間 $isset=$ossClient->doesObjectExist($bucket, $object); return $isset; }
//當銷燬類是執行 public function __destruct(){ unset($this->chID); unset($this->gameInfo); unset($this->sourFile); $this->empty_apk(); }
public function empty_apk(){
} }