Bladeren bron

文件云存储代码修改

lideyin 5 jaren geleden
bovenliggende
commit
438516532b

+ 101 - 93
examcloud-exchange-inner-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/inner/api/controller/UpyunController.java

@@ -7,6 +7,9 @@ import java.io.IOException;
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 
+import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
+import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
+import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.util.IOUtils;
@@ -40,98 +43,103 @@ import io.swagger.annotations.ApiOperation;
 @RequestMapping("${$rmp.ctr.exchange.inner}/upyun")
 public class UpyunController extends ControllerSupport {
 
-	@Autowired
-	SystemProperties systemConfig;
-
-	@Autowired
-	UpyunService upyunService;
-
-	@ApiOperation(value = "保存照片")
-	@PutMapping("put/{siteId}/{fileSuffix}")
-	public String putFile(@PathVariable String siteId, @PathVariable String fileSuffix,
-			@RequestParam(required = false) String md5, HttpServletRequest req) {
-
-		String contentLength = req.getHeader("Content-Length");
-		if (StringUtils.isNotBlank(contentLength)) {
-			long contentLengthLong = Long.parseLong(contentLength);
-			if (contentLengthLong < 10) {
-				throw new StatusException("600108", "empty IO stream");
-			}
-		}
-
-		if (StringUtils.isNotBlank(md5)) {
-			if (MD5.encrypt16("").equalsIgnoreCase(md5)) {
-				throw new StatusException("600109", "empty IO stream");
-			}
-		}
-
-		User accessUser = getAccessUser();
-
-		if (!fileSuffix.matches("\\w+")) {
-			throw new StatusException("600100", "fileSuffix is wrong");
-		}
-
-		fileSuffix = "." + fileSuffix;
-
-		ServletInputStream in = null;
-
-		if (StringUtils.isNotBlank(md5)) {
-
-			try {
-				in = req.getInputStream();
-
-				UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
-				env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
-				env.setUserId(String.valueOf(accessUser.getUserId()));
-				env.setFileSuffix(fileSuffix);
-				String url = upyunService.writeFile(siteId, env, in, md5).getUrl();
-				url = new AES().encrypt(url);
-				return url;
-			} catch (IOException e) {
-				throw new ExamCloudRuntimeException(e);
-			} finally {
-				IOUtils.closeQuietly(in);
-			}
-
-		} else {
-
-			try {
-				FileUtils.forceMkdir(new File(systemConfig.getTempDataDir()));
-			} catch (IOException e1) {
-				log.error("fail to make dir. path=" + systemConfig.getTempDataDir());
-			}
-
-			String filePath = systemConfig.getTempDataDir() + File.separator + UUID.randomUUID()
-					+ fileSuffix;
-			File file = new File(filePath);
-
-			FileOutputStream out = null;
-			try {
-				in = req.getInputStream();
-				out = new FileOutputStream(file);
-				IOUtils.copy(in, out);
-
-				UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
-				env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
-				env.setUserId(String.valueOf(accessUser.getUserId()));
-				env.setFileSuffix(fileSuffix);
-				String url = upyunService.writeFile(siteId, env, file, true).getUrl();
-				url = new AES().encrypt(url);
-				return url;
-			} catch (IOException e) {
-				throw new ExamCloudRuntimeException(e);
-			} finally {
-				IOUtils.closeQuietly(in);
-				IOUtils.closeQuietly(out);
-				try {
-					FileUtils.forceDelete(file);
-				} catch (IOException e) {
-					log.error("fail to delete file. path=" + filePath);
-				}
-			}
-
-		}
-
-	}
+    @Autowired
+    SystemProperties systemConfig;
+
+    @Autowired
+    UpyunService upyunService;
+
+    @ApiOperation(value = "保存照片")
+    @PutMapping("put/{siteId}/{fileSuffix}")
+    public String putFile(@PathVariable String siteId, @PathVariable String fileSuffix,
+                          @RequestParam(required = false) String md5, HttpServletRequest req) {
+
+        String contentLength = req.getHeader("Content-Length");
+        if (StringUtils.isNotBlank(contentLength)) {
+            long contentLengthLong = Long.parseLong(contentLength);
+            if (contentLengthLong < 10) {
+                throw new StatusException("600108", "empty IO stream");
+            }
+        }
+
+        if (StringUtils.isNotBlank(md5)) {
+            if (MD5.encrypt16("").equalsIgnoreCase(md5)) {
+                throw new StatusException("600109", "empty IO stream");
+            }
+        }
+
+        User accessUser = getAccessUser();
+
+        if (!fileSuffix.matches("\\w+")) {
+            throw new StatusException("600100", "fileSuffix is wrong");
+        }
+
+        fileSuffix = "." + fileSuffix;
+
+        ServletInputStream in = null;
+
+        if (StringUtils.isNotBlank(md5)) {
+
+            try {
+                in = req.getInputStream();
+
+                //通用存储
+                FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
+                env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
+                env.setUserId(String.valueOf(accessUser.getUserId()));
+                env.setFileSuffix(fileSuffix);
+                YunPathInfo pi = FileStorageUtil.saveFile(siteId, env, in, md5);
+                String url = pi.getUrl();
+
+                url = new AES().encrypt(url);
+                return url;
+            } catch (IOException e) {
+                throw new ExamCloudRuntimeException(e);
+            } finally {
+                IOUtils.closeQuietly(in);
+            }
+
+        } else {
+
+            try {
+                FileUtils.forceMkdir(new File(systemConfig.getTempDataDir()));
+            } catch (IOException e1) {
+                log.error("fail to make dir. path=" + systemConfig.getTempDataDir());
+            }
+
+            String filePath = systemConfig.getTempDataDir() + File.separator + UUID.randomUUID()
+                    + fileSuffix;
+            File file = new File(filePath);
+
+            FileOutputStream out = null;
+            try {
+                in = req.getInputStream();
+                out = new FileOutputStream(file);
+                IOUtils.copy(in, out);
+
+                //通用存储
+                FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
+                env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
+                env.setUserId(String.valueOf(accessUser.getUserId()));
+                env.setFileSuffix(fileSuffix);
+                YunPathInfo pi = FileStorageUtil.saveFile(siteId, env, file, true);
+                String url = pi.getUrl();
+                url = new AES().encrypt(url);
+                return url;
+            } catch (IOException e) {
+                throw new ExamCloudRuntimeException(e);
+            } finally {
+                IOUtils.closeQuietly(in);
+                IOUtils.closeQuietly(out);
+                try {
+                    FileUtils.forceDelete(file);
+                } catch (IOException e) {
+                    log.error("fail to delete file. path=" + filePath);
+                }
+            }
+
+        }
+
+    }
 
 }

+ 64 - 61
examcloud-exchange-inner-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/inner/api/provider/UpyunCloudServiceProvider.java

@@ -2,6 +2,9 @@ package cn.com.qmth.examcloud.exchange.inner.api.provider;
 
 import java.io.IOException;
 
+import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
+import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
+import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.ModelAttribute;
@@ -36,66 +39,66 @@ import cn.com.qmth.examcloud.web.upyun.UpyunService;
 @RequestMapping("${$rmp.cloud.exchange.inner}/upyun")
 public class UpyunCloudServiceProvider extends ControllerSupport implements UpyunCloudService {
 
-	private static final long serialVersionUID = -6911335293165224994L;
-
-	@Autowired
-	UpyunService upyunService;
-
-	@Autowired
-	SystemProperties systemConfig;
-
-	AES aes = new AES();
-
-	@PostMapping("putFile")
-	@Override
-	public PutFileResp putFile(@ModelAttribute PutFileReq req) {
-		String fileSuffix = req.getFileSuffix();
-		String relativePath = req.getRelativePath();
-		if (StringUtils.isNotBlank(relativePath)) {
-			relativePath = PathUtil.startsWithoutSeparator(relativePath);
-		}
-		Long rootOrgId = req.getRootOrgId();
-		String rootOrgDomain = req.getRootOrgDomain();
-		Long userId = req.getUserId();
-		String siteId = req.getSiteId();
-		MultipartFile mf = req.getFile();
-
-		UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
-		env.setRootOrgId(String.valueOf(rootOrgId));
-		env.setRootOrgDomain(rootOrgDomain);
-		env.setUserId(String.valueOf(userId));
-		env.setFileSuffix(fileSuffix);
-		env.setRelativePath(relativePath);
-
-		env.setExt1(req.getExt1());
-		env.setExt2(req.getExt2());
-		env.setExt3(req.getExt3());
-		env.setExt4(req.getExt4());
-		env.setExt5(req.getExt5());
-
-		UpYunPathInfo path = null;
-		try {
-			path = upyunService.writeFile(siteId, env, mf.getInputStream(), "");
-		} catch (IOException e) {
-			throw new StatusException("005001", "fail to write to upyun", e);
-		}
-
-		PutFileResp resp = new PutFileResp();
-		resp.setUrl(path.getUrl());
-		resp.setRelativePath(path.getRelativePath());
-		return resp;
-	}
-
-	@PostMapping("deleteFile")
-	@Override
-	public DeleteFileResp deleteFile(@RequestBody DeleteFileReq req) {
-		String siteId = req.getSiteId();
-		String filePath = req.getFilePath();
-
-		upyunService.delete(siteId, filePath);
-
-		DeleteFileResp resp = new DeleteFileResp();
-		return resp;
-	}
+    private static final long serialVersionUID = -6911335293165224994L;
+
+    @Autowired
+    UpyunService upyunService;
+
+    @Autowired
+    SystemProperties systemConfig;
+
+    AES aes = new AES();
+
+    @PostMapping("putFile")
+    @Override
+    public PutFileResp putFile(@ModelAttribute PutFileReq req) {
+        String fileSuffix = req.getFileSuffix();
+        String relativePath = req.getRelativePath();
+        if (StringUtils.isNotBlank(relativePath)) {
+            relativePath = PathUtil.startsWithoutSeparator(relativePath);
+        }
+        Long rootOrgId = req.getRootOrgId();
+        String rootOrgDomain = req.getRootOrgDomain();
+        Long userId = req.getUserId();
+        String siteId = req.getSiteId();
+        MultipartFile mf = req.getFile();
+
+        FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
+        env.setRootOrgId(String.valueOf(rootOrgId));
+        env.setRootOrgDomain(rootOrgDomain);
+        env.setUserId(String.valueOf(userId));
+        env.setFileSuffix(fileSuffix);
+        env.setRelativePath(relativePath);
+
+        env.setExt1(req.getExt1());
+        env.setExt2(req.getExt2());
+        env.setExt3(req.getExt3());
+        env.setExt4(req.getExt4());
+        env.setExt5(req.getExt5());
+
+        YunPathInfo pi = null;
+        try {
+            pi = FileStorageUtil.saveFile(siteId, env, mf.getInputStream(), null);
+        } catch (IOException e) {
+            throw new StatusException("005001", "fail to write to upyun", e);
+        }
+
+        PutFileResp resp = new PutFileResp();
+        resp.setUrl(pi.getUrl());
+        resp.setRelativePath(pi.getRelativePath());
+        return resp;
+    }
+
+    @PostMapping("deleteFile")
+    @Override
+    public DeleteFileResp deleteFile(@RequestBody DeleteFileReq req) {
+        String siteId = req.getSiteId();
+        String filePath = req.getFilePath();
+
+        upyunService.delete(siteId, filePath);
+
+        DeleteFileResp resp = new DeleteFileResp();
+        return resp;
+    }
 
 }

+ 4 - 2
examcloud-exchange-outer-service/src/main/java/cn/com/qmth/examcloud/exchange/outer/service/impl/FaceServiceImpl.java

@@ -5,6 +5,8 @@ import java.net.URI;
 import java.util.List;
 import java.util.Map;
 
+import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
+import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpStatus;
@@ -86,12 +88,12 @@ public class FaceServiceImpl implements FaceService {
 		identityNumber = student.getIdentityNumber();
 		Long id = student.getId();
 
-		UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
+		FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
 		env.setFileSuffix(fileSuffix);
 		env.setRootOrgId(String.valueOf(rootOrgId));
 		env.setUserId(String.valueOf(id));
 
-		String photoUrl = upyunService.writeFile("studentBasePhoto", env, file, true).getUrl();
+		String photoUrl = FileStorageUtil.saveFile("studentBasePhoto", env, file, true).getUrl();
 
 		String faceToken = detect(photoUrl);
 

+ 57 - 0
examcloud-exchange-starter/src/main/resources/aliyun.xml

@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<sites>
+
+	<site>
+		<!--ID -->
+		<id>studentBasePhoto</id>
+		<!--名称 -->
+		<name>考生照片</name>
+		<!--又拍云账户ID标识 -->
+		<aliyunId>1</aliyunId>
+		<!--文件大小限制(IO流限制) -->
+		<maxSize>1M</maxSize>
+		<!--又拍云存储路径 -->
+		<path>/student_base_photo/${rootOrgId}/${userId}/${timeMillis}${fileSuffix}</path>
+	</site>
+
+	<site>
+		<id>resource</id>
+		<name>资源</name>
+		<aliyunId>1</aliyunId>
+		<maxSize>50M</maxSize>
+		<path>/resource/${relativePath}</path>
+	</site>
+
+	<site>
+		<id>capturePhoto</id>
+		<name>网考学生端抓拍照片</name>
+		<aliyunId>1</aliyunId>
+		<maxSize>1M</maxSize>
+		<path>/capture_photo/${rootOrgId}/${userId}/${timeMillis}${fileSuffix}</path>
+	</site>
+
+	<site>
+		<id>orgLogo</id>
+		<name>机构logo</name>
+		<aliyunId>1</aliyunId>
+		<maxSize>1M</maxSize>
+		<path>/org_logo/${rootOrgId}/${timeMillis}${fileSuffix}</path>
+	</site>
+
+	<site>
+		<id>orgPropertiesByOrgId</id>
+		<name>机构属性(机构ID查询)</name>
+		<aliyunId>1</aliyunId>
+		<maxSize>1M</maxSize>
+		<path>/org_properties/byOrgId/${rootOrgId}/${ext1}${fileSuffix}</path>
+	</site>
+
+	<site>
+		<id>orgPropertiesByOrgDomain</id>
+		<name>机构属性(机构域名查询)</name>
+		<aliyunId>1</aliyunId>
+		<maxSize>1M</maxSize>
+		<path>/org_properties/byOrgDomain/${rootOrgDomain}/${ext1}${fileSuffix}</path>
+	</site>
+
+</sites>