package com.fssi.pdfconvert.service.pdfpreview.impl;
import com.fssi.framework.common.utils.CommonUtil;
import com.fssi.pdfconvert.config.ConfigConstants;
import com.fssi.pdfconvert.model.ReturnResponse;
import com.fssi.pdfconvert.model.pdfpreview.FileAttributeMsg;
import com.fssi.pdfconvert.service.pdfpreview.PdfPreviewService;
import com.fssi.pdfconvert.utils.DownloadUtils;
import com.fssi.pdfconvert.utils.FileUtils;
import com.fssi.pdfconvert.utils.OfficeToPdf;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
@Service
public class PdfPreviewServiceImpl implements PdfPreviewService {
@Resource
private FileUtils fileUtils;
@Resource
private DownloadUtils downloadUtils;
@Resource
private OfficeToPdf officeToPdf;
private static final String FILE_DIR = ConfigConstants.getFileDir();
@Override
public Map<String,Object> filePreviewHandle(FileAttributeMsg fileAttributeMsg) {
Map<String,Object> kmap=new HashMap<String,Object>();
kmap.put("success",true);
InputStream is =null;
try {
String suffix=fileAttributeMsg.getSuffix();
String fileName=fileAttributeMsg.getFileName();
String base64str="";
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
String senfType=fileAttributeMsg.getSuffix();
String fileType = (isHtml ? "html" : "pdf");
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + fileType;
String outFilePath = FILE_DIR + pdfName;
kmap.put("fileName",pdfName);
kmap.put("fileType",fileType);
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath;
ReturnResponse<String> response = downloadUtils.downLoadbase64(fileAttributeMsg, null);
Thread.sleep(500);
if (0 != response.getCode()) {
kmap.put("success",false);
kmap.put("msg",response.getMsg());
return kmap;
}
filePath = response.getContent();
if (StringUtils.hasText(outFilePath) && !"pdf".equals(senfType.toLowerCase())) {
officeToPdf.openOfficeToPDF(filePath, outFilePath);
Thread.sleep(1000);
if (isHtml) {
fileUtils.doActionConvertedFile(outFilePath);
Thread.sleep(500);
}
if (ConfigConstants.isCacheEnabled()) {
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
}
is = new FileInputStream(outFilePath);
base64str= CommonUtil.toByteString(is);
kmap.put("base64str",base64str);
}
}else{
if("pdf".equals(senfType.toLowerCase())){
kmap.put("base64str",fileAttributeMsg.getBase64Str());
}else{
is = new FileInputStream(outFilePath);
base64str= CommonUtil.toByteString(is);
kmap.put("base64str",base64str);
}
}
if(isHtml){
FileAttributeMsg kfileAttributeMsg=new FileAttributeMsg();
kfileAttributeMsg.setBase64Str((String)kmap.get("base64str"));
kfileAttributeMsg.setSuffix(fileType);
kfileAttributeMsg.setFileName(pdfName);
filePreviewHandle(kfileAttributeMsg);
}
}catch (Exception e) {
e.printStackTrace();
kmap.put("success",false);
kmap.put("msg","出现异常,"+e.getMessage());
return kmap;
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return kmap;
}
}