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;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
|
public PictureFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
2020-05-18 09:46:52 +08:00
|
|
|
|
}
|
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")) {
|
2020-12-27 01:43:50 +08:00
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
if (response.isFailure()) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2020-05-14 16:22:48 +08:00
|
|
|
|
} 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);
|
|
|
|
|
|
}
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|