CopyImageFileJob.php 1.8 KB
<?php

namespace App\Jobs;

use App\Events\CopyImageFile;
use App\Models\File\File as FileModel;
use App\Models\File\Image as ImageModel;
use App\Services\AmazonS3Service;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CopyImageFileJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 3; // 可配置任务重试次数

    protected $param;

    /**
     * Create a new job instance.
     *
     * @param  CopyImageFile  $event
     * @return void
     */
    public function __construct($data)
    {
        $this->param = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $imageModel = new ImageModel();
        //获取当前项目的所有图片
        $imageList = $imageModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos','size']);
        if(!empty($imageList)){
            $amazonS3Service = new AmazonS3Service();
            foreach ($imageList as $k => $v){
                $amazonS3Service->syncImageFiles(getImageUrl($v['path']));
                $imageModel->edit(['is_cos'=>0],['id'=>$v['id']]);
            }
        }

        $fileModel = new FileModel();
        $fileList = $fileModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos']);
        if(!empty($fileList)){
            $amazonS3Service = new AmazonS3Service();
            foreach ($fileList as $k => $v){
                $amazonS3Service->syncImageFiles(getImageUrl($v['path']));
                $fileList->edit(['is_cos'=>0],['id'=>$v['id']]);
            }
        }
        return true;
    }
}