lh 1 deň pred
rodič
commit
22859075cf
2 zmenil súbory, kde vykonal 68 pridanie a 1 odobranie
  1. 35 0
      app/Console/Test/TestCommand.php
  2. 33 1
      app/Libs/Helpers.php

+ 35 - 0
app/Console/Test/TestCommand.php

@@ -49,6 +49,41 @@ class TestCommand extends Command
      */
     public function handle()
     {
+        // // 转存阿里云oss文件到tos
+        // $audio_effects = DB::table('mp_timbres')->where('id', 246)->get();
+        // $total = count($audio_effects);
+        // $success = 0;
+        // $continue = 0;
+        // $fail = 0;
+        // foreach ($audio_effects as $item) {
+        //     $id = getProp($item, 'id');
+        //     $url = getProp($item, 'audio_url');
+        //     if (!$url) {
+        //         $continue++;
+        //         continue;
+        //     }
+        //     if (!strstr($url, 'https://cdn-zwai.ycsd.cn')) {
+        //         $continue++;
+        //         continue;
+        //     }
+        //     $index = strrpos($url, '/');
+        //     $filename = mb_substr($url, $index + 1);
+        //     $stream = file_get_contents($url);
+        //     $new_url = uploadStreamByTos('demonstrate', $stream, $filename);
+        //     dd($new_url);
+        //     if ($new_url) {
+        //         $boolen = DB::table('mp_timbres')->where('id', $id)->update([
+        //             'audio_url' => $new_url,
+        //             'updated_at' => date('Y-m-d H:i:s'),
+        //         ]);
+        //         if ($boolen) $success++;
+        //         else $fail++;
+        //     }else {
+        //         dump('未生成新url');
+        //         $fail++;
+        //     }
+        // }
+        // dd("总条数:{$total}, 成功条数:{$success}, 跳过条数:{$continue}, 失败条数:{$fail}");
         exit();
         // $audio_effects = DB::table('mp_bgms')->where('is_enabled', 1)->where('playtime_seconds', 0)->get();
         // foreach ($audio_effects as $audio_effect) {

+ 33 - 1
app/Libs/Helpers.php

@@ -262,7 +262,7 @@ function randStr($num = 5)
 }
 
 /**
- * 上传文件(阿里云oss)
+ * 上传文件(火山云tos)
  *
  * @param $prefix   文件夹前缀
  * @param $file     文件二进制
@@ -298,6 +298,38 @@ function uploadFileByTos($prefix, $file, $filename='')
     return '';
 }
 
+/**
+ * 上传文件流(火山云tos)
+ *
+ * @param $prefix   文件夹前缀
+ * @param $file     文件二进制
+ * @param $filename 文件名(带后缀)
+ * @return mixed
+ */
+function uploadStreamByTos($prefix, $stream, $filename)
+{
+    try {
+        $client = new TosClient([
+            'region'    => env('VOLC_REGION'),
+            'endpoint'  => env('VOLC_END_POINT'),
+            'ak'        => env('VOLC_AK'),
+            'sk'        => env('VOLC_SK'),
+        ]);
+
+        $input = new PutObjectInput(env('VOLC_BUCKET'),  "$prefix/$filename", $stream);
+        $output = $client->putObject($input);
+        if ($output->getRequestId()) {
+            return "https://".env('VOLC_BUCKET').'.'.env('VOLC_END_POINT')."/$prefix/$filename";
+        }
+    } catch (TosClientException $ex) {
+        dLog('exception')->info('saveToTos', [$ex->getMessage()]);
+    } catch (TosServerException $ex) {
+        dLog('exception')->info('saveToTos', [$ex->getRequestId(), $ex->getStatusCode(), $ex->getErrorCode()]);
+    }
+
+    return '';
+}
+
 
 /**
  * 上传文件(阿里云oss)