master
elf 4 months ago
parent d5d98df533
commit a2bb9f0007

@ -20,6 +20,8 @@ use BaiduBce\Auth\SignOptions;
use BaiduBce\Log\LogFactory;
use Think\Think;
use Base\Service\GameSourceService;
use Base\Tool\CosClient;
use Base\Tool\OssClient as ToolOssClient;
use Base\Tool\Printer;
use Base\Tool\Redis;
@ -312,7 +314,7 @@ class AutoPackController extends Think
$map['status'] = 1;
$map['enable_status'] = ['in', '0,2'];
$applys = M('apply', 'tab_')->field('id,game_id,game_name,promote_id,promote_account,sdk_version,pack_url,plist_url')
$applys = M('apply', 'tab_')->field('id,game_id,game_name,promote_id,promote_account,sdk_version')
->where($map)
->order('bale_sort desc,id desc')
->limit(15)
@ -1077,4 +1079,8 @@ class AutoPackController extends Think
return '';
}
}
public function clearPkg() {
$client = new ToolOssClient();
}
}

@ -1,11 +1,9 @@
<?php
namespace Base\Service;
use ZipArchive;
use Base\Model\PromoteModel;
use Base\Model\ApplyModel;
use Base\Tool\Base62;
use Base\Tool\PlistDemo;
use Base\Tool\Storage;
use GuzzleHttp\Client;
class GameSourceService {
@ -88,8 +86,8 @@ class GameSourceService {
'source_version' => $gameSource['source_version'],
];
$zip = new ZipArchive();
if (!$zip->open($localPath, ZipArchive::CREATE)) {
$zip = new \ZipArchive();
if (!$zip->open($localPath, \ZipArchive::CREATE)) {
return false;
}
@ -109,7 +107,7 @@ class GameSourceService {
/**
* 打入渠道信息文件
* @param ZipArchive $zip 包文件
* @param string $zip 包文件
* @param string $distFile 打入文件名
* @param array $packData 打入信息
* @return boolean 是否成功
@ -122,9 +120,9 @@ class GameSourceService {
/**
* 打入渠道信息目录用于IOS主要为了解决IOS13问题
* @param ZipArchive $zip 包文件
* @param string $distFolder 打入文件夹
* @return boolean 是否成功
* @param string $zip 包文件
* @param string $distFolder 打入文件夹
* @return boolean 是否成功
*/
public function packChannelFolder($zip, $distFolder)
{
@ -176,7 +174,7 @@ class GameSourceService {
} else {
return [
'status' => false,
'message' => '打包失败,上传文件失败!' . $result['message'],
'message' => '打包失败,上传OSS失败!' . $result['message'],
];
}
@ -275,13 +273,8 @@ class GameSourceService {
M('apply_launch', 'tab_')->where(['apply_id' => $apply['id'], 'launch_packge'=>['in', [0, 2, 3]]])->save($launchData);
}
$this->removeOlderFile($apply['pack_url']);
$this->removeOlderFile($apply['plist_url']);
$savePath = '';
$fileName = $apply['game_id'] . '_' . substr(md5(microtime(true) . 'game_package' . $apply['game_id'] . '-' . $apply['promote_id']), 8 , 16);
$fileName = 'game_package' . $apply['game_id'] . '-' . $apply['promote_id'];
if ($apply['sdk_version'] == 1) {
$fileName .= '.apk';
$savePath = 'Uploads/GamePack/' . $fileName;
@ -316,7 +309,7 @@ class GameSourceService {
} else {
return [
'status' => false,
'message' => '打包失败,上传文件失败!' . $result['message'],
'message' => '打包失败,上传OSS失败!' . $result['message'],
];
}
@ -368,7 +361,7 @@ class GameSourceService {
public function uploadPackage($localFilePath, $distFilePath, $isDeleteLocal = false)
{
$isChunk = C('PACKAGE_CHUNK_ENABLED') ? true : false;
if (C('OSS_STATUS')) {
if (C('STORAGE_TYPE')) {
if ($isChunk) {
return $this->uploadPackageChunk($localFilePath, $distFilePath, $isDeleteLocal);
} else {
@ -423,7 +416,7 @@ class GameSourceService {
'timeout' => 30.0,
]);
try {
$response = $client->post('/upload', [
$response = $client->post('/upload/upload', [
'verify' => false,
'form_params' => [
'file' => $localFilePath,
@ -450,8 +443,7 @@ class GameSourceService {
public function uploadPackageOnce($localFilePath, $distFilePath, $isDeleteLocal = false)
{
$ossService = new OssService();
$result = $ossService->upload($localFilePath, $distFilePath);
$result = Storage::upload($localFilePath, $distFilePath);
if ($isDeleteLocal && file_exists($localFilePath)) {
@unlink($localFilePath);
}

@ -0,0 +1,94 @@
<?php
namespace Base\Tool;
use Base\Tool\StorageClient;
use Exception;
use Qcloud\Cos\Client as BaseCosClient;
/**
* 腾讯云OBS
*/
class CosClient implements StorageClient
{
private $secretId;
private $secretKey;
private $region;
private $domain;
private $bucket;
private $client;
public function __construct()
{
$this->domain = C('COS_DOMAIN');
$this->region = C('COS_REGION');
$this->bucket = C('COS_BUCKET');
$this->secretId = C('COS_SECRET_ID');
$this->secretKey = C('COS_SECRET_KEY');
$this->client = new BaseCosClient(
[
'region' => $this->region,
'scheme' => 'https', //协议头部默认为http
'credentials'=> [
'secretId' => $this->secretId,
'secretKey' => $this->secretKey,
]
]
);
}
public function upload($localFilePath, $saveFileName)
{
try {
$file = fopen($localFilePath, 'rb');
if (!$file) {
throw new Exception('读取文件失败');
}
$result = $this->client->Upload(
$this->bucket,
$saveFileName,
$file
);
return [
'status' => true,
'message' => '上传成功',
'data' => [
'url' => $this->getUrl($saveFileName)
]
];
} catch (\Exception $e) {
return [
'status' => false,
'message' => $e->getMessage()
];
}
}
public function delete($deleteFile)
{
try {
$result = $this->client->deleteObject(array(
'Bucket' => $this->bucket,
'Key' => $deleteFile
));
return [
'status' => true,
'message' => '删除成功',
];
} catch (\Exception $e) {
return [
'status' => false,
'message' => $e->getMessage()
];
}
}
public function getUrl($saveFileName)
{
return $this->domain . '/' . $saveFileName;
}
public function __destruct()
{
}
}

@ -0,0 +1,113 @@
<?php
namespace Base\Tool;
use Base\Tool\StorageClient;
use Obs\ObsClient as BaseObsClient;
/**
* 华为云OBS
*/
class ObsClient implements StorageClient
{
const MAX_PART_SIZE = 5368709120;
const MID_PART_SIZE = 10485760;
const MIN_PART_SIZE = 102400;
private $accessKey;
private $secretKey;
private $endpoint;
private $bucket;
private $domain;
private $client;
public function __construct()
{
$this->accessKey = C('OBS_ACCESS_KEY');
$this->secretKey = C('OBS_SECRET_KEY');
$this->endpoint = C('OBS_ENDPOINT');
$this->domain = C('OBS_DOMAIN');
$this->bucket = C('OBS_BUCKET');
$this->client = new BaseObsClient([
'key' => $this->accessKey,
'secret' => $this->secretKey,
'endpoint' => $this->endpoint
]);
}
public function upload($localFilePath, $saveFileName)
{
/* $resp = $this->client->putObject([
'Bucket' => $this->bucket,
'Key' => $saveFileName,
'SourceFile' => $localFilePath,
]); */
try {
$this->multiuploadFile($localFilePath, $saveFileName);
return [
'status' => true,
'message' => '上传OSS成功',
'data' => [
'url' => $this->getUrl($saveFileName)
]
];
} catch (\Obs\ObsException $e) {
return [
'status' => false,
'message' => $e->getExceptionMessage()
];
}
}
private function multiuploadFile($file, $saveFileName)
{
$resp = $this->client->initiateMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $saveFileName,
'ContentType' => 'text/plain'
]);
$uploadId = $resp['UploadId'];
$packetSize = 1048576;
$byteTotal = filesize($file);
$packetTotal = ceil($byteTotal / $packetSize);
$fileContent = fopen($file, 'r');
$parts = [];
for ($i=0; $i<$packetTotal; $i++) {
$packetFileContent = fread($fileContent, $packetSize);
$resp = $this->client->uploadPart([
'Bucket' => $this->bucket,
'Key' => $saveFileName,
'UploadId' => $uploadId,
'PartNumber' => $i+1,
'Body' => $packetFileContent
]);
$parts[] = ['PartNumber' => $i+1, 'ETag' => $resp['ETag']];
}
fclose($fileContent);
$resp = $this->client->completeMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $saveFileName,
'UploadId' => $uploadId,
'Parts' => $parts
]);
}
public function delete($deleteFile)
{
$resp = $this->client->deleteObject([
'Bucket' => $this->bucket,
'Key' => $deleteFile,
]);
}
public function getUrl($saveFileName)
{
return $this->domain . '/' . $saveFileName;
}
public function __destruct()
{
$this->client->close();
}
}

@ -0,0 +1,127 @@
<?php
namespace Base\Tool;
use OSS\Core\OssUtil;
use OSS\OssClient as BaseOssClient;
use OSS\Core\OSsException;
/**
* 阿里云OSS
*/
class OssClient implements StorageClient {
private $accessKeyId = '';
private $accessKeySecret = '';
private $endpoint = '';
private $isCName = false;
private $bucket = '';
private $domain = '';
private $client;
private $errorMessage = '';
public function __construct()
{
Vendor('OSS.autoload');
$this->accessKeyId = C('OSS_ACCESS_KEY_ID');
$this->accessKeySecret = C('OSS_ACCESS_KEY_SECRET');
$this->endpoint = C('OSS_ENDPOINT');
$this->domain = C('OSS_DOMAIN');
$this->isCName = C('OSS_IS_CNAME');
$this->bucket = C('OSS_BUCKET');
$this->client = new BaseOssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint, $this->isCName);
}
public function upload($localFilePath, $saveFileName)
{
try {
$this->multiuploadFile($localFilePath, $saveFileName);
return [
'status' => true,
'message' => '上传OSS成功',
'data' => [
'url' => $this->getUrl($saveFileName)
]
];
} catch (OssException $e) {
return [
'status' => false,
'message' => $e->getMessage()
];
}
}
private function multiuploadFile($file, $saveFileName)
{
$uploadId = $this->client->initiateMultipartUpload($this->bucket, $saveFileName);
/*
* step 2. 上传分片
*/
$partSize = 5 * 1000 * 1024;
$uploadFile = $file;
$uploadFileSize = filesize($uploadFile);
$pieces = $this->client->generateMultiuploadParts($uploadFileSize, $partSize);
$responseUploadPart = [];
$uploadPosition = 0;
$isCheckMd5 = true;
foreach ($pieces as $i => $piece) {
$fromPos = $uploadPosition + (integer) $piece[BaseOssClient::OSS_SEEK_TO];
$toPos = (integer) $piece[BaseOssClient::OSS_LENGTH] + $fromPos - 1;
$upOptions = [
BaseOssClient::OSS_FILE_UPLOAD => $uploadFile,
BaseOssClient::OSS_PART_NUM => ($i + 1),
BaseOssClient::OSS_SEEK_TO => $fromPos,
BaseOssClient::OSS_LENGTH => $toPos - $fromPos + 1,
BaseOssClient::OSS_CHECK_MD5 => $isCheckMd5,
];
if ($isCheckMd5) {
$contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
$upOptions[BaseOssClient::OSS_CONTENT_MD5] = $contentMd5;
}
// 2. 将每一分片上传到OSS
$responseUploadPart[] = $this->client->uploadPart($this->bucket, $saveFileName, $uploadId, $upOptions);
}
$uploadParts = [];
foreach ($responseUploadPart as $i => $eTag) {
$uploadParts[] = [
'PartNumber' => ($i + 1),
'ETag' => $eTag,
];
}
/**
* step 3. 完成上传
*/
$this->client->completeMultipartUpload($this->bucket, $saveFileName, $uploadId, $uploadParts);
}
/**
*删除文件
*/
public function delete($deleteFile)
{
$this->client->deleteObject($this->bucket, $deleteFile);
}
public function getUrl($saveFileName)
{
$url = '';
/* if ($this->isCName) {
$url = 'http://' . $this->endpoint . '/' . $saveFileName;
} else {
$url = 'https://' . $this->bucket . '.' . $this->endpoint . '/' . $saveFileName;
$url = str_replace('-internal', '', $url);
} */
$url = $this->domain . '/' . $saveFileName;
return $url;
}
/**
* 删除文件
*/
public function listObjects($deleteFile)
{
$listObject = $this->client->listObjects($this->bucket);
var_dump($listObject);
}
}

@ -0,0 +1,42 @@
<?php
namespace Base\Tool;
use Exception;
class Storage {
/**
* @var StorageClient
*/
private static $client;
private static function getClient(): StorageClient {
if (empty(self::$client)) {
if (C('STORAGE_TYPE') == 'oss') {
self::$client = new OssClient();
} elseif (C('STORAGE_TYPE') == 'obs') {
self::$client = new ObsClient();
} elseif (C('STORAGE_TYPE') == 'cos') {
self::$client = new CosClient();
} else {
throw new Exception("Storage type undefined!");
}
}
return self::$client;
}
public static function upload($localFilePath, $saveFileName) {
return self::getClient()->upload($localFilePath, $saveFileName);
}
public static function delete($deleteFile)
{
return self::getClient()->delete($deleteFile);
}
public static function getUrl($saveFileName)
{
return self::getClient()->getUrl($saveFileName);
}
}

@ -0,0 +1,12 @@
<?php
namespace Base\Tool;
interface StorageClient {
public function upload($localFilePath, $saveFileName);
public function delete($deleteFile);
public function getUrl($saveFileName);
}

@ -1,7 +1,8 @@
{
"require": {
"rodneyrehm/plist": "^2.0",
"guzzlehttp/guzzle": "~6.0"
"guzzlehttp/guzzle": "~6.0",
"obs/esdk-obs-php": "3.22.6"
},
"repositories": {
"packagist": {

Loading…
Cancel
Save