|
@@ -1,26 +1,49 @@
|
|
|
package com.zhongshu.card.server.core.service.oss;
|
|
package com.zhongshu.card.server.core.service.oss;
|
|
|
|
|
|
|
|
|
|
+import com.github.microservice.core.util.response.ResponseUtil;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.github.microservice.net.ResultState;
|
|
import com.github.microservice.net.ResultState;
|
|
|
|
|
+import com.github.microservice.netdisk.client.example.AccountExample;
|
|
|
|
|
+import com.github.microservice.netdisk.client.model.file.CopyFileModel;
|
|
|
import com.github.microservice.netdisk.client.model.file.FileItemModel;
|
|
import com.github.microservice.netdisk.client.model.file.FileItemModel;
|
|
|
import com.github.microservice.netdisk.client.model.file.FileStoreModel;
|
|
import com.github.microservice.netdisk.client.model.file.FileStoreModel;
|
|
|
import com.github.microservice.netdisk.client.model.remote.RemoteFile;
|
|
import com.github.microservice.netdisk.client.model.remote.RemoteFile;
|
|
|
import com.github.microservice.netdisk.client.service.NetDiskOssService;
|
|
import com.github.microservice.netdisk.client.service.NetDiskOssService;
|
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
|
-import com.zhongshu.card.client.type.oss.PlatformStoreType;
|
|
|
|
|
|
|
+import com.mongodb.client.gridfs.model.GridFSFile;
|
|
|
|
|
+import com.zhongshu.card.client.model.base.OidParam;
|
|
|
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
|
|
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
|
|
|
import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
|
import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
-import com.zhongshu.card.server.core.model.upLoadFile.*;
|
|
|
|
|
|
|
+import com.zhongshu.card.server.core.model.upLoadFile.AliObjectStorageStore;
|
|
|
|
|
+import com.zhongshu.card.server.core.model.upLoadFile.RemoteBusFile;
|
|
|
|
|
+import com.zhongshu.card.server.core.model.upLoadFile.RemoteGetTokenFileParam;
|
|
|
|
|
+import com.zhongshu.card.server.core.model.upLoadFile.StsToken;
|
|
|
import com.zhongshu.card.server.core.model.upLoadFile.oss.ALiObjectStorage;
|
|
import com.zhongshu.card.server.core.model.upLoadFile.oss.ALiObjectStorage;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.card.server.core.util.CommonUtil;
|
|
import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
+import lombok.Cleanup;
|
|
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
|
|
+import org.springframework.data.mongodb.gridfs.GridFsResource;
|
|
|
|
|
+import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -50,15 +73,69 @@ public class NetDiskOssBusService extends SuperService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private OrganizationDao organizationDao;
|
|
private OrganizationDao organizationDao;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private GridFsTemplate gridFsTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${gateWay.host:http://172.24.50.53:8080}")
|
|
|
|
|
+ private String gateWayHost;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${spring.application.name}")
|
|
|
|
|
+ private String applicationName;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * @param param
|
|
|
|
|
* @param file
|
|
* @param file
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
- public String uploadMinoFile(RemoteGetTokenFileParam param, MultipartFile file) {
|
|
|
|
|
- String url = "";
|
|
|
|
|
|
|
+ public ResultContent<String> uploadMinioFile(MultipartFile file, OidParam param) throws IOException {
|
|
|
|
|
+ @Cleanup var inputStream = file.getInputStream();
|
|
|
|
|
+ String accountId = "";
|
|
|
|
|
+ if (ObjectUtils.isNotEmpty(param)) {
|
|
|
|
|
+ accountId = param.getOid();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(accountId)) {
|
|
|
|
|
+ accountId = getCurrentProjectOid();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(accountId)) {
|
|
|
|
|
+ accountId = getCurrentOid();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(accountId)) {
|
|
|
|
|
+ return ResultContent.buildFail("");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ final String filename = CommonUtil.UUID() + "_" + file.getOriginalFilename();
|
|
|
|
|
+ String mimeTypeName = ResponseUtil.getMimeType(FilenameUtils.getExtension(filename));
|
|
|
|
|
+ gridFsTemplate.store(inputStream, filename, mimeTypeName).toHexString();
|
|
|
|
|
+ String url = String.format("%s/%s/oss/dl/%s", gateWayHost, applicationName.toLowerCase(), filename);
|
|
|
|
|
+ // 通知网盘上传文件
|
|
|
|
|
+ CopyFileModel copyFileModel = new CopyFileModel();
|
|
|
|
|
+ copyFileModel.setAccountId(accountId);
|
|
|
|
|
+ copyFileModel.setUrl(url);
|
|
|
|
|
+ copyFileModel.setPath(new String[]{"files", filename});
|
|
|
|
|
|
|
|
- return url;
|
|
|
|
|
|
|
+ // 通知网盘拷贝文件
|
|
|
|
|
+ com.github.microservice.netdisk.client.content.ResultContent<FileStoreModel> fileStoreModelResultContent = this.netDiskOssService.copyFile(copyFileModel);
|
|
|
|
|
+ //删除文件
|
|
|
|
|
+ this.gridFsTemplate.delete(makeQuery(filename));
|
|
|
|
|
+ if (fileStoreModelResultContent.getState() == com.github.microservice.netdisk.client.content.ResultState.Success) {
|
|
|
|
|
+ FileStoreModel fileStoreModel = fileStoreModelResultContent.getContent();
|
|
|
|
|
+ return ResultContent.buildSuccess(fileStoreModel.getUrl());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return ResultContent.buildFail(String.format("上传文件失败:%s", fileStoreModelResultContent.getMsg()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SneakyThrows
|
|
|
|
|
+ public void download(HttpServletResponse response, String filename) {
|
|
|
|
|
+ GridFSFile gridFSFile = gridFsTemplate.findOne(makeQuery(filename));
|
|
|
|
|
+ GridFsResource gridFsResource = gridFsTemplate.getResource(gridFSFile);
|
|
|
|
|
+ @Cleanup InputStream inputStream = gridFsResource.getInputStream();
|
|
|
|
|
+
|
|
|
|
|
+ // 拷贝流
|
|
|
|
|
+ @Cleanup OutputStream outputStream = response.getOutputStream();
|
|
|
|
|
+ response.setContentType(gridFsResource.getContentType());
|
|
|
|
|
+ response.setContentLengthLong(gridFsResource.contentLength());
|
|
|
|
|
+ StreamUtils.copy(inputStream, outputStream);
|
|
|
|
|
+ outputStream.flush();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ResultContent<com.github.microservice.netdisk.client.model.token.StsToken> getUploadFileToken(RemoteGetTokenFileParam param) {
|
|
public ResultContent<com.github.microservice.netdisk.client.model.token.StsToken> getUploadFileToken(RemoteGetTokenFileParam param) {
|
|
@@ -178,4 +255,9 @@ public class NetDiskOssBusService extends SuperService {
|
|
|
});
|
|
});
|
|
|
return paths;
|
|
return paths;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private Query makeQuery(String filename) {
|
|
|
|
|
+ return Query.query(Criteria.where("filename").is(filename));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|