|
@@ -8,7 +8,6 @@ import java.util.Optional;
|
|
|
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItem;
|
|
import org.apache.commons.fileupload.disk.DiskFileItem;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.dao.DataIntegrityViolationException;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
@@ -24,82 +23,85 @@ import cn.com.qmth.examcloud.exchange.inner.api.request.PutFileReq;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class ResourceServiceImpl implements ResourceService {
|
|
public class ResourceServiceImpl implements ResourceService {
|
|
- private static final String fileRootPath="/net_disk";
|
|
|
|
- private static final String filePathSeparate="/";
|
|
|
|
|
|
+
|
|
|
|
+ private static final String FILE_ROOT_PATH = "/net_disk";
|
|
|
|
+
|
|
|
|
+ private static final String FILE_PATH_SEPARATE = "/";
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
ResourceRepo resourceRepo;
|
|
ResourceRepo resourceRepo;
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
UpyunCloudService upyunCloudService;
|
|
UpyunCloudService upyunCloudService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void addDir(ResourceInfo info) {
|
|
public void addDir(ResourceInfo info) {
|
|
- ResourceEntity e=new ResourceEntity();
|
|
|
|
|
|
+
|
|
|
|
+ ResourceEntity e = new ResourceEntity();
|
|
e.setName(info.getName());
|
|
e.setName(info.getName());
|
|
e.setRootOrgId(info.getRootOrgId());
|
|
e.setRootOrgId(info.getRootOrgId());
|
|
e.setIsFile(false);
|
|
e.setIsFile(false);
|
|
- if(info.getParentId()==null||info.getParentId().longValue()==-1) {
|
|
|
|
|
|
+ if (info.getParentId() == null) {
|
|
e.setParentId(null);
|
|
e.setParentId(null);
|
|
- e.setFilePath(fileRootPath+filePathSeparate+info.getRootOrgId()+filePathSeparate+info.getName());
|
|
|
|
- }else {
|
|
|
|
|
|
+ e.setFilePath(FILE_ROOT_PATH + FILE_PATH_SEPARATE + info.getRootOrgId()
|
|
|
|
+ + FILE_PATH_SEPARATE + info.getName());
|
|
|
|
+ } else {
|
|
e.setParentId(info.getParentId());
|
|
e.setParentId(info.getParentId());
|
|
- Optional<ResourceEntity> op=resourceRepo.findById(info.getParentId());
|
|
|
|
- if(op.isPresent()) {
|
|
|
|
- ResourceEntity pe=op.get();
|
|
|
|
- e.setFilePath(pe.getFilePath()+filePathSeparate+info.getName());
|
|
|
|
- }else {
|
|
|
|
|
|
+ Optional<ResourceEntity> op = resourceRepo.findById(info.getParentId());
|
|
|
|
+ if (op.isPresent()) {
|
|
|
|
+ ResourceEntity pe = op.get();
|
|
|
|
+ e.setFilePath(pe.getFilePath() + FILE_PATH_SEPARATE + info.getName());
|
|
|
|
+ } else {
|
|
throw new StatusException("100000", "上级目录不存在");
|
|
throw new StatusException("100000", "上级目录不存在");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- try {
|
|
|
|
- resourceRepo.save(e);
|
|
|
|
- } catch (DataIntegrityViolationException e1) {
|
|
|
|
- if(e1.getMessage().contains("IDX_B_RESOURCE_000002")) {
|
|
|
|
- throw new StatusException("100001", "目录已存在");
|
|
|
|
- }else {
|
|
|
|
- throw e1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ resourceRepo.saveAndFlush(e);
|
|
}
|
|
}
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void addFile(Long rootOrgId, Long parentId, MultipartFile dataFile) {
|
|
public void addFile(Long rootOrgId, Long parentId, MultipartFile dataFile) {
|
|
- ResourceEntity e=new ResourceEntity();
|
|
|
|
- String fileName=dataFile.getOriginalFilename();
|
|
|
|
|
|
+
|
|
|
|
+ ResourceEntity e = new ResourceEntity();
|
|
|
|
+ String fileName = dataFile.getOriginalFilename();
|
|
e.setName(fileName);
|
|
e.setName(fileName);
|
|
e.setRootOrgId(rootOrgId);
|
|
e.setRootOrgId(rootOrgId);
|
|
e.setIsFile(true);
|
|
e.setIsFile(true);
|
|
- String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
|
|
|
|
- e.setSuffix(suffix);
|
|
|
|
- if(parentId==null||parentId.longValue()==-1) {
|
|
|
|
|
|
+ String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
|
|
|
|
+ e.setSuffix(suffix);
|
|
|
|
+ if (parentId == null || parentId.longValue() == -1) {
|
|
e.setParentId(null);
|
|
e.setParentId(null);
|
|
- e.setFilePath(fileRootPath+filePathSeparate+rootOrgId+filePathSeparate+fileName);
|
|
|
|
- }else {
|
|
|
|
|
|
+ e.setFilePath(FILE_ROOT_PATH + FILE_PATH_SEPARATE + rootOrgId + FILE_PATH_SEPARATE
|
|
|
|
+ + fileName);
|
|
|
|
+ } else {
|
|
e.setParentId(parentId);
|
|
e.setParentId(parentId);
|
|
- Optional<ResourceEntity> op=resourceRepo.findById(parentId);
|
|
|
|
- if(op.isPresent()) {
|
|
|
|
- ResourceEntity pe=op.get();
|
|
|
|
- e.setFilePath(pe.getFilePath()+filePathSeparate+fileName);
|
|
|
|
- }else {
|
|
|
|
|
|
+ Optional<ResourceEntity> op = resourceRepo.findById(parentId);
|
|
|
|
+ if (op.isPresent()) {
|
|
|
|
+ ResourceEntity pe = op.get();
|
|
|
|
+ e.setFilePath(pe.getFilePath() + FILE_PATH_SEPARATE + fileName);
|
|
|
|
+ } else {
|
|
throw new StatusException("100020", "上级目录不存在");
|
|
throw new StatusException("100020", "上级目录不存在");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- ResourceEntity olde=resourceRepo.findByRootOrgIdAndFilePath(rootOrgId, e.getFilePath());
|
|
|
|
- if(olde!=null) {
|
|
|
|
|
|
+ ResourceEntity olde = resourceRepo.findByRootOrgIdAndFilePath(rootOrgId, e.getFilePath());
|
|
|
|
+ if (olde != null) {
|
|
olde.setUpdateTime(new Date());
|
|
olde.setUpdateTime(new Date());
|
|
resourceRepo.save(olde);
|
|
resourceRepo.save(olde);
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
resourceRepo.save(e);
|
|
resourceRepo.save(e);
|
|
}
|
|
}
|
|
- PutFileReq req=new PutFileReq();
|
|
|
|
- List<FormFilePart> formFilePartList =new ArrayList<FormFilePart>();
|
|
|
|
- CommonsMultipartFile cf= (CommonsMultipartFile)dataFile;
|
|
|
|
- DiskFileItem fi = (DiskFileItem)cf.getFileItem();
|
|
|
|
|
|
+ PutFileReq req = new PutFileReq();
|
|
|
|
+ List<FormFilePart> formFilePartList = new ArrayList<FormFilePart>();
|
|
|
|
+ CommonsMultipartFile cf = (CommonsMultipartFile) dataFile;
|
|
|
|
+ DiskFileItem fi = (DiskFileItem) cf.getFileItem();
|
|
|
|
|
|
- File f = fi.getStoreLocation();
|
|
|
|
|
|
+ File f = fi.getStoreLocation();
|
|
FormFilePart part = new FormFilePart("file", fileName, f);
|
|
FormFilePart part = new FormFilePart("file", fileName, f);
|
|
formFilePartList.add(part);
|
|
formFilePartList.add(part);
|
|
|
|
|
|
req.setFormFilePartList(formFilePartList);
|
|
req.setFormFilePartList(formFilePartList);
|
|
req.setSiteId("netDisk");
|
|
req.setSiteId("netDisk");
|
|
- String relativePath=e.getFilePath().replaceFirst(fileRootPath, "").replaceFirst(filePathSeparate, "");
|
|
|
|
|
|
+ String relativePath = e.getFilePath().replaceFirst(FILE_ROOT_PATH, "")
|
|
|
|
+ .replaceFirst(FILE_PATH_SEPARATE, "");
|
|
req.setRelativePath(relativePath);
|
|
req.setRelativePath(relativePath);
|
|
upyunCloudService.putFile(req);
|
|
upyunCloudService.putFile(req);
|
|
}
|
|
}
|