Files
file-online-preview/server/src/main/java/cn/keking/service/impl/MediaFilePreviewImpl.java

52 lines
1.8 KiB
Java
Raw Normal View History

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;
import cn.keking.service.FilePreview;
2020-05-19 11:04:53 +08:00
import cn.keking.utils.DownloadUtils;
import cn.keking.service.FileHandlerService;
2020-05-19 11:04:53 +08:00
import cn.keking.web.filter.BaseUrlFilter;
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 {
private final FileHandlerService fileHandlerService;
private final OtherFilePreviewImpl otherFilePreview;
2020-05-19 11:04:53 +08:00
public MediaFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
this.fileHandlerService = fileHandlerService;
this.otherFilePreview = otherFilePreview;
2020-05-19 11:04:53 +08:00
}
@Override
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")) {
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileAttribute.getName());
if (response.isFailure()) {
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
2020-05-19 11:04:53 +08:00
} else {
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + fileHandlerService.getRelativePath(response.getContent()));
2020-05-19 11:04:53 +08:00
}
} else {
model.addAttribute("mediaUrl", url);
}
model.addAttribute("mediaUrl", url);
String suffix = fileAttribute.getSuffix();
2019-05-16 17:46:08 +08:00
if ("flv".equalsIgnoreCase(suffix)) {
return "flv";
}
return "media";
}
}