package com.file_upload.service.Impl;


import com.file_upload.domain.VideoUploadResult;
import com.file_upload.service.IStorageService;
import com.file_upload.utils.AliyunOSSUtil;
import com.file_upload.utils.DatePattern;
import com.file_upload.utils.DateUtils;
import lombok.extern.log4j.Log4j2;
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 java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * OSS存储服务
 *
 * @author chenpengfei
 */
@Service
@Log4j2
public class StorageServiceImpl implements IStorageService {

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Value("${oss.filepath}")
    private String filePath;

    @Value("${oss.filehost}")
    String fileHost;

    @Override
    public String store(MultipartFile file) {

        log.info("文件上传");
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        try {
            if (file != null) {
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);

                    // 根据不同文件文件类型-不同日期生成不同文件夹
                    Date date = new Date();
                    String datePath = DateUtils.formatDateByStyle(date, DatePattern.CN_DATE_BASIC_STYLE4.getDatePattern());

                    String timeStamp = String.valueOf(System.currentTimeMillis());
                    filename = timeStamp + filename.substring(filename.lastIndexOf("."), filename.length());

                    String path;
                    if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".png")) {
                        // images
                        path = filePath + "images/" + datePath + "/" + filename;
                    } else if (filename.endsWith(".mp4")) {
                        // video
                        path = filePath + "images/" + datePath + "/" + filename;
                    } else {
                        path = filePath + "other/" + datePath + "/" + filename;
                    }
                    // 上传到OSS
                    String uploadUrl = aliyunOSSUtil.upLoad(newFile, path);
                    newFile.delete();
                    if (uploadUrl != null) {
                        return uploadUrl;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }

        return null;
    }

    @Override
    public VideoUploadResult storeVideo(MultipartFile file) {
        log.info("视频上传");
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        try {
            if (file != null) {
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);

                    // 根据不同文件文件类型-不同日期生成不同文件夹
                    Date date = new Date();
                    String datePath = DateUtils.formatDateByStyle(date, DatePattern.CN_DATE_BASIC_STYLE4.getDatePattern());

                    String timeStamp = String.valueOf(System.currentTimeMillis());
                    filename = timeStamp + filename.substring(filename.lastIndexOf("."), filename.length());

                    String path;
                    if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".png")) {
                        // images
                        path = filePath + "images/" + datePath + "/" + filename;
                    } else if (filename.endsWith(".mp4")) {
                        // video
                        path = filePath + "video/" + datePath + "/" + filename;
                    } else {
                        path = filePath + "other/" + datePath + "/" + filename;
                    }
                    // 上传到OSS
                    VideoUploadResult upload = aliyunOSSUtil.videoUpload(newFile, path);
                    if (upload != null) {
                        upload.setVideoUrl(fileHost + upload.getVideoUrl());
                        upload.setSignedUrl(fileHost + upload.getSignedUrl());
                    }
                    newFile.delete();
                    if (upload != null) {
                        return upload;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }

        return null;
    }
}
