package org.jeecg.modules.fileupload.service.impl;

import cn.com.ctop.common.module.utils.FileEntity;
import cn.com.ctop.common.module.utils.FileUploadTool;
import cn.com.ctop.common.module.utils.OSSUtils;
import cn.com.ctop.common.module.entity.FileInfo;
import cn.com.ctop.common.module.service.IFileInfoService;
import org.apache.commons.codec.digest.DigestUtils;
import org.jeecg.modules.fileupload.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jeecg-boot
 * 2019年12月5日19:57:58
 */
@Service
public class UploadServiceImpl implements IUploadService {
    @Value("${jeecg.path.video-upload}")
    private String videoUpLoadPath;
    @Value("${jeecg.path.image-upload}")
    private String imageUpLoadPath;
    @Autowired
    private IFileInfoService fileInfoService;

    @Override
    public Map<String, Object> imageUpload(MultipartFile multipartFile, HttpServletRequest request, String type) {
        Map<String, Object> resultMap = new HashMap<>();
        //1: 文件上传至服务器
        FileUploadTool fileUploadTool = new FileUploadTool();
        FileEntity entity;
        entity = fileUploadTool.createFile(multipartFile, request, imageUpLoadPath);
        //2：获取MD5值
        String md5Hex = null;
        try {
            md5Hex = DigestUtils.md5Hex(new FileInputStream(entity.getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3: 根据MD5值查询是否已经上传过服务器
        List<FileInfo> getFileInfo = fileInfoService.getFileInfoByMd5(md5Hex);
        if (null != getFileInfo && getFileInfo.size() > 0) {
            //表示已经上传过服务器
            resultMap.put("message", "文件上传成功");
            resultMap.put("success", true);
            resultMap.put("fileInfo", getFileInfo.get(0));
        } else {
            //未上传过服务器
            String url = OSSUtils.uploadObject2Oss(new File(entity.getPath()));
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileName(entity.getTitleOrig() + entity.getType());
            fileInfo.setPath(entity.getPath());
            fileInfo.setType("IMAGE");
            fileInfo.setFileUrl(url);
            fileInfo.setMd5(md5Hex);
            fileInfo.setPlatformType(type);
            fileInfoService.save(fileInfo);
            resultMap.put("message", "文件上传成功");
            resultMap.put("success", true);
            resultMap.put("fileInfo", fileInfo);
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> videoUpload(MultipartFile multipartFile, HttpServletRequest request, String type) {
        Map<String, Object> resultMap = new HashMap<>();
        String savaPath = "D:\\upload\\video";
        FileUploadTool fileUploadTool = new FileUploadTool();
        FileEntity entity;
        entity = fileUploadTool.createFile(multipartFile, request, savaPath);
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileName(entity.getTitleOrig() + entity.getType());
        fileInfo.setPath(entity.getPath());
        fileInfo.setType("VIDEO");
        fileInfo.setFileUrl("http://photocdn.sohu.com/20111207/Img328215620.jpg");
        fileInfo.setPlatformType(type);
        fileInfoService.save(fileInfo);
        resultMap.put("message", "文件上传成功");
        resultMap.put("success", true);
        resultMap.put("fileInfo", fileInfo);
        return resultMap;
    }
}
