|
@@ -1,22 +1,46 @@
|
|
|
-package cn.com.ctop.track.service.impl;
|
|
|
+package cn.com.ctop.material.service.impl;
|
|
|
|
|
|
-import cn.com.ctop.track.entity.MaterialUploadTask;
|
|
|
-import cn.com.ctop.track.entity.TaskVo;
|
|
|
-import cn.com.ctop.track.mapper.MaterialUploadTaskMapper;
|
|
|
-import cn.com.ctop.track.service.IMaterialUploadTaskService;
|
|
|
-import cn.com.ctop.track.utils.Check;
|
|
|
-import cn.com.ctop.track.utils.LoadFileUtil;
|
|
|
+import cn.com.ctop.material.entity.MaterialUploadTask;
|
|
|
+import cn.com.ctop.material.entity.TaskVo;
|
|
|
+import cn.com.ctop.material.enums.MaterialEnum;
|
|
|
+import cn.com.ctop.material.mapper.MaterialUploadTaskMapper;
|
|
|
+import cn.com.ctop.material.service.IMaterialUploadTaskService;
|
|
|
+import cn.com.ctop.material.utils.Check;
|
|
|
+import cn.com.ctop.material.utils.HttpConstant;
|
|
|
+import cn.com.ctop.material.utils.LoadFileUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.entity.mime.content.FileBody;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.URI;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
@@ -34,66 +58,66 @@ public class MaterialUploadTaskServiceImpl extends ServiceImpl<MaterialUploadTas
|
|
|
private MaterialUploadTaskMapper materialUploadTaskMapper;
|
|
|
// private static String downloadUrl = "/data/file/video/";
|
|
|
private static String downloadUrl = "D:\\file";
|
|
|
- private static ExecutorService kuaishouTiaoExecutorService = Executors.newFixedThreadPool(5);
|
|
|
+ private static ExecutorService kuaishouExecutorService = Executors.newFixedThreadPool(5);
|
|
|
+ private static ExecutorService bytedanceExecutorService = Executors.newFixedThreadPool(5);
|
|
|
+ private static ExecutorService submitExecutorService = Executors.newFixedThreadPool(5);
|
|
|
|
|
|
@Override
|
|
|
public void upload(String signature) {
|
|
|
- TaskVo taskVo = getTaskList(signature);
|
|
|
- if (Check.isNull(taskVo)) {
|
|
|
- log.info("此素材md5未获取到待上传的任务列表");
|
|
|
- return;
|
|
|
- }
|
|
|
- List<MaterialUploadTask> taskList = taskVo.getTaskList();
|
|
|
- if (Check.isNull(taskList)) {
|
|
|
- log.info("此素材md5未获取到待上传的任务列表");
|
|
|
- return;
|
|
|
- }
|
|
|
- MaterialUploadTask materialUploadTask = taskList.get(0);
|
|
|
- String materialUrl = materialUploadTask.getMaterialUrl();
|
|
|
- String localUrl = LoadFileUtil.downLoadFromUrl(materialUrl, downloadUrl);
|
|
|
- File file = new File(localUrl);
|
|
|
- FileSystemResource resource = null;
|
|
|
- Integer materialType = null;
|
|
|
- if (!Check.isNull(taskVo.getKuaiShou())) {
|
|
|
- resource = new FileSystemResource(file);
|
|
|
-
|
|
|
- JSONObject sizeJson = materialUploadTaskMapper.getVideoSize(signature);
|
|
|
- if (Check.isNull(sizeJson)) {
|
|
|
- Integer type = MaterialEnum.getTypeBySize(Integer.valueOf(materialParameter.getWidth()), Integer.valueOf(materialParameter.getHeight()));
|
|
|
- if (!Check.isNull(type) && type != 0) {
|
|
|
- requestJson.put("type", type);
|
|
|
+ submitExecutorService.submit(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ TaskVo taskVo = getTaskList(signature);
|
|
|
+ if (Check.isNull(taskVo)) {
|
|
|
+ log.info("此素材md5未获取到待上传的任务列表");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<MaterialUploadTask> taskList = taskVo.getTaskList();
|
|
|
+ if (Check.isNull(taskList)) {
|
|
|
+ log.info("此素材md5未获取到待上传的任务列表");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ MaterialUploadTask materialUploadTask = taskList.get(0);
|
|
|
+ String materialUrl = materialUploadTask.getMaterialUrl();
|
|
|
+ String localUrl = LoadFileUtil.downLoadFromUrl(materialUrl, downloadUrl);
|
|
|
+ File file = new File(localUrl);
|
|
|
+ FileSystemResource resource = null;
|
|
|
+ Integer materialType = null;
|
|
|
+ if (!Check.isNull(taskVo.getKuaiShou())) {
|
|
|
+ resource = new FileSystemResource(file);
|
|
|
+ JSONObject sizeJson = materialUploadTaskMapper.getVideoSize(signature);
|
|
|
+ if (!Check.isNull(sizeJson)) {
|
|
|
+ Integer width = sizeJson.getInteger("width");
|
|
|
+ Integer height = sizeJson.getInteger("height");
|
|
|
+ Integer type = MaterialEnum.getTypeBySize(width, height);
|
|
|
+ if (!Check.isNull(type) && type != 0) {
|
|
|
+ materialType = type;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileBody fileBody = null;
|
|
|
+ if (!Check.isNull(taskVo.getBytedance())) {
|
|
|
+ fileBody = new FileBody(file);
|
|
|
+ }
|
|
|
+ for (MaterialUploadTask task : taskList) {
|
|
|
+ if (task.getMediaId() == 2 || task.getMediaId() == 4) {
|
|
|
+ uploadKuaiShou(task, resource, materialType);
|
|
|
+ } else if (task.getMediaId() == 1 || task.getMediaId() == 3) {
|
|
|
+ uploadBytedance(fileBody, task);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- }
|
|
|
- FileBody fileBody = null;
|
|
|
- if (!Check.isNull(taskVo.getBytedance())) {
|
|
|
- fileBody = new FileBody(file);
|
|
|
- }
|
|
|
-
|
|
|
- for (MaterialUploadTask task : taskList) {
|
|
|
- if (task.getMediaId() == 2 || task.getMediaId() == 4) {
|
|
|
- uploadKuaiShou(task, resource);
|
|
|
-
|
|
|
- } else if (task.getMediaId() == 1 || task.getMediaId() == 3) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private void uploadKuaiShou(MaterialUploadTask task, FileSystemResource resource) {
|
|
|
- kuaishouTiaoExecutorService.submit(new Runnable() {
|
|
|
+ private void uploadKuaiShou(MaterialUploadTask task, FileSystemResource resource, Integer type) {
|
|
|
+ kuaishouExecutorService.submit(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
Long accountId = task.getAccountId();
|
|
|
if (Check.isNull(accountId)) {
|
|
|
- log.error("账户不能为空");
|
|
|
+ log.error("账户不能为空。");
|
|
|
return;
|
|
|
}
|
|
|
String token = materialUploadTaskMapper.getTokenByAccountId(accountId);
|
|
@@ -101,16 +125,122 @@ public class MaterialUploadTaskServiceImpl extends ServiceImpl<MaterialUploadTas
|
|
|
log.error("此账户id未获取到token,accountId:{}", accountId);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ Map<String, String> headerMap = new HashMap<>();
|
|
|
+ headerMap.put("Content-Type", "multipart/form-data");
|
|
|
+ headerMap.put("Access-Token", token);
|
|
|
+ JSONObject requestJson = new JSONObject();
|
|
|
+ requestJson.put("file", resource);
|
|
|
+ requestJson.put("signature", task.getSignature());
|
|
|
+ if (!Check.isNull(task.getShieldBackwardSwitch())) {
|
|
|
+ if (task.getShieldBackwardSwitch() == 1) {
|
|
|
+ requestJson.put("shield_backward_switch", true);
|
|
|
+ } else {
|
|
|
+ requestJson.put("shield_backward_switch", false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!Check.isNull(type)) {
|
|
|
+ requestJson.put("type", type);
|
|
|
+ }
|
|
|
+ requestJson.put("photo_name", task.getMaterialName());
|
|
|
+ requestJson.put("advertiser_id", accountId);
|
|
|
+ String url = HttpConstant.VIDEO_UPLOAD;
|
|
|
+ String result = exceptInfoForRestTemplate(url, requestJson, headerMap);
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
+ if (resultJson.getInteger("code") == 0) {
|
|
|
+ materialUploadTaskMapper.syncKuaishou(task.getSignature());
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 3, "同步成功", new Date());
|
|
|
+ log.info("快手素材同步完成,accountId:{},code:{},返回信息:{}", accountId, task.getSignature(), resultJson);
|
|
|
+ } else {
|
|
|
+ String message = resultJson.getString("message");
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 4, message, new Date());
|
|
|
+ log.error("快手同步素材失败,返回信息:{},请求参数:{}", resultJson, requestJson);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 4, "返回结果为空", new Date());
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 4, "接口请求失败", new Date());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
}
|
|
|
|
|
|
+ private void uploadBytedance(FileBody fileBody, MaterialUploadTask task) {
|
|
|
+ bytedanceExecutorService.submit(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Long accountId = task.getAccountId();
|
|
|
+ if (Check.isNull(accountId)) {
|
|
|
+ log.error("账户不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String token = materialUploadTaskMapper.getTokenByAccountId(accountId);
|
|
|
+ if (Check.isNull(token)) {
|
|
|
+ log.error("此账户id未获取到token,accountId:{}", accountId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String url = HttpConstant.BYTEDANCE_VIDEO_UPLOAD;
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ CloseableHttpClient client = null;
|
|
|
+ // 构造请求
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeader("Access-Token", token);
|
|
|
+ // 文件参数
|
|
|
+ try {
|
|
|
+ MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create()
|
|
|
+ .addPart("video_file", fileBody);
|
|
|
+ // 其他参数
|
|
|
+ entityBuilder.addTextBody("advertiser_id", String.valueOf(task.getAccountId()));
|
|
|
+ entityBuilder.addTextBody("video_signature", task.getSignature());
|
|
|
+ entityBuilder.addTextBody("filename", task.getMaterialName(), ContentType.create("text/plain", Charset.forName("UTF-8")));
|
|
|
+ org.apache.http.HttpEntity entity = entityBuilder.build();
|
|
|
+ client = HttpClientBuilder.create().build();
|
|
|
+ httpPost.setURI(URI.create(url));
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ response = client.execute(httpPost);
|
|
|
+ if (response != null && response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result.toString());
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
+ if (resultJson.getInteger("code") == 0) {
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 3, "同步成功", new Date());
|
|
|
+ materialUploadTaskMapper.syncBytedance(task.getSignature());
|
|
|
+ log.info("头条素材同步完成,accountId:{},code:{},返回信息:{}", accountId, task.getSignature(), resultJson);
|
|
|
+ } else {
|
|
|
+ String message = resultJson.getString("message");
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 4, message, new Date());
|
|
|
+ log.error("头条同步步素材失败,返回信息:{}", resultJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 4, "返回结果为空", new Date());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ materialUploadTaskMapper.updateStatusById(task.getId(), 4, "接口请求失败", new Date());
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ if (null != client) {
|
|
|
+ client.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
private TaskVo getTaskList(String signature) {
|
|
|
TaskVo taskVo = new TaskVo();
|
|
@@ -135,5 +265,32 @@ public class MaterialUploadTaskServiceImpl extends ServiceImpl<MaterialUploadTas
|
|
|
return taskVo;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ private String exceptInfoForRestTemplate(String url, Map<String, Object> paramMap, Map<String, String> headerMap) {
|
|
|
+ try {
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
|
|
+ if (!Check.isNullMap(paramMap)) {
|
|
|
+ for (String key : paramMap.keySet()) {
|
|
|
+ param.add(key, paramMap.get(key));
|
|
|
+ }
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ if (!Check.isNullMap(headerMap)) {
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
+ headers.add(key, headerMap.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
+ requestFactory.setConnectTimeout(1000 * 500);// 设置超时
|
|
|
+ requestFactory.setReadTimeout(1000 * 500);
|
|
|
+ RestTemplate rest = new RestTemplate(requestFactory);
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
|
|
|
+ ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
|
|
|
+ log.info("上传素材时间:{} s ,账户:{}。code:{}", (System.currentTimeMillis() - l) / 1000, paramMap.get("advertiser_id"), paramMap.get("signature"));
|
|
|
+ return responseEntity.getBody();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|