@@ -27,6 +27,11 @@ class GithubApi
2727 */
2828 const API_NETWORK_ERROR = - 2 ;
2929
30+ /**
31+ * 请求错误
32+ */
33+ const API_REQUEST_ERROR = - 3 ;
34+
3035 /**
3136 * 数据完整性错误
3237 */
@@ -56,6 +61,13 @@ class GithubApi
5661 */
5762 private static $ client ;
5863
64+ /**
65+ * 日志文件
66+ *
67+ * @var string|boolean false-禁止记录日志
68+ */
69+ private static $ log_file = false ;
70+
5971 /**
6072 * 初始化
6173 *
@@ -75,6 +87,11 @@ public static function init ($config = [])
7587 {
7688 static ::setAccessToken ($ config ['access_token ' ]);
7789 }
90+
91+ if (static ::$ log_file !== false )
92+ {
93+ static ::$ log_file = dirname (__DIR__ ) . '/info.log ' ;
94+ }
7895 }
7996
8097 /**
@@ -146,9 +163,11 @@ public static function getSha ($owner, $repo, $path)
146163 * 上传的文件路径
147164 * @param string $insertOnly
148165 * 同名文件是否覆盖
166+ * @param integer $retry
167+ * 部分失败的原因会进行尝试重新上传,最多不超过重试次数
149168 * @return array
150169 */
151- public static function upload ($ owner , $ repo , $ srcPath , $ dstPath , $ message = '' , $ insertOnly = false , $ sha = null )
170+ public static function upload ($ owner , $ repo , $ srcPath , $ dstPath , $ message = '' , $ insertOnly = false , $ sha = null , $ retry = 3 )
152171 {
153172 if (! file_exists ($ srcPath ))
154173 {
@@ -208,8 +227,8 @@ public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '',
208227 {
209228 return array (
210229 'code ' => $ e ->getCode (),
211- 'message ' => ' 资源冲突 ' ,
212- 'data ' => []
230+ 'data ' => [] ,
231+ 'message ' => ' 资源冲突 '
213232 );
214233 }
215234 // 更新资源却没有提供 sha 签名值
@@ -237,6 +256,20 @@ public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '',
237256 );
238257 }
239258 }
259+ catch (\Exception $ e )
260+ {
261+ // 部分失败的原因会导致重试,不超过3次
262+ if ($ retry > 0 && strpos ($ e ->getMessage (), 'cURL error 35: OpenSSL SSL_connect: SSL_ERROR_SYSCALL ' ) !== false )
263+ {
264+ return static ::upload ($ owner , $ repo , $ srcPath , $ dstPath , $ message , $ insertOnly , $ sha , $ retry - 1 );
265+ }
266+
267+ return array (
268+ 'code ' => $ e ->getCode () == 0 ? 0 : static ::API_REQUEST_ERROR ,
269+ 'data ' => [],
270+ 'message ' => $ e ->getMessage ()
271+ );
272+ }
240273 }
241274
242275 /**
@@ -380,4 +413,40 @@ private static function normalizerPath ($path, $isfolder = False)
380413
381414 return $ path ;
382415 }
416+
417+ /**
418+ * 记录错误信息
419+ *
420+ * @param string $message
421+ * @return boolean
422+ */
423+ public static function error ($ message )
424+ {
425+ if (empty (static ::$ log_file ))
426+ {
427+ return false ;
428+ }
429+
430+ $ date = date ('Y-m-d H:i:s ' );
431+
432+ @file_put_contents (static ::$ log_file , "[ERROR][ {$ date }] " . $ message . "\n" , FILE_APPEND );
433+ }
434+
435+ /**
436+ * 记录日志信息
437+ *
438+ * @param string $message
439+ * @return boolean
440+ */
441+ public static function info ($ message )
442+ {
443+ if (empty (static ::$ log_file ))
444+ {
445+ return false ;
446+ }
447+
448+ $ date = date ('Y-m-d H:i:s ' );
449+
450+ @file_put_contents (static ::$ log_file , "[INFO][ {$ date }] " . $ message . "\n" , FILE_APPEND );
451+ }
383452}
0 commit comments