|
@@ -1,10 +1,22 @@
|
|
package com.hssx.cloudmodel.service.impl;
|
|
package com.hssx.cloudmodel.service.impl;
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.hssx.cloudmodel.entity.ProjectFile;
|
|
import com.hssx.cloudmodel.entity.ProjectFile;
|
|
|
|
+import com.hssx.cloudmodel.entity.User;
|
|
|
|
+import com.hssx.cloudmodel.entity.vo.UserVO;
|
|
import com.hssx.cloudmodel.mapper.ProjectFileMapper;
|
|
import com.hssx.cloudmodel.mapper.ProjectFileMapper;
|
|
|
|
+import com.hssx.cloudmodel.mapper.UserMapper;
|
|
import com.hssx.cloudmodel.service.ProjectFileService;
|
|
import com.hssx.cloudmodel.service.ProjectFileService;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.hssx.cloudmodel.util.FileUtil;
|
|
|
|
+import com.hssx.cloudmodel.util.HttpRespMsg;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
/**
|
|
* <p>
|
|
* <p>
|
|
@@ -17,4 +29,57 @@ import org.springframework.stereotype.Service;
|
|
@Service
|
|
@Service
|
|
public class ProjectFileServiceImpl extends ServiceImpl<ProjectFileMapper, ProjectFile> implements ProjectFileService {
|
|
public class ProjectFileServiceImpl extends ServiceImpl<ProjectFileMapper, ProjectFile> implements ProjectFileService {
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ UserMapper userMapper;
|
|
|
|
+ @Resource
|
|
|
|
+ ProjectFileMapper projectFileMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public HttpRespMsg addFile(UserVO userVO, MultipartFile file, String path) {
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
|
|
|
|
+ if(user != null){
|
|
|
|
+ if (file != null && !file.isEmpty()) {
|
|
|
|
+ ProjectFile projectFile = new ProjectFile();
|
|
|
|
+ projectFile.setUploaderId(user.getId());
|
|
|
|
+ projectFile.setUploader(user.getUsername());
|
|
|
|
+ projectFile.setProjectId(userVO.getProjectId());
|
|
|
|
+ File dir = null;
|
|
|
|
+ dir = new File(path);
|
|
|
|
+ // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ String fileName = "";
|
|
|
|
+ if (file != null && !file.isEmpty()) {
|
|
|
|
+ fileName = file.getOriginalFilename();
|
|
|
|
+ projectFile.setFileName(fileName);
|
|
|
|
+ projectFile.setFileSize(FileUtil.getReadableFileSize(file.getSize()));
|
|
|
|
+ System.out.println("上传文件名称" + file.getName() + ", dir = " + dir.getAbsolutePath());
|
|
|
|
+ int pos = fileName.lastIndexOf(".");
|
|
|
|
+ String rand = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+ String sufix = fileName.substring(pos);
|
|
|
|
+ fileName = rand + sufix;
|
|
|
|
+ projectFile.setFileType(sufix);//文件后缀
|
|
|
|
+ projectFile.setUrl("/upload/"+fileName);
|
|
|
|
+ File saveFile = new File(dir, fileName);
|
|
|
|
+ projectFileMapper.insert(projectFile);
|
|
|
|
+ try {
|
|
|
|
+ saveFile.createNewFile();
|
|
|
|
+ file.transferTo(saveFile);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ projectFile = null;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ projectFile = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ msg.data = projectFile;
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ msg.setError("当前用户不存在或者未登录");
|
|
|
|
+ }
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
}
|
|
}
|