WANG 6 سال پیش
والد
کامیت
1af0f3b53a

+ 30 - 4
examcloud-exchange-outer-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/controller/FaceController.java

@@ -9,6 +9,7 @@ import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.tomcat.util.http.fileupload.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -69,7 +70,19 @@ public class FaceController extends ControllerSupport {
 		DiskFileItem item = (DiskFileItem) file.getFileItem();
 		File storeLocation = item.getStoreLocation();
 		String fileName = file.getOriginalFilename();
-		faceService.processFace(rootOrgId, fileName, storeLocation, accessUser.getDisplayName());
+
+		if (StringUtils.containsWhitespace(fileName)) {
+			throw new StatusException("EX-600100", "文件名不能含有空白字符");
+		}
+
+		if (!fileName.matches("[^\\.\\s]+\\.[^\\.\\s]+")) {
+			throw new StatusException("EX-600101", "文件名不合法");
+		}
+		String identityNumber = fileName.substring(0, fileName.lastIndexOf("."));
+		String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
+
+		faceService.processFace(rootOrgId, identityNumber, fileSuffix, storeLocation,
+				accessUser.getDisplayName());
 	}
 
 	/**
@@ -117,19 +130,32 @@ public class FaceController extends ControllerSupport {
 
 		for (File f : files) {
 			Map<String, String> map = Maps.newHashMap();
+			String fileName = f.getName();
+			map.put("file", fileName);
 
 			if (f.length() > MAX_SIZE * 1024) {
-				map.put("file", f.getName());
 				map.put("statusCode", "EX-620005");
 				map.put("statusDesc", "文件大小超过500KB");
 				ret.add(map);
 				continue;
 			}
 			try {
-				map.put("file", f.getName());
-				faceService.processFace(rootOrgId, f.getName(), f, accessUser.getDisplayName());
+				if (StringUtils.containsWhitespace(fileName)) {
+					throw new StatusException("EX-600100", "文件名不能含有空白字符");
+				}
+
+				if (!fileName.matches("[^\\.\\s]+\\.[^\\.\\s]+")) {
+					throw new StatusException("EX-600101", "文件名不合法");
+				}
+				String identityNumber = fileName.substring(0, fileName.lastIndexOf("."));
+				String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
+
+				faceService.processFace(rootOrgId, identityNumber, fileSuffix, storeLocation,
+						accessUser.getDisplayName());
+
 				map.put("statusCode", "200");
 				map.put("statusDesc", "成功");
+
 			} catch (StatusException e) {
 				map.put("statusCode", e.getCode());
 				map.put("statusDesc", e.getDesc());

+ 33 - 8
examcloud-exchange-outer-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/provider/FaceOuterServiceProvider.java

@@ -9,6 +9,7 @@ import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -61,8 +62,8 @@ public class FaceOuterServiceProvider extends ControllerSupport implements FaceO
 	@ApiOperation(value = "保存学生照片")
 	@PostMapping("addByUrl")
 	@Override
-	public void addPhotoByUrl(@RequestParam Long rootOrgId, @RequestParam String photoUrl,
-			@RequestParam String operator) {
+	public void addPhotoByUrl(@RequestParam Long rootOrgId, @RequestParam String identityNumber,
+			@RequestParam String photoUrl, @RequestParam String operator) {
 
 		if (photoUrl.startsWith("http")) {
 			byte[] bs = HttpClientUtil.get(photoUrl);
@@ -84,7 +85,7 @@ public class FaceOuterServiceProvider extends ControllerSupport implements FaceO
 				throw new StatusException("EX-100003", "文件读写失败");
 			}
 
-			faceService.processFace(rootOrgId, fileName, temp, operator);
+			faceService.processFace(rootOrgId, identityNumber, fileSuffix, temp, operator);
 		}
 
 	}
@@ -96,11 +97,22 @@ public class FaceOuterServiceProvider extends ControllerSupport implements FaceO
 		User accessUser = getAccessUser();
 		Long rootOrgId = accessUser.getRootOrgId();
 
-		log.debug("fileName =" + file.getOriginalFilename());
 		DiskFileItem item = (DiskFileItem) file.getFileItem();
 		File storeLocation = item.getStoreLocation();
 		String fileName = file.getOriginalFilename();
-		faceService.processFace(rootOrgId, fileName, storeLocation, accessUser.getDisplayName());
+
+		if (StringUtils.containsWhitespace(fileName)) {
+			throw new StatusException("EX-600100", "文件名不能含有空白字符");
+		}
+
+		if (!fileName.matches("[^\\.\\s]+\\.[^\\.\\s]+")) {
+			throw new StatusException("EX-600101", "文件名不合法");
+		}
+		String identityNumber = fileName.substring(0, fileName.lastIndexOf("."));
+		String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
+
+		faceService.processFace(rootOrgId, identityNumber, fileSuffix, storeLocation,
+				accessUser.getDisplayName());
 	}
 
 	@ApiOperation(value = "导入学生照片")
@@ -142,19 +154,32 @@ public class FaceOuterServiceProvider extends ControllerSupport implements FaceO
 
 		for (File f : files) {
 			Map<String, String> map = Maps.newHashMap();
+			String fileName = f.getName();
+			map.put("file", fileName);
 
 			if (f.length() > MAX_SIZE * 1024) {
-				map.put("file", f.getName());
 				map.put("statusCode", "EX-620005");
 				map.put("statusDesc", "文件大小超过500KB");
 				ret.add(map);
 				continue;
 			}
 			try {
-				map.put("file", f.getName());
-				faceService.processFace(rootOrgId, f.getName(), f, accessUser.getDisplayName());
+				if (StringUtils.containsWhitespace(fileName)) {
+					throw new StatusException("EX-600100", "文件名不能含有空白字符");
+				}
+
+				if (!fileName.matches("[^\\.\\s]+\\.[^\\.\\s]+")) {
+					throw new StatusException("EX-600101", "文件名不合法");
+				}
+				String identityNumber = fileName.substring(0, fileName.lastIndexOf("."));
+				String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
+
+				faceService.processFace(rootOrgId, identityNumber, fileSuffix, storeLocation,
+						accessUser.getDisplayName());
+
 				map.put("statusCode", "200");
 				map.put("statusDesc", "成功");
+
 			} catch (StatusException e) {
 				map.put("statusCode", e.getCode());
 				map.put("statusDesc", e.getDesc());

+ 3 - 2
examcloud-exchange-outer-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/provider/StudentOuterServiceProvider.java

@@ -62,6 +62,7 @@ public class StudentOuterServiceProvider extends ControllerSupport implements St
 		trim(req);
 
 		Long rootOrgId = req.getRootOrgId();
+		String identityNumber = req.getIdentityNumber();
 
 		SaveStudentReq request = new SaveStudentReq();
 		request.setIdentityNumber(req.getIdentityNumber());
@@ -95,10 +96,10 @@ public class StudentOuterServiceProvider extends ControllerSupport implements St
 			try {
 				FileUtils.writeByteArrayToFile(temp, bs);
 			} catch (IOException e) {
-				throw new StatusException("EX-100003", "文件读写失败");
+				throw new StatusException("EX-100003", "文件读写失败", e);
 			}
 
-			faceService.processFace(rootOrgId, fileName, temp, req.getOperator());
+			faceService.processFace(rootOrgId, identityNumber, fileSuffix, temp, req.getOperator());
 		}
 
 		OuterSaveStudentResp resp = new OuterSaveStudentResp();

+ 1 - 1
examcloud-exchange-outer-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/provider/cug/CugOuterServiceProvider.java

@@ -133,7 +133,7 @@ public class CugOuterServiceProvider extends ControllerSupport implements CugOut
 				throw new StatusException("EX-100003", "文件读写失败");
 			}
 
-			faceService.processFace(rootOrgId, fileName, temp, req.getOperator());
+			faceService.processFace(rootOrgId, identityNumber, fileSuffix, temp, req.getOperator());
 		}
 
 		GetExamReq getExamReq = new GetExamReq();

+ 2 - 2
examcloud-exchange-outer-api/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/FaceOuterService.java

@@ -17,8 +17,8 @@ import cn.com.qmth.examcloud.commons.web.cloud.api.OuterService;
  */
 public interface FaceOuterService extends OuterService {
 
-	void addPhotoByUrl(@RequestParam Long rootOrgId, @RequestParam String photoUrl,
-			@RequestParam String operator);
+	void addPhotoByUrl(@RequestParam Long rootOrgId, @RequestParam String identityNumber,
+			@RequestParam String photoUrl, @RequestParam String operator);
 
 	void addPhoto(@RequestParam CommonsMultipartFile file);
 

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

@@ -16,10 +16,12 @@ public interface FaceService {
 	 *
 	 * @author WANGWEI
 	 * @param rootOrgId
-	 * @param fileName
+	 * @param identityNumber
+	 * @param fileSuffix
 	 * @param file
 	 * @param operator
 	 */
-	void processFace(Long rootOrgId, String fileName, File file, String operator);
+	void processFace(Long rootOrgId, String identityNumber, String fileSuffix, File file,
+			String operator);
 
 }

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

@@ -17,7 +17,6 @@ import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
@@ -75,19 +74,10 @@ public class FaceServiceImpl implements FaceService {
 	private String apiSecret;
 
 	@Override
-	public void processFace(Long rootOrgId, String fileName, File file, String operator) {
+	public void processFace(Long rootOrgId, String identityNumber, String fileSuffix, File file,
+			String operator) {
 
-		if (StringUtils.containsWhitespace(fileName)) {
-			throw new StatusException("EX-600100", "文件名不能含有空白字符");
-		}
-
-		if (!fileName.matches("[^\\.\\s]+\\.[^\\.\\s]+")) {
-			throw new StatusException("EX-600101", "文件名不合法");
-		}
-
-		String identityNumber = fileName.substring(0, fileName.lastIndexOf("."));
-		String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
-		String randomFileName = System.currentTimeMillis() + fileSuffix;
+		String photoName = "photo" + fileSuffix;
 
 		GetStudentReq req = new GetStudentReq();
 		req.setIdentityNumber(identityNumber);
@@ -96,7 +86,7 @@ public class FaceServiceImpl implements FaceService {
 		StudentBean student = resp.getStudentInfo();
 		identityNumber = student.getIdentityNumber();
 
-		String photoUrl = upyunService.writeFile(rootOrgId, identityNumber, randomFileName, file);
+		String photoUrl = upyunService.writeFile(rootOrgId, identityNumber, photoName, file);
 
 		String faceToken = detect(photoUrl);
 
@@ -109,7 +99,7 @@ public class FaceServiceImpl implements FaceService {
 		request.setFacesetToken(facesetToken);
 		request.setFaceToken(faceToken);
 		request.setOperator(operator);
-		request.setPhotoName(randomFileName);
+		request.setPhotoName(photoName);
 		request.setRootOrgId(rootOrgId);
 		request.setStudentId(student.getId());
 		request.setFaceCount(faceCount);