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;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
2020-12-26 01:52:52 +08:00
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
|
* Content :图片文件处理
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class PictureFilePreviewImpl implements FilePreview {
|
|
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
private final FileHandlerService fileHandlerService;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-05-18 09:46:52 +08:00
|
|
|
|
private final DownloadUtils downloadUtils;
|
|
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
public PictureFilePreviewImpl(FileHandlerService fileHandlerService,
|
2020-05-18 09:46:52 +08:00
|
|
|
|
DownloadUtils downloadUtils) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-05-18 09:46:52 +08:00
|
|
|
|
this.downloadUtils = downloadUtils;
|
|
|
|
|
|
}
|
2020-05-14 16:22:48 +08:00
|
|
|
|
|
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-12-26 01:52:52 +08:00
|
|
|
|
List<String> imgUrls = new ArrayList<>();
|
|
|
|
|
|
imgUrls.add(url);
|
|
|
|
|
|
String fileKey = fileAttribute.getFileKey();
|
2020-12-26 19:13:50 +08:00
|
|
|
|
List<String> zipImgUrls = fileHandlerService.getImgCache(fileKey);
|
2020-12-26 01:52:52 +08:00
|
|
|
|
if (!CollectionUtils.isEmpty(zipImgUrls)) {
|
|
|
|
|
|
imgUrls.addAll(zipImgUrls);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
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 {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
String file = fileHandlerService.getRelativePath(response.getContent());
|
2020-12-26 01:52:52 +08:00
|
|
|
|
imgUrls.clear();
|
|
|
|
|
|
imgUrls.add(file);
|
|
|
|
|
|
model.addAttribute("imgurls", imgUrls);
|
2020-05-14 16:22:48 +08:00
|
|
|
|
model.addAttribute("currentUrl", file);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
model.addAttribute("imgurls", imgUrls);
|
|
|
|
|
|
model.addAttribute("currentUrl", url);
|
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return "picture";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|