WANG 6 年之前
父節點
當前提交
45416d3665

+ 2 - 4
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/UpyunService.java

@@ -1,6 +1,5 @@
 package cn.com.qmth.examcloud.exchange.inner.service;
 
-import java.io.File;
 import java.io.InputStream;
 
 import cn.com.qmth.examcloud.exchange.inner.service.bean.UpyunPathEnvironmentInfo;
@@ -14,16 +13,15 @@ import cn.com.qmth.examcloud.exchange.inner.service.bean.UpyunPathEnvironmentInf
  */
 public interface UpyunService {
 
-	String writeFile(Long rootOrgId, String identityNumber, String fileName, File file);
-
 	/**
 	 * 写文件
 	 *
 	 * @author WANGWEI
+	 * @param siteId
 	 * @param env
 	 * @param in
 	 * @return
 	 */
-	String writeFile(UpyunPathEnvironmentInfo env, InputStream in);
+	String writeFile(String siteId, UpyunPathEnvironmentInfo env, InputStream in);
 
 }

+ 3 - 3
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/bean/UpyunPathEnvironmentInfo.java

@@ -31,7 +31,7 @@ public class UpyunPathEnvironmentInfo implements JsonSerializable {
 	/**
 	 * 文件后缀(以"."开头,如 ".jpg",".zip")
 	 */
-	private Long fileSuffix;
+	private String fileSuffix;
 
 	public Long getRootOrgId() {
 		return rootOrgId;
@@ -57,11 +57,11 @@ public class UpyunPathEnvironmentInfo implements JsonSerializable {
 		this.currentTimeMillis = currentTimeMillis;
 	}
 
-	public Long getFileSuffix() {
+	public String getFileSuffix() {
 		return fileSuffix;
 	}
 
-	public void setFileSuffix(Long fileSuffix) {
+	public void setFileSuffix(String fileSuffix) {
 		this.fileSuffix = fileSuffix;
 	}
 

+ 12 - 41
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/impl/UpyunServiceImpl.java

@@ -1,21 +1,17 @@
 package cn.com.qmth.examcloud.exchange.inner.service.impl;
 
-import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
-import java.util.Locale;
 
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
-import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
-import cn.com.qmth.examcloud.commons.base.util.UrlUtil;
+import cn.com.qmth.examcloud.commons.base.util.FreeMarkerUtil;
 import cn.com.qmth.examcloud.exchange.inner.service.UpyunService;
 import cn.com.qmth.examcloud.exchange.inner.service.bean.UpyunPathEnvironmentInfo;
-import main.java.com.UpYun;
-import main.java.com.upyun.UpYunUtils;
+import cn.com.qmth.examcloud.exchange.inner.service.upyun.UpYunClient;
+import cn.com.qmth.examcloud.exchange.inner.service.upyun.UpyunSite;
+import cn.com.qmth.examcloud.exchange.inner.service.upyun.UpyunSiteManager;
 
 /**
  * 类注释
@@ -25,45 +21,20 @@ import main.java.com.upyun.UpYunUtils;
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  */
 @Service
-public class UpyunServiceImpl  implements UpyunService {
+public class UpyunServiceImpl implements UpyunService {
 
 	protected ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
 
-	@Value("${$UpYun.bucketName}")
-	private String bucketName;
-
-	@Value("${$UpYun.userName}")
-	private String userName;
-
-	@Value("${$UpYun.password}")
-	private String password;
-
-	@Value("${$UpYun.studentPhotoUrlBasePath}")
-	private String studentPhotoUrlBasePath;
-
 	@Override
-	public String writeFile(Long rootOrgId, String identityNumber, String fileName, File file) {
-		String photoPath = rootOrgId + "/" + UrlUtil.encode(identityNumber).toUpperCase(Locale.US)
-				+ "/" + fileName;
-
-		String filePath = "/student_base_photo" + "/" + photoPath;
+	public String writeFile(String siteId, UpyunPathEnvironmentInfo env, InputStream in) {
+		env.setCurrentTimeMillis(System.currentTimeMillis());
 
-//		try {
-////			this.writeFile(filePath, file, true);
-//		} catch (IOException e) {
-//			log.error("[UpYun] fail to write file. filePath=" + filePath, e);
-//			throw new StatusException("EX-360001", "上传文件到UpYun失败", e);
-//		}
-
-		String photoUrl = studentPhotoUrlBasePath + filePath;
-
-		return photoUrl;
-	}
-
-	@Override
-	public String writeFile(UpyunPathEnvironmentInfo env, InputStream in) {
+		UpyunSite upyunSite = UpyunSiteManager.getUpyunSite(siteId);
+		UpYunClient upYunClient = UpyunSiteManager.getUpYunClient(siteId);
 
-		return null;
+		String path = FreeMarkerUtil.process(upyunSite.getPath(), env);
+		String url = upYunClient.writeFile(path, in);
+		return url;
 	}
 
 }

+ 12 - 10
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/upyun/UpYunClient.java

@@ -9,6 +9,7 @@ import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
 
+import org.apache.commons.compress.utils.IOUtils;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -86,26 +87,24 @@ public class UpYunClient {
 	 * @param filePath
 	 * @param in
 	 * @return
-	 * @throws UpException
 	 */
-	public String writeFile(String filePath, InputStream in) throws UpException {
+	public String writeFile(String filePath, InputStream in) {
 
 		String url = "http://" + API_DOMAIN + filePath;
 
 		HttpPut httpPut = new HttpPut(url);
 		httpPut.setConfig(this.requestConfig);
 
-		String date = getGMTDate();
-		httpPut.addHeader(DATE, date);
-
-		httpPut.addHeader(AUTHORIZATION,
-				UpYunUtils.sign(METHOD_PUT, date, filePath, userName, password, null).trim());
+		try {
+			String date = getGMTDate();
+			httpPut.addHeader(DATE, date);
 
-		httpPut.addHeader(MKDIR, "true");
+			httpPut.addHeader(AUTHORIZATION,
+					UpYunUtils.sign(METHOD_PUT, date, filePath, userName, password, null).trim());
 
-		httpPut.setEntity(new InputStreamEntity(in));
+			httpPut.addHeader(MKDIR, "true");
 
-		try {
+			httpPut.setEntity(new InputStreamEntity(in));
 			CloseableHttpResponse response = httpclient.execute(httpPut);
 			int statusCode = response.getStatusLine().getStatusCode();
 			if (HttpStatus.SC_OK != statusCode) {
@@ -113,10 +112,13 @@ public class UpYunClient {
 			}
 		} catch (StatusException e) {
 			throw e;
+		} catch (UpException e) {
+			throw new ExamCloudRuntimeException(e);
 		} catch (Exception e) {
 			throw new ExamCloudRuntimeException(e);
 		} finally {
 			httpPut.releaseConnection();
+			IOUtils.closeQuietly(in);
 		}
 
 		String path = "	http://" + bucketName + ".b0.upaiyun.com" + filePath;

+ 66 - 0
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/upyun/UpyunSite.java

@@ -0,0 +1,66 @@
+package cn.com.qmth.examcloud.exchange.inner.service.upyun;
+
+import cn.com.qmth.examcloud.commons.web.cloud.api.JsonSerializable;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年11月21日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class UpyunSite implements JsonSerializable {
+
+	private static final long serialVersionUID = -1754474062438702321L;
+
+	private String id;
+
+	private String name;
+
+	private String upyunId;
+
+	private String maxSize;
+
+	private String path;
+
+	public String getId() {
+		return id;
+	}
+
+	public void setId(String id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getUpyunId() {
+		return upyunId;
+	}
+
+	public void setUpyunId(String upyunId) {
+		this.upyunId = upyunId;
+	}
+
+	public String getMaxSize() {
+		return maxSize;
+	}
+
+	public void setMaxSize(String maxSize) {
+		this.maxSize = maxSize;
+	}
+
+	public String getPath() {
+		return path;
+	}
+
+	public void setPath(String path) {
+		this.path = path;
+	}
+
+}

+ 109 - 0
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/upyun/UpyunSiteManager.java

@@ -0,0 +1,109 @@
+package cn.com.qmth.examcloud.exchange.inner.service.upyun;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.thoughtworks.xstream.XStream;
+
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.commons.base.helpers.XStreamBuilder;
+import cn.com.qmth.examcloud.commons.base.util.PathUtil;
+import cn.com.qmth.examcloud.commons.base.util.PropertiesUtil;
+
+/**
+ * upyun site manager
+ *
+ * @author WANGWEI
+ * @date 2018年11月21日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class UpyunSiteManager {
+
+	private static final Map<String, UpYunClient> CLIENT_HOLDERS = new ConcurrentHashMap<>();
+
+	private static final Map<String, UpyunSite> SITE_HOLDERS = new ConcurrentHashMap<>();
+
+	public static void init() {
+		String resoucePath = PathUtil.getResoucePath("upyun.xml");
+		File file = new File(resoucePath);
+
+		XStream xStream = XStreamBuilder.newInstance().build();
+		xStream.allowTypes(new Class[]{UpyunSite.class, List.class});
+		xStream.alias("sites", List.class);
+		xStream.alias("site", UpyunSite.class);
+
+		List<UpyunSite> list = null;
+		try {
+			@SuppressWarnings("unchecked")
+			List<UpyunSite> obj = (List<UpyunSite>) xStream.fromXML(file);
+			list = obj;
+		} catch (Exception e) {
+			throw new StatusException("EX-520001", "upyun.xml is wrong", e);
+		}
+
+		for (UpyunSite upyunSite : list) {
+
+			SITE_HOLDERS.put(upyunSite.getId(), upyunSite);
+
+			String upyunId = upyunSite.getUpyunId();
+			String bucketName = PropertiesUtil.getString("$upyun.site." + upyunId + ".bucketName");
+			String userName = PropertiesUtil.getString("$upyun.site." + upyunId + ".userName");
+			String password = PropertiesUtil.getString("$upyun.site." + upyunId + ".password");
+
+			if (StringUtils.isBlank(bucketName)) {
+				throw new StatusException("EX-520002",
+						"bucketName is not configured. upyunId=" + upyunId);
+			}
+			if (StringUtils.isBlank(userName)) {
+				throw new StatusException("EX-520003",
+						"userName is not configured. upyunId=" + upyunId);
+			}
+			if (StringUtils.isBlank(password)) {
+				throw new StatusException("EX-520004",
+						"password is not configured. upyunId=" + upyunId);
+			}
+
+			UpYunClient upYunClient = new UpYunClient(bucketName, userName, password);
+
+			CLIENT_HOLDERS.put(upyunSite.getId(), upYunClient);
+		}
+
+	}
+
+	/**
+	 * 方法注释
+	 *
+	 * @author WANGWEI
+	 * @param siteId
+	 * @return
+	 */
+	public static UpYunClient getUpYunClient(String siteId) {
+		UpYunClient upYunClient = CLIENT_HOLDERS.get(siteId);
+
+		if (null == upYunClient) {
+			throw new StatusException("EX-520005", "upYunClient is null");
+		}
+		return upYunClient;
+	}
+
+	/**
+	 * 方法注释
+	 *
+	 * @author WANGWEI
+	 * @param siteId
+	 * @return
+	 */
+	public static UpyunSite getUpyunSite(String siteId) {
+		UpyunSite upyunSite = SITE_HOLDERS.get(siteId);
+
+		if (null == upyunSite) {
+			throw new StatusException("EX-520006", "upyunSite is null");
+		}
+		return upyunSite;
+	}
+
+}

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

@@ -1,6 +1,8 @@
 package cn.com.qmth.examcloud.exchange.outer.service.impl;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
@@ -14,6 +16,7 @@ import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
+import org.apache.poi.util.IOUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -23,6 +26,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Maps;
 
+import cn.com.qmth.examcloud.commons.base.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
@@ -38,6 +42,7 @@ import cn.com.qmth.examcloud.core.basic.api.request.SaveStudentFaceReq;
 import cn.com.qmth.examcloud.core.basic.api.response.GetStudentResp;
 import cn.com.qmth.examcloud.core.basic.api.response.GetUsableFacesetListResp;
 import cn.com.qmth.examcloud.exchange.inner.service.UpyunService;
+import cn.com.qmth.examcloud.exchange.inner.service.bean.UpyunPathEnvironmentInfo;
 import cn.com.qmth.examcloud.exchange.outer.service.FaceService;
 
 /**
@@ -85,8 +90,24 @@ public class FaceServiceImpl implements FaceService {
 		GetStudentResp resp = studentCloudService.getStudent(req);
 		StudentBean student = resp.getStudentInfo();
 		identityNumber = student.getIdentityNumber();
-
-		String photoUrl = upyunService.writeFile(rootOrgId, identityNumber, photoName, file);
+		Long id = student.getId();
+
+		UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
+		env.setFileSuffix(fileSuffix);
+		env.setRootOrgId(rootOrgId);
+		env.setUserId(id);
+		InputStream in = null;
+		String photoUrl = null;
+		try {
+			in = new FileInputStream(file);
+			photoUrl = upyunService.writeFile("studentBasePhoto", env, in);
+		} catch (StatusException e) {
+			throw e;
+		} catch (Exception e) {
+			throw new ExamCloudRuntimeException(e);
+		} finally {
+			IOUtils.closeQuietly(in);
+		}
 
 		String faceToken = detect(photoUrl);