Files
file-online-preview/jodconverter-web/src/main/java/cn/keking/service/impl/PictureFilePreviewImpl.java
2020-05-18 15:55:26 +08:00

58 lines
2.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.keking.service.impl;
import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.context.request.RequestContextHolder;
import java.util.List;
/**
* Created by kl on 2018/1/17.
* Content :图片文件处理
*/
@Service
public class PictureFilePreviewImpl implements FilePreview {
@Autowired
FileUtils fileUtils;
@Autowired
DownloadUtils downloadUtils;
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
List<String> imgUrls = Lists.newArrayList(url);
try {
imgUrls.clear();
imgUrls.addAll(fileUtils.getImgCache(fileKey));
} catch (Exception e){
imgUrls = Lists.newArrayList(url);
}
// 不是http开头浏览器不能直接访问需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) {
model.addAttribute("fileType", fileAttribute.getSuffix());
model.addAttribute("msg", response.getMsg());
return "fileNotSupported";
} else {
String file = fileUtils.getRelativePath(response.getContent());
model.addAttribute("imgurls", Lists.newArrayList(file));
model.addAttribute("currentUrl", file);
}
} else {
model.addAttribute("imgurls", imgUrls);
model.addAttribute("currentUrl", url);
}
return "picture";
}
}