WANG 5 tahun lalu
induk
melakukan
1bb68ac69f

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

@@ -1,21 +1,29 @@
 package cn.com.qmth.examcloud.exchange.inner.api.controller;
 
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.util.IOUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
 import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.AES;
+import cn.com.qmth.examcloud.commons.util.MD5;
+import cn.com.qmth.examcloud.commons.util.UUID;
+import cn.com.qmth.examcloud.web.config.SystemConfig;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.web.upyun.UpyunPathEnvironmentInfo;
 import cn.com.qmth.examcloud.web.upyun.UpyunService;
@@ -33,14 +41,29 @@ import io.swagger.annotations.ApiOperation;
 public class UpyunController extends ControllerSupport {
 
 	@Autowired
-	UpyunService upyunService;
+	SystemConfig systemConfig;
 
-	AES aes = new AES();
+	@Autowired
+	UpyunService upyunService;
 
 	@ApiOperation(value = "保存照片")
 	@PutMapping("put/{siteId}/{fileSuffix}")
 	public String putFile(@PathVariable String siteId, @PathVariable String fileSuffix,
-			HttpServletRequest req) {
+			@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();
 
@@ -51,21 +74,64 @@ public class UpyunController extends ControllerSupport {
 		fileSuffix = "." + fileSuffix;
 
 		ServletInputStream in = null;
-		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, "").getUrl();
-			url = aes.encrypt(url);
-			return url;
-		} catch (IOException e) {
-			throw new ExamCloudRuntimeException(e);
-		} finally {
-			IOUtils.closeQuietly(in);
+
+		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);
+				}
+			}
+
 		}
+
 	}
 
 }