2018-03-25 13:26:51 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
2019-05-16 17:46:08 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
2020-05-19 11:04:53 +08:00
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2018-03-25 13:26:51 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
2020-05-19 11:04:53 +08:00
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2020-05-19 11:04:53 +08:00
|
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2018-03-25 13:26:51 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
|
2018-03-25 13:26:51 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @author : kl
|
|
|
|
|
|
* @authorboke : kailing.pub
|
|
|
|
|
|
* @create : 2018-03-25 上午11:58
|
|
|
|
|
|
* @description:
|
|
|
|
|
|
**/
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class MediaFilePreviewImpl 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;
|
2020-05-19 11:04:53 +08:00
|
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
|
public MediaFilePreviewImpl(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-19 11:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-25 13:26:51 +08:00
|
|
|
|
@Override
|
2019-06-17 14:21:16 +08:00
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2020-05-19 11:04:53 +08:00
|
|
|
|
// 不是http开头,浏览器不能直接访问,需下载到本地
|
|
|
|
|
|
if (url != null && !url.toLowerCase().startsWith("http")) {
|
2020-12-27 01:43:50 +08:00
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileAttribute.getName());
|
2020-12-27 14:47:28 +08:00
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2020-05-19 11:04:53 +08:00
|
|
|
|
} else {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + fileHandlerService.getRelativePath(response.getContent()));
|
2020-05-19 11:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
model.addAttribute("mediaUrl", url);
|
|
|
|
|
|
}
|
2018-03-25 13:26:51 +08:00
|
|
|
|
model.addAttribute("mediaUrl", url);
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return MEDIA_FILE_PREVIEW_PAGE;
|
2018-03-25 13:26:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|