获取七牛token返回给前端
public RestResult minGetCosplayToken(RequestBean model) {
CosplayPerson cosplayPerson = cosplayPersonService.queryByOId(model.getPersonId(), model.getClientId());
if (cosplayPerson == null) {
return RestResult.error("cosplay area is not found");
}
String bucket = "wilssir";
long expireSeconds = 600; //过期时间
String body = "clientId=" + cosplayPerson.getClientId() +
"&nickName=" + "nickNamw" +
"&userId=" + "userId" +
"&personId=" + "personId" +
"&key=$(key)" +
"&width=$(imageInfo.width)" +
"&height=$(imageInfo.height)";
StringMap putPolicy = new StringMap();
putPolicy.put("scope", Constant.getQiNiuBucket());
putPolicy.put("callbackUrl", Constant.getQiNiuCallbackUrl());
putPolicy.put("callbackBody", body);
String token = QiNiuUtils.getQiNiuToken(bucket, expireSeconds, putPolicy);
return RestResult.ok(token);
}
回调
public RestResult callBack(HttpServletRequest request) {
try {
String clientId = request.getParameter("clientId");
String userId = request.getParameter("userId");
String personId = request.getParameter("personId");
String key = request.getParameter("key");
String width = request.getParameter("width");
String height = request.getParameter("height");
String nickName = request.getParameter("nickName");
CosplayPerson person = cosplayPersonService.queryByOId(personId, null);
if (person == null) {
return RestResult.error("person not found");
}
// add library
Library library = new Library();
library.setOId(UUIDGenerator.getUUID());
library.setUserId(userId);
library.setName("图中换人");
library.setTags("图中换人,用户上传");
library.setModule("Cosplay");
library.setSubject("User");
library.setOldUrl(key);
library.setWidth(Long.valueOf(width));
library.setHeight(Long.valueOf(height));
library.setDateline(TimeUtils.getCurrentUnixTime());
library.setStatus(200L);
// add cosplay
Cosplay cosplay = new Cosplay();
cosplay.setOId(UUIDGenerator.getUUID());
cosplay.setName("baidu:v2.0");
cosplay.setUserId(userId);
cosplay.setDateline(TimeUtils.getCurrentUnixTime());
cosplay.setSubject("picture");
cosplay.setTargetId(person.getTargetId());
cosplay.setTargetNumber(person.getTargetNumber());
cosplay.setPersonId(person.getOId());
cosplay.setPersonName(person.getName());
cosplay.setOldImageUrl(person.getPartImageUrl());
cosplay.setStartShowTime(TimeUtils.getCurrentUnixTime());
cosplay.setEndShowTime(cosplay.getStartShowTime() + 60);
cosplay.setIsStar(0L);// 不参与点赞活动
cosplay.setStar(0L);
// cosplay.setStarTime(0L);
// cosplay.setStarEndTime(0L);
// cosplay.setStarLimit(1L);
// request baidu faceMerge
RestResult restResult = FaceMergeUtils.faceMerge(person.getPartImageUrl(), key);
if (restResult.getCode() != 200) { //fail add errInfo
cosplay.setStatus(400L);
cosplay.setErrInfo(restResult.getData());
cosplayService.add(cosplay);
return restResult;
}
String data = (String) restResult.getData();
cosplay.setStatus(200L);
cosplay.setNewImageUrl(data);
cosplayService.add(cosplay);
library.setNewUrl(data);
libraryService.add(library);
Map<String, Object> map = new HashMap<>();
map.put("cosplay", cosplay);
//send socket
MinUserSocket.SendMessageAll(JSON.toJSONString(map));
String start = TimeUtils.unixToTime(cosplay.getStartShowTime(), "HH:mm:ss");
String end = TimeUtils.unixToTime(cosplay.getEndShowTime(), "HH:mm:ss");
map.put("msg", start + "-" + end);
return RestResult.ok(map);
} catch (Exception e) {
e.printStackTrace();
return RestResult.exception();
}
}