package org.springblade.file.service.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.file.config.ObsProperties;
import org.springblade.file.exception.UnzipFailException;
import org.springblade.file.pojo.vo.FileHandleVO;
import org.springblade.file.pojo.vo.FileVO;
import org.springblade.file.service.IFileHandleService;
import org.springblade.file.service.IFileService;
import org.springblade.file.util.UnzipUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author bfhuange
 * @date 2024/10/22
 */
@Slf4j
@AllArgsConstructor
@Service
public class FileHandleServiceImpl implements IFileHandleService {

    private final IFileService fileService;
    private final ObsProperties obsProperties;

    @Override
    public FileHandleVO downloadAndUnzip(FileVO file) {
        File downloadDir = new File(obsProperties.getTmpDownloadDir());
        if (!downloadDir.exists()) {
            downloadDir.mkdirs();
        }
        File downloadFile = new File(downloadDir, file.getObjectKey());
        String downloadFilePath = downloadFile.getAbsolutePath();
        File unzipDir = null;
        long unzipStart = System.currentTimeMillis();
        try {
            // 下载文件
            downloadFile(file, downloadFile, downloadFilePath);
            // 2. 解压文件目录
            unzipDir = new File(obsProperties.getTmpUnzipDir(), file.getObjectKey());
            if (!unzipDir.exists()) {
                unzipDir.mkdirs();
            }
            long start = System.currentTimeMillis();
            List<File> files = UnzipUtil.unzipOrUnrar(downloadFilePath, unzipDir);
            long end = System.currentTimeMillis();
            log.info("文件：{} 解压完成 共{}个 耗时：{}", file.getObjectKey(), files.size(), end - start);
            FileHandleVO fileHandleVO = new FileHandleVO();
            fileHandleVO.setDownloadFile(downloadFile);
            fileHandleVO.setUnzippedDir(unzipDir);
            fileHandleVO.setUnzippedFiles(files);
            return fileHandleVO;
        } catch (Exception e) {
            log.error("文件解压失败异常", e);
            throw new UnzipFailException(e, unzipDir);
        } finally {
            long unzipEnd = System.currentTimeMillis();
            log.info("文件：{} 下载并解压完成 耗时：{}", file.getObjectKey(), unzipEnd - unzipStart);
        }
    }

    /**
     * 下载文件
     *
     * @param file
     * @param downloadFile
     * @param downloadFilePath
     */
    private void downloadFile(FileVO file, File downloadFile, String downloadFilePath) {
        if (downloadFile.exists() && file.getMd5() != null) {
            // 下载文件存在且md5不为空，可能是重新解压
            String fileMd5 = fileService.getFileMd5(downloadFile);
            if (StringUtil.equals(fileMd5, file.getMd5())) {
                log.info("文件存在且md5一致，无需重新下载 文件路径：{}", downloadFile.getAbsolutePath());
                return;
            }
        }
        long downloadStart = System.currentTimeMillis();
        try {
            // 1. 下载文件
            fileService.downloadFile(file.getObjectKey(), downloadFilePath);
        } finally {
            long downloadEnd = System.currentTimeMillis();
            log.info("文件：{} 下载完成 耗时：{}", file.getObjectKey(), downloadEnd - downloadStart);
        }
    }

}
