Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\Models\Entities\Cw\Magazine;
  6. use App\Models\Entities\MagazineTransfer;
  7. use Log;
  8. use GuzzleHttp;
  9. use Illuminate\Console\Command;
  10. use App\Repositories\ArticleTransferRepository;
  11. use App\Repositories\MagazineTransferRepository;
  12. use App\Repositories\Cw;
  13. use App\Repositories\Cheers;
  14. use App\Traits\CustomLogTrait;
  15.  
  16. class ArticleTransfer extends Command
  17. {
  18. protected $description = 'transfer:ArticleTransfer';
  19. protected $signature = 'transfer:ArticleTransfer {base_path?} {keep_files?}';
  20.  
  21. use CustomLogTrait;
  22.  
  23. public function handle()
  24. {
  25. file_put_contents('/tmp/123456',date("h:i:sa")."\n",FILE_APPEND);
  26. $this->setCustomLog();
  27.  
  28. $base_path = empty($this->argument('base_path')) ? 'article_transfer' : $this->argument('base_path');
  29. $keep_files = empty($this->argument('keep_files')) ? false : true;
  30.  
  31. //TODO
  32. $this->downloadGcsSrc($base_path);
  33.  
  34. $src_folder = 'src';
  35. $proccess_folder = 'magazine/proccess';
  36. $magazine_transfer_repo = new MagazineTransferRepository();
  37. $magazine_transfer_repo->setGcsPath();
  38. $magazine_transfer_repo->setGcsFolder($base_path);
  39. $magazine_transfer_repo->setKeepFiles($keep_files);
  40. $output = new \Symfony\Component\Console\Output\ConsoleOutput();
  41. $magazine_transfer_repo->setOutput($output);
  42. Log::info("--- Paper Magazines Start ---");
  43. $magazine_transfer_repo->setBasePath(storage_path() . '/app/' . $base_path);
  44. $magazine_transfer_repo->setSrcFolder($src_folder);
  45. $magazine_transfer_repo->setProccessFolder($proccess_folder);
  46. $magazines = $magazine_transfer_repo->getMagazineByDir();
  47. Log::info("--- Paper Magazines End ---");
  48.  
  49. Log::info("--- Paper Articles Start ---");
  50. $src_folder = 'src';
  51. $proccess_folder = 'article/proccess';
  52. $finish_folder = "article/finish/" . date('Ym') . "/" . date('YmdHi');
  53. $article_transfer_repo = new ArticleTransferRepository();
  54. $article_transfer_repo->setGcsPath();
  55. $article_transfer_repo->setGcsFolder($base_path);
  56. $article_transfer_repo->setKeepFiles($keep_files);
  57. $article_transfer_repo->setBasePath(storage_path() . '/app/' . $base_path);
  58. $article_transfer_repo->setSrcFolder($src_folder);
  59. $article_transfer_repo->setProccessFolder($proccess_folder);
  60. $article_transfer_repo->setFinishFolder($finish_folder);
  61. $article_transfer_repo->setMagazines($magazines);
  62. $article_transfer_repo->setOutput($output);
  63. $articles = $article_transfer_repo->getArticlesByDir();
  64. Log::info("--- Paper Articles Done ---");
  65.  
  66. Log::info("--- Paper Magazines Publish Start ---");
  67. foreach ($magazines as $magazine) {
  68. $article_repo = null;
  69.  
  70. switch ($magazine->website) {
  71. case 'C':
  72. $magazine_repo = new Cw\MagazineRepository();
  73. break;
  74. case 'J':
  75. $magazine_repo = new Cheers\MagazineRepository();
  76. break;
  77. }
  78. Log::info("publish magazine:{$magazine->idMagazine},{$magazine->magazinePeriod}");
  79. $magazine_repo->magazineClone($magazine->idMagazine);
  80. }
  81. Log::info("--- Paper Magazines Publish End ---");
  82.  
  83. $this->info('all done');
  84.  
  85. // 傳email通知
  86. Log::info("Mail To :" . print_r($this->getMailTo(), true));
  87. if (env('APP_ENV') == 'product') {
  88. $message = '報表如附件';
  89. $this->mail($message, $this->custom_log_name, $this->custom_log_path);
  90. }
  91. }
  92.  
  93. /*
  94. * 將gcs上下載到local
  95. */
  96. private function downloadGcsSrc($base_path)
  97. {
  98. Log::info("--- Download GCS src Start ---");
  99.  
  100. $file_system = new \Illuminate\Filesystem\Filesystem();
  101. $storage_path = storage_path() . '/app/';
  102. $dir_ary = [];
  103.  
  104. $gcs_src_path = "{$base_path}/src";
  105. $gcs_src_lists = $this->listGcs($gcs_src_path);
  106. $count = count($gcs_src_lists);
  107.  
  108. $this->line("base_path:{$base_path}");
  109. $this->line("gcs_src_path:{$gcs_src_path}");
  110. Log::info("base_path {$base_path}");
  111. Log::info("gcs_src_path {$gcs_src_path}");
  112.  
  113. Log::info("src total {$count}");
  114.  
  115. foreach ($gcs_src_lists as $list) {
  116. if ($list->type == 'file') {
  117. if (!$file_system->exists($storage_path . $list->dirname)) {
  118. $file_system->makeDirectory($storage_path . $list->dirname, 0777, true);
  119. }
  120.  
  121. if (!$file_system->exists($storage_path . $list->path)) {
  122. $file = $this->downloadGcs($list->path);
  123. $response = $file_system->put($storage_path . $list->path, $file);
  124.  
  125. Log::info("Download File {$list->path}");
  126.  
  127. if ($response) {
  128. $this->deleteFileGcs($list->path);
  129. Log::info("Delete File {$list->path}");
  130. }
  131. }
  132. } else {
  133. if (!in_array($list->path, $dir_ary)) {
  134. $dir_ary[] = $list->path;
  135. }
  136. }
  137. }
  138.  
  139. Log::info("--- Download GCS src End ---");
  140.  
  141. Log::info("--- Delete GCS src Start ---");
  142. foreach (array_reverse($dir_ary) as $dir) {
  143. $this->deleteDirGcs($dir);
  144. }
  145. Log::info("--- Delete GCS src End ---");
  146. }
  147.  
  148. private function listGcs($file_name)
  149. {
  150. $bucket = env('CMS_BUCKET_NAME');
  151. $client = new GuzzleHttp\Client();
  152. $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/list/files';
  153.  
  154. try {
  155. $json = $client->request('POST', $url, [
  156. 'form_params' => [
  157. 'bucket' => $bucket,
  158. 'file_name' => $file_name,
  159. ],
  160. ]);
  161.  
  162. $res = json_decode(($json->getBody()->getContents()));
  163. if ($res->success) {
  164. Log::info("{$file_name} get files list success");
  165. }
  166. } catch (Exception $e) {
  167. Log::error("{$file_name} get files list fail:" . $e);
  168. }
  169. return $res->data;
  170. }
  171.  
  172. private function downloadGcs($file_name)
  173. {
  174. $bucket = env('CMS_BUCKET_NAME');
  175. $client = new GuzzleHttp\Client();
  176. $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/download/file';
  177.  
  178. try {
  179. $json = $client->request('POST', $url, [
  180. 'form_params' => [
  181. 'bucket' => $bucket,
  182. 'file_name' => $file_name,
  183. 'real_file_name' => $file_name
  184. ],
  185. ]);
  186.  
  187. $file = $json->getBody()->getContents();
  188. Log::info("{$file_name} download file list success");
  189. } catch (Exception $e) {
  190. Log::error("{$file_name} download file list fail:" . $e);
  191. }
  192. return $file;
  193. }
  194.  
  195. private function deleteFileGcs($file_name)
  196. {
  197. $bucket = env('CMS_BUCKET_NAME');
  198. $client = new GuzzleHttp\Client();
  199. $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/delete/file';
  200.  
  201. try {
  202. $json = $client->request('DELETE', $url, [
  203. 'form_params' => [
  204. 'bucket' => $bucket,
  205. 'file_name' => $file_name,
  206. ],
  207. ]);
  208.  
  209. $res = json_decode(($json->getBody()->getContents()));
  210. if ($res->success) {
  211. Log::info("{$file_name} delete file list success");
  212. }
  213. } catch (Exception $e) {
  214. Log::error("{$file_name} delete file list fail:" . $e);
  215. }
  216. return $res;
  217. }
  218.  
  219. private function deleteDirGcs($file_name)
  220. {
  221. $bucket = env('CMS_BUCKET_NAME');
  222. $client = new GuzzleHttp\Client();
  223. $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/delete/dir';
  224.  
  225. try {
  226. $this->info("{$file_name} delete dir");
  227.  
  228. $json = $client->request('DELETE', $url, [
  229. 'form_params' => [
  230. 'bucket' => $bucket,
  231. 'file_name' => $file_name,
  232. ],
  233. ]);
  234.  
  235. $res = json_decode(($json->getBody()->getContents()));
  236. if ($res->success) {
  237. Log::info("{$file_name} delete dir success");
  238. }
  239. } catch (Exception $e) {
  240. Log::error("{$file_name} delete dir fail:" . $e);
  241. }
  242. return $res;
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement