|
@@ -11,6 +11,7 @@ import com.gis.common.util.AliyunOssUtil;
|
|
|
import com.gis.common.util.CmdUtils;
|
|
|
import com.gis.common.util.Result;
|
|
|
import com.gis.service.UploadService;
|
|
|
+import com.gis.service.queue.Variable;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -18,6 +19,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.util.Date;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -33,6 +35,12 @@ public class UploadServiceImpl implements UploadService {
|
|
|
@Autowired
|
|
|
AliyunOssUtil aliyunOssUtil;
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保证线程安全的
|
|
|
+ */
|
|
|
+ private static AtomicInteger count = new AtomicInteger();
|
|
|
+
|
|
|
@Override
|
|
|
public Result uploadFaceSwap(MultipartFile file, String type) {
|
|
|
String originalFilename = file.getOriginalFilename();
|
|
@@ -45,10 +53,30 @@ public class UploadServiceImpl implements UploadService {
|
|
|
String faceSwapName = time + "/faceSwap." + suffix;
|
|
|
log.info("faceSwapName: " + faceSwapName);
|
|
|
String ossUrl = null;
|
|
|
+
|
|
|
+ // 参数,用了区分调用不同的方法
|
|
|
+ String data = count.incrementAndGet()+"";
|
|
|
+ log.info("data: {}", data);
|
|
|
+ // 将数据存入队列中
|
|
|
+ boolean offer = Variable.queue.offer(data);
|
|
|
+
|
|
|
+ int size = Variable.queue.size();
|
|
|
+ log.info("当前队列数量: {}", size);
|
|
|
+
|
|
|
+ if (size > 8) {
|
|
|
+ // 消费消息
|
|
|
+ Variable.queue.poll();
|
|
|
+ return Result.failure(5006, "当前使用人数较多,请稍后再试~");
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
+
|
|
|
FileUtil.writeFromStream(file.getInputStream(), filePath);
|
|
|
log.info("文件写入完成");
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// 换装并上传
|
|
|
ossUrl = cmdReloadAndUploadOss(filePath, basePath, type, time);
|
|
|
return Result.success(ossUrl);
|
|
@@ -86,6 +114,8 @@ public class UploadServiceImpl implements UploadService {
|
|
|
String uploadJsonPath = basePath + "/upload.json";
|
|
|
if (!FileUtil.exist(uploadJsonPath)) {
|
|
|
log.error("算法合成失败,upload.json不存在");
|
|
|
+ // 消费消息
|
|
|
+ Variable.queue.poll();
|
|
|
throw new BaseRuntimeException(MsgCode.e5005, "算法合成失败,upload.json不存在");
|
|
|
}
|
|
|
log.info("换装完成: {}", basePath);
|
|
@@ -97,14 +127,14 @@ public class UploadServiceImpl implements UploadService {
|
|
|
if (status != 0) {
|
|
|
String msg = uploadJson.getString("msg");
|
|
|
log.error("算法合成失败: " + msg);
|
|
|
+ // 消费消息
|
|
|
+ Variable.queue.poll();
|
|
|
throw new BaseRuntimeException(MsgCode.e5005, "算法合成失败: " + msg);
|
|
|
}
|
|
|
|
|
|
- // 合成图片上传oss
|
|
|
-// String ossDir = configConstant.ossBasePath + dir;
|
|
|
-// aliyunOssUtil.uploadDir(basePath, ossDir);
|
|
|
-// String ossUrl = configConstant.ossDomain + ossDir;
|
|
|
-// log.info("oss上传完成: {}", ossUrl);
|
|
|
+ // 消费消息
|
|
|
+ Variable.queue.poll();
|
|
|
+
|
|
|
// 更改为读取本地服务器静态资源, 前端自己拼接域名+文件名
|
|
|
return dir;
|
|
|
|