|
@@ -0,0 +1,142 @@
|
|
|
+package com.fdkankan.scene.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fdkankan.common.constant.ErrorCode;
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
|
+import com.fdkankan.common.user.SSOLoginHelper;
|
|
|
+import com.fdkankan.common.util.BASE64DecodedMultipartFile;
|
|
|
+import com.fdkankan.common.util.FileUtil;
|
|
|
+import com.fdkankan.fyun.oss.OssFilePath;
|
|
|
+import com.fdkankan.fyun.oss.UploadToOssUtil;
|
|
|
+import com.fdkankan.scene.entity.SceneUpload;
|
|
|
+import com.fdkankan.scene.mapper.ISceneUploadMapper;
|
|
|
+import com.fdkankan.scene.service.ISceneUploadService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2022-01-19
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SceneUploadServiceImpl extends ServiceImpl<ISceneUploadMapper, SceneUpload> implements ISceneUploadService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ UploadToOssUtil uploadToOssUtil;
|
|
|
+ @Value("${oss.point:http://oss-cn-shenzhen-internal.aliyuncs.com}")
|
|
|
+ private String point;
|
|
|
+
|
|
|
+ @Value("${oss.key:LTAIUrvuHqj8pvry}")
|
|
|
+ private String key;
|
|
|
+
|
|
|
+ @Value("${oss.secrey:JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4}")
|
|
|
+ private String secrey;
|
|
|
+
|
|
|
+ @Value("${oss.bucket:4dkankan}")
|
|
|
+ private String bucket;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SSOLoginHelper ssoLoginHelper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> uploads(String imgData,MultipartFile[] files,String sceneCode,Integer type) throws Exception{
|
|
|
+ List<MultipartFile> multipartFiles = new ArrayList<>();
|
|
|
+ if(StringUtils.isNotBlank(imgData)){
|
|
|
+ MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(imgData);
|
|
|
+ multipartFiles.add(file);
|
|
|
+ }
|
|
|
+ if(files !=null && files.length >0){
|
|
|
+ multipartFiles.addAll(Arrays.asList(files));
|
|
|
+ }
|
|
|
+ return this.uploadFiles(multipartFiles,sceneCode,type);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> uploadFiles(List<MultipartFile> files, String sceneCode, Integer type) throws Exception{
|
|
|
+ if (StringUtils.isEmpty(sceneCode) || files == null || files.size() <= 0) {
|
|
|
+ throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
|
|
|
+ }
|
|
|
+ List<String> urlList = new ArrayList<>();
|
|
|
+ String prefix ="";
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ if(StringUtils.isNotBlank(prefix) && !prefix.equals(fileName.substring(fileName.lastIndexOf(".")))){
|
|
|
+ throw new BusinessException(-1,"文件类型不一致");
|
|
|
+ }
|
|
|
+ // 获取文件后缀
|
|
|
+ prefix = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ }
|
|
|
+ if(type != null && 1 == type){
|
|
|
+ this.updateFileByPreFix(sceneCode,prefix);
|
|
|
+ }
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ // 获取文件后缀
|
|
|
+ prefix = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ File newFile = File.createTempFile(UUID.randomUUID().toString() ,prefix);
|
|
|
+ file.transferTo(newFile);
|
|
|
+ String ossPath = String.format(OssFilePath.USER_EDIT_PATH ,sceneCode) + fileName;
|
|
|
+ try {
|
|
|
+ uploadToOssUtil.upload2(newFile.getPath(),ossPath);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(ossPath+"上传文件失败"+e);
|
|
|
+ throw new BusinessException(-1,"上传文件失败");
|
|
|
+ }
|
|
|
+ String url =String.format("http://%s.%s/%s", bucket, point.replace("http://",""), ossPath);
|
|
|
+ urlList.add(url);
|
|
|
+
|
|
|
+ FileUtil.delFile(newFile.getPath());
|
|
|
+ //添加记录
|
|
|
+ this.saveData(sceneCode,ossPath,prefix);
|
|
|
+ }
|
|
|
+ return urlList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateFileByPreFix(String sceneCode, String prefix) {
|
|
|
+ LambdaQueryWrapper<SceneUpload> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(SceneUpload::getSceneCode,sceneCode)
|
|
|
+ .eq(SceneUpload::getFilePrefix,prefix)
|
|
|
+ .eq(SceneUpload::getTbStatus,0);
|
|
|
+ List<SceneUpload> list = this.list(queryWrapper);
|
|
|
+
|
|
|
+ if(list != null && list.size() >0){
|
|
|
+ for (SceneUpload sceneUpload : list) {
|
|
|
+ try {
|
|
|
+ uploadToOssUtil.delete(sceneUpload.getFileName());
|
|
|
+ this.removeEntity(sceneUpload);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(sceneUpload.getFileName()+"删除oss文件失败",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void removeEntity(SceneUpload sceneUpload) {
|
|
|
+ sceneUpload.setTbStatus(1);
|
|
|
+ this.updateById(sceneUpload);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveData(String sceneCode, String ossPath, String prefix) {
|
|
|
+ SceneUpload sceneUpload = new SceneUpload();
|
|
|
+ sceneUpload.setSceneCode(sceneCode);
|
|
|
+ sceneUpload.setFileName(ossPath);
|
|
|
+ sceneUpload.setFilePrefix(prefix);
|
|
|
+ sceneUpload.setUploadUser(null);
|
|
|
+ this.save(sceneUpload);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|