2018-01-17 17:51:53 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
2020-05-14 16:22:48 +08:00
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
2020-05-14 16:22:48 +08:00
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
2020-05-14 16:22:48 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
DownloadUtils downloadUtils;
|
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
|
@Override
|
2019-06-17 14:21:16 +08:00
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2020-04-14 11:17:07 +08:00
|
|
|
|
String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
|
2020-05-15 18:09:19 +08:00
|
|
|
|
List<String> imgUrls = Lists.newArrayList(url);
|
2020-05-14 16:22:48 +08:00
|
|
|
|
try {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
imgUrls.clear();
|
2020-05-12 11:00:20 +08:00
|
|
|
|
imgUrls.addAll(fileUtils.getImgCache(fileKey));
|
2020-05-14 16:22:48 +08:00
|
|
|
|
} catch (Exception e){
|
2018-01-17 17:51:53 +08:00
|
|
|
|
imgUrls = Lists.newArrayList(url);
|
|
|
|
|
|
}
|
2020-05-14 16:22:48 +08:00
|
|
|
|
// 不是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);
|
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return "picture";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|