|
@@ -12,7 +12,6 @@ import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eco.common.core.config.EcoConfig;
|
|
|
-import org.eco.common.core.constant.CacheNames;
|
|
|
import org.eco.common.core.constant.Constants;
|
|
|
import org.eco.common.core.core.domain.UploadRes;
|
|
|
import org.eco.common.core.core.page.PageResult;
|
|
@@ -23,6 +22,7 @@ import org.eco.common.core.utils.SpringUtils;
|
|
|
import org.eco.common.core.utils.StreamUtils;
|
|
|
import org.eco.common.core.utils.StringUtils;
|
|
|
import org.eco.common.core.utils.file.FileUtils;
|
|
|
+import org.eco.common.hadoop.hdfs.HadoopClient;
|
|
|
import org.eco.common.orm.core.page.PageQuery;
|
|
|
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
|
|
|
import org.eco.common.oss.core.OssClient;
|
|
@@ -32,11 +32,10 @@ import org.eco.common.oss.factory.OssFactory;
|
|
|
import org.eco.system.domain.SysOss;
|
|
|
import org.eco.system.domain.bo.SysOssBo;
|
|
|
import org.eco.system.domain.vo.SysOssVo;
|
|
|
-import org.eco.system.mapper.SysDeptMapper;
|
|
|
import org.eco.system.mapper.SysOssMapper;
|
|
|
import org.eco.system.service.ICommonService;
|
|
|
import org.eco.system.service.ISysOssService;
|
|
|
-import org.springframework.cache.annotation.Cacheable;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -49,6 +48,7 @@ import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static org.eco.common.core.utils.file.FileUploadUtils.extractFilename;
|
|
|
import static org.eco.common.core.utils.file.FileUtils.percentEncode;
|
|
|
import static org.eco.system.domain.table.SysOssTableDef.SYS_OSS;
|
|
|
|
|
@@ -67,6 +67,11 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
|
|
|
@Resource
|
|
|
private ICommonService commonService;
|
|
|
+ @Resource
|
|
|
+ private HadoopClient hadoopClient;
|
|
|
+
|
|
|
+ @Value("${hadoop.directoryPath}")
|
|
|
+ private String directoryPath;
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -136,7 +141,7 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
|
|
|
@Override
|
|
|
public void download(Long ossId, HttpServletResponse response) throws IOException {
|
|
|
- SysOssVo sysOss = SpringUtils.getAopProxy(this).getById(ossId);
|
|
|
+ SysOssVo sysOss = this.getById(ossId);
|
|
|
if (ObjectUtil.isNull(sysOss)) {
|
|
|
throw new BusinessException("文件数据不存在!");
|
|
|
}
|
|
@@ -152,6 +157,21 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void downloadHadoop(Long ossId, HttpServletResponse response) throws IOException {
|
|
|
+ SysOssVo sysOss = this.getById(ossId);
|
|
|
+ if (ObjectUtil.isNull(sysOss)) {
|
|
|
+ throw new BusinessException("文件数据不存在!");
|
|
|
+ }
|
|
|
+ FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
|
|
|
+ response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
|
|
|
+ try {
|
|
|
+ hadoopClient.download(sysOss.getUrl(), "", response.getOutputStream());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public InputStream getFileStream(Long ossId) {
|
|
|
SysOssVo sysOss = SpringUtils.getAopProxy(this).getById(ossId);
|
|
@@ -175,7 +195,7 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
OssClient storage = OssFactory.instance();
|
|
|
UploadResult uploadResult;
|
|
|
try {
|
|
|
- if (StrUtil.equals(storage.getConfigKey(), "local")) {
|
|
|
+ if (StrUtil.equals(storage.getConfigKey(), "hadoop")) {
|
|
|
Map<String, String> map = commonService.uploadFile(file);
|
|
|
uploadResult = UploadResult.builder().url(map.get("url")).filename(map.get("fileName")).build();
|
|
|
} else {
|
|
@@ -188,6 +208,25 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
return buildResultEntity(originalFilename, suffix, storage.getConfigKey(), uploadResult);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public SysOssVo uploadHadoop(MultipartFile file) {
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ assert originalFilename != null;
|
|
|
+ String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
|
|
|
+ UploadResult uploadResult;
|
|
|
+ try {
|
|
|
+ String fileName = extractFilename(file);
|
|
|
+ String uploadPath = directoryPath + fileName;
|
|
|
+ hadoopClient.copyFileToHDFS(false, true, org.eco.common.hadoop.utils.FileUtil.multipartFileToFile(file).getPath(), uploadPath);
|
|
|
+ uploadResult = UploadResult.builder().url(uploadPath).filename(uploadPath).build();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ // 保存文件信息
|
|
|
+ return buildResultEntity(originalFilename, suffix, "hadoop", uploadResult);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public UploadRes upload(byte[] file, String name) {
|
|
|
String suffix = StringUtils.substring(name, name.lastIndexOf("."), name.length());
|
|
@@ -196,7 +235,7 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
File tempFile = null;
|
|
|
try {
|
|
|
tempFile = File.createTempFile(name, suffix);
|
|
|
- if (StrUtil.equals(storage.getConfigKey(), "local")) {
|
|
|
+ if (StrUtil.equals(storage.getConfigKey(), "hadoop")) {
|
|
|
Map<String, String> map = commonService.uploadFile(FileUtils.fileToMultipartFile(tempFile));
|
|
|
uploadResult = UploadResult.builder().url(map.get("url")).filename(map.get("fileName")).build();
|
|
|
} else {
|
|
@@ -210,21 +249,39 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
FileUtil.del(tempFile);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
SysOssVo ossVo = buildResultEntity(name, suffix, storage.getConfigKey(), uploadResult);
|
|
|
return UploadRes.builder().name(name).url(uploadResult.getUrl()).ossId(ossVo.getOssId()).build();
|
|
|
}
|
|
|
+ @Override
|
|
|
+ public UploadRes uploadHadoop(byte[] file, String name) {
|
|
|
+ String suffix = StringUtils.substring(name, name.lastIndexOf("."), name.length());
|
|
|
+ UploadResult uploadResult;
|
|
|
+ File tempFile = null;
|
|
|
+ try {
|
|
|
+ tempFile = File.createTempFile(name, suffix);
|
|
|
+ String uploadPath = directoryPath + name;
|
|
|
+ hadoopClient.copyFileToHDFS(false, true, tempFile.getPath(), uploadPath);
|
|
|
+ uploadResult = UploadResult.builder().url(uploadPath).filename(uploadPath).build();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ // 删除临时文件(可选)
|
|
|
+ if (tempFile != null) {
|
|
|
+ FileUtil.del(tempFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SysOssVo ossVo = buildResultEntity(name, suffix, "hadoop", uploadResult);
|
|
|
+ return UploadRes.builder().name(name).url(uploadResult.getUrl()).ossId(ossVo.getOssId()).build();
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public SysOssVo upload(File file) {
|
|
|
String originalFileName = file.getName();
|
|
|
String suffix = StringUtils.substring(originalFileName, originalFileName.lastIndexOf("."), originalFileName.length());
|
|
|
OssClient storage = OssFactory.instance();
|
|
|
-
|
|
|
UploadResult uploadResult;
|
|
|
try {
|
|
|
- if (StrUtil.equals(storage.getConfigKey(), "local")) {
|
|
|
+ if (StrUtil.equals(storage.getConfigKey(), "hadoop")) {
|
|
|
Map<String, String> map = commonService.uploadFile(FileUtils.fileToMultipartFile(file));
|
|
|
uploadResult = UploadResult.builder().url(map.get("url")).filename(map.get("fileName")).build();
|
|
|
} else {
|
|
@@ -237,6 +294,22 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
return buildResultEntity(originalFileName, suffix, storage.getConfigKey(), uploadResult);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public SysOssVo uploadHadoop(File file) {
|
|
|
+ String originalFileName = file.getName();
|
|
|
+ String suffix = StringUtils.substring(originalFileName, originalFileName.lastIndexOf("."), originalFileName.length());
|
|
|
+ UploadResult uploadResult;
|
|
|
+ try {
|
|
|
+ String uploadPath = directoryPath + originalFileName;
|
|
|
+ hadoopClient.copyFileToHDFS(false, true, file.getPath(), uploadPath);
|
|
|
+ uploadResult = UploadResult.builder().url(uploadPath).filename(uploadPath).build();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ // 保存文件信息
|
|
|
+ return buildResultEntity(originalFileName, suffix, "hadoop", uploadResult);
|
|
|
+ }
|
|
|
+
|
|
|
private SysOssVo buildResultEntity(String originalFileName, String suffix, String configKey, UploadResult uploadResult) {
|
|
|
SysOss oss = new SysOss();
|
|
|
oss.setUrl(uploadResult.getUrl());
|
|
@@ -258,7 +331,7 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
|
|
|
List<SysOss> list = this.listByIds(ids);
|
|
|
for (SysOss sysOss : list) {
|
|
|
- if (StrUtil.equals("local", sysOss.getService())) {
|
|
|
+ if (StrUtil.equals("hadoop", sysOss.getService())) {
|
|
|
// 删除本地文件
|
|
|
String filePath = EcoConfig.getProfile() + StringUtils.substringAfter(sysOss.getFileName(), Constants.RESOURCE_PREFIX);
|
|
|
boolean deleted = FileUtil.del(filePath);
|
|
@@ -280,7 +353,7 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
* @return oss 匹配Url的OSS对象
|
|
|
*/
|
|
|
private SysOssVo matchingUrl(SysOssVo oss) {
|
|
|
- if (StrUtil.equals("local", oss.getService())) {
|
|
|
+ if (StrUtil.equals("hadoop", oss.getService())) {
|
|
|
return oss;
|
|
|
}
|
|
|
OssClient storage = OssFactory.instance(oss.getService());
|
|
@@ -298,7 +371,7 @@ public class SysOssServiceImpl extends BaseServiceImpl<SysOssMapper, SysOss> imp
|
|
|
oss.setFileSuffix(suffix);
|
|
|
oss.setFileName(fileName);
|
|
|
oss.setOriginalName(originalFileName);
|
|
|
- oss.setService("local");
|
|
|
+ oss.setService("hadoop");
|
|
|
this.save(oss);
|
|
|
SysOssVo sysOssVo = MapstructUtils.convert(oss, SysOssVo.class);
|
|
|
assert sysOssVo != null;
|