package ink.wgink.module.file.media.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.FileException;
import ink.wgink.module.file.media.service.IMediaService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MediaServiceImpl
 * @Description: 媒体
 * @Author: WangGeng
 * @Date: 2021/6/9 21:54
 * @Version: 1.0
 **/
@Service
public class MediaServiceImpl extends DefaultBaseService implements IMediaService {
    private static final char[] HEX_CODE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


    @Override
    public String upload(MultipartFile multipartFile, String uploadPath, String uploadName) {
        String fileMd5;
        File uploadFolderFile = new File(uploadPath);
        if (!uploadFolderFile.exists()) {
            uploadFolderFile.mkdirs();
        }

        try (InputStream uploadFileInputStream = multipartFile.getInputStream();
             FileOutputStream uploadFileOutputStream = new FileOutputStream(uploadPath + File.separator + uploadName);) {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            for (byte[] buf = new byte[INPUT_STREAM_SIZE]; uploadFileInputStream.read(buf) > -1; ) {
                uploadFileOutputStream.write(buf, 0, buf.length);
                messageDigest.update(buf, 0, buf.length);
            }
            uploadFileOutputStream.flush();
            // 计算MD5
            byte[] data = messageDigest.digest();
            StringBuilder fileMd5SB = new StringBuilder(data.length * 2);
            for (byte b : data) {
                fileMd5SB.append(HEX_CODE[(b >> 4) & 0xF]);
                fileMd5SB.append(HEX_CODE[(b & 0xF)]);
            }
            fileMd5 = fileMd5SB.toString();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new FileException("视频上传失败");
        }
        return fileMd5;
    }

    @Override
    public String getContentType(String fileType) {
        String contentType;
        switch (fileType) {
            case "png":
                contentType = "image/png";
                break;
            case "blob":
            case "jpg":
            case "jpeg":
                contentType = "image/jpeg";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "mp4":
                contentType = "video/mp4";
                break;
            case "rmvb":
                contentType = "application/vnd.rn-realmedia-vbr";
                break;
            case "mp3":
                contentType = "audio/mp3";
                break;
            case "wmv":
                contentType = "video/x-ms-wmv";
                break;
            case "wav":
                contentType = "audio/wav";
                break;
            case "doc":
                contentType = "application/msword";
                break;
            case "docx":
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                break;
            case "xls":
                contentType = "application/vnd.ms-excel";
                break;
            case "xlsx":
                contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case "ppt":
                contentType = "application/vnd.ms-powerpoint";
                break;
            case "pptx":
                contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                break;
            case "txt":
                contentType = "text/plain";
                break;
            case "apk":
                contentType = "application/vnd.android.package-archive";
                break;
            case "pdf":
                contentType = "application/pdf";
                break;
            default:
                contentType = "application/octet-stream";
        }
        return contentType;
    }

    @Override
    public String getFileType(String fileName) {
        String[] names = fileName.split("\\.");
        if (names != null) {
            return names[names.length - 1].toLowerCase();
        }
        return "";
    }
}
