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;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @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-05-19 11:04:53 +08:00
|
|
|
|
|
2020-12-27 01:43:50 +08:00
|
|
|
|
public MediaFilePreviewImpl(FileHandlerService fileHandlerService) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
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-05-19 11:04:53 +08:00
|
|
|
|
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
|
|
|
|
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);
|
2019-05-16 17:46:08 +08:00
|
|
|
|
String suffix=fileAttribute.getSuffix();
|
|
|
|
|
|
if ("flv".equalsIgnoreCase(suffix)) {
|
|
|
|
|
|
return "flv";
|
|
|
|
|
|
}
|
2018-03-25 13:26:51 +08:00
|
|
|
|
return "media";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|