zxl
5 天以前 7e9972d25b3d035201331d401c8e1a74d8f2d8e8
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
118
119
package cn.lili.modules.lmk.service.impl;
 
import cn.lili.base.Result;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.StringUtils;
import cn.lili.cos.CosSTS;
import cn.lili.modules.lmk.domain.entity.LmkFile;
import cn.lili.modules.lmk.domain.entity.Video;
import cn.lili.modules.lmk.domain.form.FileInfoForm;
import cn.lili.modules.lmk.domain.vo.LmkFileVO;
import cn.lili.modules.lmk.mapper.LmkFileMapper;
import cn.lili.modules.lmk.mapper.VideoMapper;
import cn.lili.modules.lmk.service.LmkFileService;
import cn.lili.utils.COSUtil;
import cn.lili.utils.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
 
/**
 * 文件信息 服务实现类
 *
 * @author xp
 * @since 2025-05-19
 */
@Service
@RequiredArgsConstructor
public class LmkFileServiceImpl extends ServiceImpl<LmkFileMapper, LmkFile> implements LmkFileService {
 
    private final LmkFileMapper lmkFileMapper;
    private final COSUtil cosUtil;
 
    private final VideoMapper videoMapper;
    @Override
    public Result generateVideoCoverUrl(String videoId, Long snapshotTime, Integer width, Integer height){
        Video video = videoMapper.selectById(videoId);
        if (video != null){
            if (StringUtils.isNotBlank(video.getCoverUrl())){
                return Result.ok().data(cosUtil.getPreviewUrl(video.getCoverUrl()));
            }
            //获得videoKey
            try {
                System.out.println(video.getVideoFileKey());
                MultipartFile file =cosUtil.captureVideoCoverAsMultipart(cosUtil.getPreviewUrl(video.getVideoFileKey()), width, height);
                LmkFileVO fileVo = (LmkFileVO) this.uploadObject(file).get("data");
                video.setCoverUrl(fileVo.getFileKey());
                videoMapper.updateById(video);
                return Result.ok().data(fileVo.getUrl());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
 
        }
        return Result.ok();
    }
 
    @Override
    public Result uploadObject(MultipartFile file) {
        LmkFile fileInfo = FileUtil.getFileInfo(file);
        try {
            cosUtil.upload(file.getInputStream(), fileInfo);
            lmkFileMapper.insert(fileInfo);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("上传异常,请重试");
        }
        String url = cosUtil.getPreviewUrl(fileInfo.getFileKey());
        LmkFileVO fileVo = new LmkFileVO().setUrl(url).setFileKey(fileInfo.getFileKey());
        return Result.ok("上传成功").data(fileVo);
    }
 
    @Override
    public Result uploadObjects(List<MultipartFile> files) {
        return null;
    }
 
    @Override
    public Result deleteObject(String fileKey) {
        cosUtil.deleteFile(fileKey);
        return Result.ok("删除成功");
    }
 
    @Override
    public Result deleteObjects(List<String> fileKeys) {
        cosUtil.deleteFiles(fileKeys);
        return Result.ok("删除成功");
    }
 
    @Override
    public void downloadObject(String fileKey, HttpServletResponse response) {
        cosUtil.download(fileKey, response);
    }
 
    @Override
    public String getPreviewUrl(String fileKey) {
        return cosUtil.getPreviewUrl(fileKey);
    }
 
    @Override
    public CosSTS getSTSToken() {
        return cosUtil.getSTS();
    }
 
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addByForm(FileInfoForm fileInfo) {
        LmkFile file = new LmkFile();
        BeanUtils.copyProperties(fileInfo, file);
        baseMapper.insert(file);
    }
}