qirong
2023-12-22 ac2873bd37eeb496b0e9dd62d66e9fc4b38ef39b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package org.dromara.system.sync;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.oss.core.OssClient;
import org.dromara.common.oss.entity.SynchronousRequest;
import org.dromara.common.oss.entity.SynchronousRequest2;
import org.dromara.common.oss.entity.UploadResult;
import org.dromara.common.oss.entity.VideoRequest;
import org.dromara.common.oss.factory.OssFactory;
import org.dromara.system.domain.SysOss;
import org.dromara.system.domain.properties.Boundary;
import org.dromara.system.domain.properties.FtpConfig;
import org.dromara.system.mapper.SysOssMapper;
import org.dromara.system.uitil.FtpApche;
import org.dromara.system.uitil.HttpUtils;
import org.dromara.system.uitil.PasswordUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
@Component
public class VideoPulSync {
 
    private final Boundary boundary;
    private final FtpConfig ftpConfig;
    private final SysOssMapper baseMapper;
 
    public VideoPulSync(Boundary boundary, FtpConfig ftpConfig, SysOssMapper baseMapper) {
        this.boundary = boundary;
        this.ftpConfig = ftpConfig;
        this.baseMapper = baseMapper;
    }
 
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void get() throws IOException {
        System.out.println("同步上传......");
 
        List<String> list = FtpApche.downloadList(ftpConfig);
        if(list.size() == 0){
            return;
        }
        for (String str : list) {
            //密码
            String password = str.substring(0,6);
            //文件名称
            String fileName = str.substring(6,str.length());
            InputStream input = FtpApche.downloadFileFromDailyDir(str);
            byte[] fileBytesByName = FtpApche.getFileBytesByName(input);
            OssClient storage = OssFactory.instance();
            UploadResult uploadResult = storage.upload(input,getPath(fileName),fileName.substring(14,fileName.length()));
            FtpApche.deleteFile(str);
            MultipartFile file = new MockMultipartFile(fileName,fileName, fileName.substring(14,fileName.length()), input);
            buildResultEntity(fileName, fileName.substring(14,fileName.length()), "minio", uploadResult, file,
                password, fileBytesByName);
 
        }
 
    }
 
    @NotNull
    private void buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult
        , MultipartFile file, String password, byte[] fileBytesByName) throws IOException {
        SysOss oss = new SysOss();
        oss.setUrl(uploadResult.getUrl());
        oss.setFileSuffix(suffix);
        oss.setFileName(uploadResult.getFilename());
        oss.setOriginalName(originalfileName);
        oss.setService(configKey);
        oss.setPassword(password);
        baseMapper.insert(oss);
        //拼接同步信息
 
 
        SynchronousRequest request = new SynchronousRequest();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        request.setFile(file);
        request.setFileName(oss.getOriginalName());
        request.setPath(uploadResult.getFilename());
        request.setCreateTime(format.format(oss.getCreateTime()));
        request.setPassword(oss.getPassword());
        request.setCreateBy("1731588854831022081");
        request.setOssId(String.valueOf(oss.getOssId()));
//        request.setOssId("999999900");
        post(request, fileBytesByName);
    }
 
    private String post(SynchronousRequest request, byte[] bytes) throws IOException {
        HashMap<String, String> headers = new HashMap<>(3);
        String requestUrl = boundary.getVideo() +  "/resource/synchronization/upload";
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        headers.put("content-type", "application/json");
        // 发送post请求
        String resultData = HttpUtils.sendPostRequest2(requestUrl, request, bytes);
        // 并接收返回结果
        System.out.println(resultData);
        return resultData;
    }
 
    public String getPath(String suffix) {
        return DateUtils.datePath() + "/" + suffix;
    }
 
 
}