Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use App\Models\Entities\Cw\Magazine;
- use App\Models\Entities\MagazineTransfer;
- use Log;
- use GuzzleHttp;
- use Illuminate\Console\Command;
- use App\Repositories\ArticleTransferRepository;
- use App\Repositories\MagazineTransferRepository;
- use App\Repositories\Cw;
- use App\Repositories\Cheers;
- use App\Traits\CustomLogTrait;
- class ArticleTransfer extends Command
- {
- protected $description = 'transfer:ArticleTransfer';
- protected $signature = 'transfer:ArticleTransfer {base_path?} {keep_files?}';
- use CustomLogTrait;
- public function handle()
- {
- file_put_contents('/tmp/123456',date("h:i:sa")."\n",FILE_APPEND);
- $this->setCustomLog();
- $base_path = empty($this->argument('base_path')) ? 'article_transfer' : $this->argument('base_path');
- $keep_files = empty($this->argument('keep_files')) ? false : true;
- //TODO
- $this->downloadGcsSrc($base_path);
- $src_folder = 'src';
- $proccess_folder = 'magazine/proccess';
- $magazine_transfer_repo = new MagazineTransferRepository();
- $magazine_transfer_repo->setGcsPath();
- $magazine_transfer_repo->setGcsFolder($base_path);
- $magazine_transfer_repo->setKeepFiles($keep_files);
- $output = new \Symfony\Component\Console\Output\ConsoleOutput();
- $magazine_transfer_repo->setOutput($output);
- Log::info("--- Paper Magazines Start ---");
- $magazine_transfer_repo->setBasePath(storage_path() . '/app/' . $base_path);
- $magazine_transfer_repo->setSrcFolder($src_folder);
- $magazine_transfer_repo->setProccessFolder($proccess_folder);
- $magazines = $magazine_transfer_repo->getMagazineByDir();
- Log::info("--- Paper Magazines End ---");
- Log::info("--- Paper Articles Start ---");
- $src_folder = 'src';
- $proccess_folder = 'article/proccess';
- $finish_folder = "article/finish/" . date('Ym') . "/" . date('YmdHi');
- $article_transfer_repo = new ArticleTransferRepository();
- $article_transfer_repo->setGcsPath();
- $article_transfer_repo->setGcsFolder($base_path);
- $article_transfer_repo->setKeepFiles($keep_files);
- $article_transfer_repo->setBasePath(storage_path() . '/app/' . $base_path);
- $article_transfer_repo->setSrcFolder($src_folder);
- $article_transfer_repo->setProccessFolder($proccess_folder);
- $article_transfer_repo->setFinishFolder($finish_folder);
- $article_transfer_repo->setMagazines($magazines);
- $article_transfer_repo->setOutput($output);
- $articles = $article_transfer_repo->getArticlesByDir();
- Log::info("--- Paper Articles Done ---");
- Log::info("--- Paper Magazines Publish Start ---");
- foreach ($magazines as $magazine) {
- $article_repo = null;
- switch ($magazine->website) {
- case 'C':
- $magazine_repo = new Cw\MagazineRepository();
- break;
- case 'J':
- $magazine_repo = new Cheers\MagazineRepository();
- break;
- }
- Log::info("publish magazine:{$magazine->idMagazine},{$magazine->magazinePeriod}");
- $magazine_repo->magazineClone($magazine->idMagazine);
- }
- Log::info("--- Paper Magazines Publish End ---");
- $this->info('all done');
- // 傳email通知
- Log::info("Mail To :" . print_r($this->getMailTo(), true));
- if (env('APP_ENV') == 'product') {
- $message = '報表如附件';
- $this->mail($message, $this->custom_log_name, $this->custom_log_path);
- }
- }
- /*
- * 將gcs上下載到local
- */
- private function downloadGcsSrc($base_path)
- {
- Log::info("--- Download GCS src Start ---");
- $file_system = new \Illuminate\Filesystem\Filesystem();
- $storage_path = storage_path() . '/app/';
- $dir_ary = [];
- $gcs_src_path = "{$base_path}/src";
- $gcs_src_lists = $this->listGcs($gcs_src_path);
- $count = count($gcs_src_lists);
- $this->line("base_path:{$base_path}");
- $this->line("gcs_src_path:{$gcs_src_path}");
- Log::info("base_path {$base_path}");
- Log::info("gcs_src_path {$gcs_src_path}");
- Log::info("src total {$count}");
- foreach ($gcs_src_lists as $list) {
- if ($list->type == 'file') {
- if (!$file_system->exists($storage_path . $list->dirname)) {
- $file_system->makeDirectory($storage_path . $list->dirname, 0777, true);
- }
- if (!$file_system->exists($storage_path . $list->path)) {
- $file = $this->downloadGcs($list->path);
- $response = $file_system->put($storage_path . $list->path, $file);
- Log::info("Download File {$list->path}");
- if ($response) {
- $this->deleteFileGcs($list->path);
- Log::info("Delete File {$list->path}");
- }
- }
- } else {
- if (!in_array($list->path, $dir_ary)) {
- $dir_ary[] = $list->path;
- }
- }
- }
- Log::info("--- Download GCS src End ---");
- Log::info("--- Delete GCS src Start ---");
- foreach (array_reverse($dir_ary) as $dir) {
- $this->deleteDirGcs($dir);
- }
- Log::info("--- Delete GCS src End ---");
- }
- private function listGcs($file_name)
- {
- $bucket = env('CMS_BUCKET_NAME');
- $client = new GuzzleHttp\Client();
- $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/list/files';
- try {
- $json = $client->request('POST', $url, [
- 'form_params' => [
- 'bucket' => $bucket,
- 'file_name' => $file_name,
- ],
- ]);
- $res = json_decode(($json->getBody()->getContents()));
- if ($res->success) {
- Log::info("{$file_name} get files list success");
- }
- } catch (Exception $e) {
- Log::error("{$file_name} get files list fail:" . $e);
- }
- return $res->data;
- }
- private function downloadGcs($file_name)
- {
- $bucket = env('CMS_BUCKET_NAME');
- $client = new GuzzleHttp\Client();
- $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/download/file';
- try {
- $json = $client->request('POST', $url, [
- 'form_params' => [
- 'bucket' => $bucket,
- 'file_name' => $file_name,
- 'real_file_name' => $file_name
- ],
- ]);
- $file = $json->getBody()->getContents();
- Log::info("{$file_name} download file list success");
- } catch (Exception $e) {
- Log::error("{$file_name} download file list fail:" . $e);
- }
- return $file;
- }
- private function deleteFileGcs($file_name)
- {
- $bucket = env('CMS_BUCKET_NAME');
- $client = new GuzzleHttp\Client();
- $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/delete/file';
- try {
- $json = $client->request('DELETE', $url, [
- 'form_params' => [
- 'bucket' => $bucket,
- 'file_name' => $file_name,
- ],
- ]);
- $res = json_decode(($json->getBody()->getContents()));
- if ($res->success) {
- Log::info("{$file_name} delete file list success");
- }
- } catch (Exception $e) {
- Log::error("{$file_name} delete file list fail:" . $e);
- }
- return $res;
- }
- private function deleteDirGcs($file_name)
- {
- $bucket = env('CMS_BUCKET_NAME');
- $client = new GuzzleHttp\Client();
- $url = env('API_STORAGE_URL') . '/api/cw-cms-cwg-tw/delete/dir';
- try {
- $this->info("{$file_name} delete dir");
- $json = $client->request('DELETE', $url, [
- 'form_params' => [
- 'bucket' => $bucket,
- 'file_name' => $file_name,
- ],
- ]);
- $res = json_decode(($json->getBody()->getContents()));
- if ($res->success) {
- Log::info("{$file_name} delete dir success");
- }
- } catch (Exception $e) {
- Log::error("{$file_name} delete dir fail:" . $e);
- }
- return $res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement