package com.xiaoniu.subsystem.strategy;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.xiaoniu.subsystem.strategy.bean.FileObj;
import lombok.extern.slf4j.Slf4j;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class TarAnalysisStrategy implements UnZipStrategy {

    @Override
    public List<FileObj> analysis(InputStream inputStream) {
        List<FileObj> list=new ArrayList<>();
        TarInputStream tarInputStream = new TarInputStream(inputStream, 1024 * 2);
        TarEntry entry;
        try {
            while ((entry = tarInputStream.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    String name = entry.getName();
                    log.info("解析文件：{}", name);
                    FileObj fileObj = new FileObj();
                    fileObj.setDocType(1);
                    fileObj.setFilePath(name);
                    //后缀
                    //先判断name中是否包含/（路径分隔符）
                    int index2 = name.lastIndexOf("/");
                    if(index2==-1){
                        int index = name.lastIndexOf(".");
                        if (index!=-1) {
                            String suffix = name.substring(index + 1);
                            fileObj.setSuffix(suffix);
                        }
                    }else {
                        //截取最后的/到最后一个字符
                        String sub = name.substring(name.lastIndexOf("/"));
                        int index = sub.lastIndexOf(".");
                        if (index!=-1) {
                            String suffix = sub.substring(index + 1);
                            fileObj.setSuffix(suffix);
                        }
                    }
                    log.info("suffix:{}", fileObj.getSuffix());
                    //原文档名称
                    int index1 = name.lastIndexOf("/");
                    String docName = name.substring(index1 + 1);
                    log.info("docName:{}", docName);
                    fileObj.setDocName(docName);
                    //上传后的文件名字
                    String uuid = IdUtil.fastSimpleUUID();
                    StringBuilder fileName=new StringBuilder(uuid);
                    if (StrUtil.isNotBlank(fileObj.getSuffix())) {
                        fileName.append(".").append(fileObj.getSuffix());
                    }
                    fileObj.setFileName(fileName.toString());
                    log.info("fileName:{}",fileName);
                    //解压到本地临时存储路径
                    StringBuilder localPath=new StringBuilder();
                    String userDir = System.getProperty("user.dir");
                    localPath.append(userDir).append("/").append(fileObj.getFileName());
                    fileObj.setLocalPath(localPath.toString());
                    log.info("解压文件在本地临时路径：{}",localPath);
                    FileOutputStream fileOutputStream = new FileOutputStream(localPath.toString());
                    int count;
                    byte data[] = new byte[2048];
                    fileObj.setBytes(data);
                  /*  while ((count = tarInputStream.read(data)) != -1) {
                        fileOutputStream.write(data, 0, count);
                    }
                    fileOutputStream.close();*/
                    list.add(fileObj);
                }
            }
            inputStream.close();
            tarInputStream.close();
            log.info("完成解压文件");
        } catch (IOException e) {
            log.error("tar格式压缩文件在解压时发生错误：",e);
            throw new RuntimeException(e);
        }
        return list;
    }
}
