|
@@ -1,8 +1,10 @@
|
|
|
package org.jeecg.modules.ctop.service.impl;
|
|
|
|
|
|
import cn.com.ctop.common.utils.PropertiesUtils;
|
|
|
+import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import constant.KuaishouInterfaceConstant;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
@@ -45,17 +47,11 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FileInfoServiceImpl.class);
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> uploadVideoToTemplate(String accountId, Long videoFileId, String templatename) {
|
|
|
+ public Map<String, Object> uploadVideoToTemplate(String accountId, String videoUrl, String templatename) {
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
- FileInfo fileInfo = fileInfoMapper.selectById(videoFileId);
|
|
|
- if (null == fileInfo) {
|
|
|
- resultMap.put("message", "该视频文件信息不存在");
|
|
|
- resultMap.put("code", -1);
|
|
|
- return resultMap;
|
|
|
- }
|
|
|
//TODO查询是否已经上传过头条平台
|
|
|
CTopOauthToken token = tokenService.getOAuthTokenByAccountId(accountId);
|
|
|
- JSONObject resultObject = uploadAdvideo(token.getAccessToken(), fileInfo, token.getAccountId() + "");
|
|
|
+ JSONObject resultObject = uploadAdvideo(token.getAccessToken(), videoUrl, token.getAccountId() + "");
|
|
|
System.out.println(resultObject);
|
|
|
Integer code = resultObject.getInteger("code");
|
|
|
String message = resultObject.getString("message");
|
|
@@ -66,7 +62,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
return resultMap;
|
|
|
}
|
|
|
JSONObject data = resultObject.getJSONObject("data");
|
|
|
- ByteDanceVideoInfo videoInfo = new ByteDanceVideoInfo(data, token, videoFileId);
|
|
|
+ ByteDanceVideoInfo videoInfo = new ByteDanceVideoInfo(data, token);
|
|
|
videoInfoMapper.insert(videoInfo);
|
|
|
resultMap.put("code", 0);
|
|
|
resultMap.put("message", "视频文件上传成功");
|
|
@@ -79,22 +75,10 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
private ByteDanceImageInfoMapper imageInfoMapper;
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> uploadImageToTemplate(String accountId, Long imageFileId, String templatename) {
|
|
|
+ public Map<String, Object> uploadImageToTemplate(String accountId, String imageUrl, String templatename) {
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
- FileInfo fileInfo = fileInfoMapper.selectById(imageFileId);
|
|
|
- if (null == fileInfo) {
|
|
|
- resultMap.put("message", "该图片文件信息不存在");
|
|
|
- resultMap.put("code", -1);
|
|
|
- return resultMap;
|
|
|
- }
|
|
|
- if (null == fileInfo.getType() || !"IMAGE".equals(fileInfo.getType())) {
|
|
|
- resultMap.put("message", "文件格式错误");
|
|
|
- resultMap.put("code", -1);
|
|
|
- return resultMap;
|
|
|
- }
|
|
|
- //TODO查询是否已经上传过头条平台
|
|
|
CTopOauthToken token = tokenService.getOAuthTokenByAccountId(accountId);
|
|
|
- JSONObject resultObject = uploadAdImage(token.getAccessToken(), fileInfo, token.getAccountId() + "");
|
|
|
+ JSONObject resultObject = uploadAdImage(token.getAccessToken(), imageUrl, token.getAccountId() + "");
|
|
|
System.out.println(resultObject);
|
|
|
Integer code = resultObject.getInteger("code");
|
|
|
String message = resultObject.getString("message");
|
|
@@ -105,7 +89,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
return resultMap;
|
|
|
}
|
|
|
JSONObject data = resultObject.getJSONObject("data");
|
|
|
- ByteDanceImageInfo imageInfo = new ByteDanceImageInfo(data, token, imageFileId);
|
|
|
+ ByteDanceImageInfo imageInfo = new ByteDanceImageInfo(data, token);
|
|
|
imageInfoMapper.insert(imageInfo);
|
|
|
resultMap.put("code", 0);
|
|
|
resultMap.put("message", "图片文件上传成功");
|
|
@@ -169,22 +153,18 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public JSONObject uploadAdImage(String accessToken, FileInfo fileInfo, String advertiserId) {
|
|
|
+ public JSONObject uploadAdImage(String accessToken, String imageUrl, String advertiserId) {
|
|
|
// 请求地址
|
|
|
String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_file_image_ad");
|
|
|
// 构造请求
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.setHeader("Access-Token", accessToken);
|
|
|
- // 文件参数
|
|
|
- FileBody file = new FileBody(new File(fileInfo.getPath()));
|
|
|
- MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().addPart("image_file", file);
|
|
|
+ MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
|
|
|
// 其他参数
|
|
|
entityBuilder.addTextBody("advertiser_id", advertiserId);
|
|
|
- try {
|
|
|
- entityBuilder.addTextBody("image_signature", DigestUtils.md5Hex(new FileInputStream(fileInfo.getPath())));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ entityBuilder.addTextBody("upload_type", BytedanceInterfaceConstant.UPLOAD_TYPE_BY_URL);
|
|
|
+ entityBuilder.addTextBody("image_url", imageUrl);
|
|
|
+
|
|
|
HttpEntity entity = entityBuilder.build();
|
|
|
CloseableHttpResponse response = null;
|
|
|
CloseableHttpClient client = null;
|
|
@@ -220,26 +200,24 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public JSONObject uploadAdvideo(String accessToken, FileInfo fileInfo, String advertiserId) {
|
|
|
+ public JSONObject uploadAdvideo(String accessToken, String videoUrl, String advertiserId) {
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ CloseableHttpClient client = null;
|
|
|
// 请求地址
|
|
|
String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_file_video_ad");
|
|
|
// 构造请求
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.setHeader("Access-Token", accessToken);
|
|
|
// 文件参数
|
|
|
- FileBody file = new FileBody(new File(fileInfo.getPath()));
|
|
|
- MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().addPart("video_file", file);
|
|
|
- // 其他参数
|
|
|
- entityBuilder.addTextBody("advertiser_id", advertiserId);
|
|
|
- try {
|
|
|
- entityBuilder.addTextBody("video_signature", DigestUtils.md5Hex(new FileInputStream(fileInfo.getPath())));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- HttpEntity entity = entityBuilder.build();
|
|
|
- CloseableHttpResponse response = null;
|
|
|
- CloseableHttpClient client = null;
|
|
|
try {
|
|
|
+ URI uri = new URI(videoUrl);
|
|
|
+ FileBody file = new FileBody(new File(uri));
|
|
|
+ MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().addPart("video_file", file);
|
|
|
+ // 其他参数
|
|
|
+ entityBuilder.addTextBody("advertiser_id", advertiserId);
|
|
|
+ //TODO 需要后期优化修改代码逻辑
|
|
|
+ entityBuilder.addTextBody("image_signature", DigestUtils.md5Hex(new FileInputStream(new File(uri))));
|
|
|
+ HttpEntity entity = entityBuilder.build();
|
|
|
client = HttpClientBuilder.create().build();
|
|
|
httpPost.setURI(URI.create(url));
|
|
|
httpPost.setEntity(entity);
|
|
@@ -254,9 +232,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
bufferedReader.close();
|
|
|
return JSONObject.parseObject(result.toString());
|
|
|
}
|
|
|
- } catch (ClientProtocolException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
@@ -270,9 +246,6 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private FileInfoMapper fileInfoMapper;
|
|
|
@Autowired
|
|
|
private ICTopOauthTokenService tokenService;
|
|
|
}
|