派生自 projectDept/qhighschool

EricsHu
2023-11-23 bef4f6d51bff03c0512a75f43e79699b7296f1fa
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
120
121
122
123
124
125
126
127
package com.qxueyou.scc.sys.service.impl;
 
import java.io.File;
import java.io.InputStream;
 
import javax.annotation.PostConstruct;
 
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.impl.CommonAppService;
import com.qxueyou.scc.config.SccConfig;
import com.qxueyou.scc.sys.service.IOssVideoService;
 
/**
 *上传服务
 * 
 * @author 德虎
 * 
 */
@Service
public class VideoOssService extends CommonAppService implements IOssVideoService {
    private final  Logger log = LogManager.getLogger("VideoOssService");
    
    @Autowired
    SccConfig sccConfig;
    
    private static String resRootPath;
 
    /**
     * 初始化客户端
     */
    @PostConstruct
    private void init() {
        resRootPath =sccConfig.getResRootPath();
    }
 
 
    /**
     * 上传视频文件:
     */
    public Result uplpadVideoFile(File file, String destPath) {
        String fullPath = null;
        try {
            fullPath = resRootPath + destPath;
            File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/")));
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            FileUtils.copyFile(file, new File(fullPath));
            return new Result(true);
        } catch (Exception e) {
            log.error(e, e);
        }
        return new Result(false);
    }
    
    /**
     * 上传视频文件:
     */
    public Result uplpadVideoFile(InputStream input,String fileFormat,long fileLength, String destPath) {
        String fullPath = null;
        try {
            fullPath = (resRootPath + destPath).replace("//", "/");
            File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/")));
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            FileUtils.copyInputStreamToFile(input, new File(fullPath));
            return new Result(true);
        } catch (Exception e) {
            log.error(e, e);
        }finally{
            IOUtils.closeQuietly(input);
        }
        return new Result(false);
    }
    
    /*
     * (non-Javadoc)
     * @see com.etraining.qxueyou.sys.service.IOssService#getObject(java.lang.String, java.lang.String)
     */
    @Override
    public File getObject(String key, String directory) {
        File sourceFile = null;
        File targetFile = null;
        String fullPath = null;
        try {
            sourceFile = new File(resRootPath + key);
            if(sourceFile.exists()){
                fullPath = resRootPath + directory;
                File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/")));
                if(!fileRoot.exists()){
                    fileRoot.mkdirs();    
                }
                targetFile = new File(resRootPath + directory);
                FileUtils.copyFile(sourceFile, targetFile);
            }
        } catch (Exception e) {
            log.error(e, e);
        }
        return targetFile;
    }
    
    /**
     * 如果路径以 / 或 \ 开头,需要截取
     * @param destPath
     * @return
     */
    public String chopPath(String destPath){
        if(destPath.startsWith("/")||destPath.startsWith("\\")){
            return destPath.substring(1);
        }
        return destPath;
    }
    
    @Override
    public boolean delObject(String key) {
        FileUtils.deleteQuietly(new File(resRootPath + key));
        return true;
    }    
}