Browse Source

批量工具上传视频接口

yumeng 5 years ago
parent
commit
abc72060ea

+ 15 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/JeecgApplication.java

@@ -5,15 +5,18 @@ import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.servlet.MultipartConfigFactory;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 import org.springframework.core.env.Environment;
+import org.springframework.util.unit.DataSize;
 import org.springframework.web.client.RestTemplate;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
+import javax.servlet.MultipartConfigElement;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
@@ -49,4 +52,16 @@ public class JeecgApplication {
         return new PaginationInterceptor();
     }
 
+    @Bean
+    public MultipartConfigElement multipartConfigElement() {
+        MultipartConfigFactory factory = new MultipartConfigFactory();
+        //单个文件大小200MB,单位KB,MB
+        factory.setMaxFileSize(DataSize.parse("200MB"));
+        //设置总上传数据大小200MB,单位KB,MB
+        factory.setMaxRequestSize(DataSize.parse("200MB"));
+
+        return factory.createMultipartConfig();
+    }
+
+
 }

+ 2 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/ShiroConfig.java

@@ -103,6 +103,8 @@ public class ShiroConfig {
 		filterChainDefinitionMap.put("/ctop/bindAccount/**", "anon");
 		filterChainDefinitionMap.put("/**/*.mp4", "anon");
 
+		filterChainDefinitionMap.put("/kusiShouUpload/**", "anon");
+
 
 		//性能监控
 		filterChainDefinitionMap.put("/actuator/metrics/**", "anon");

+ 1 - 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/service/impl/MaterialTopServiceImpl.java

@@ -20,7 +20,7 @@ import org.springframework.stereotype.Service;
 public class MaterialTopServiceImpl  implements IMaterialTopService {
 
     @Autowired
-    MaterialTopMapper materialTopMapper;
+    private MaterialTopMapper materialTopMapper;
 
     @Override
     public IPage<MaterialTopVO> selectByPage(String endDate, String startDate, String target, String order, int pageNo, int pageSize) {

+ 26 - 15
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -9,6 +9,7 @@ import cn.com.ctop.crawler.modules.pangolin.entity.PangolinApp;
 import cn.com.ctop.crawler.modules.pangolin.service.PangolinAppService;
 import cn.com.ctop.crawler.modules.pangolin.service.PangolinCrawlerService;
 import cn.com.ctop.crawler.modules.pangolin.service.PangolinLoginService;
+import cn.com.ctop.kuaishou.modules.batch.service.impl.KuaishouInterfaceServiceImpl;
 import cn.com.ctop.toutiao.modules.report.service.IBytedanceFundDailyService;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.extern.slf4j.Slf4j;
@@ -36,15 +37,25 @@ public class SampleTest {
     private PangolinCrawlerService pangolinCrawlerService;
     @Autowired
     private PangolinAppService pangolinAppService;
+    @Autowired
+    private KuaishouInterfaceServiceImpl interfaceService;
+
+
+    @Test
+    public void suzhao() {
+        interfaceService.getSuZaoList("33e4f5b5fa460386e8d2f4d4113c1f27",161468L,1);
+    }
+
+
     @Test
-    public void testPangolinLogin(){
+    public void testPangolinLogin() {
         QueryWrapper<BindAccountLogin> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("login_type","pangolin");
-        queryWrapper.eq("status",1);
+        queryWrapper.eq("login_type", "pangolin");
+        queryWrapper.eq("status", 1);
         List<BindAccountLogin> list = bindAccountLoginService.list(queryWrapper);
-        if(list!=null&&!list.isEmpty()){
-            for (BindAccountLogin login:list) {
-                if(null==login.getCookie()||"".equals(login.getCookie().trim())){
+        if (list != null && !list.isEmpty()) {
+            for (BindAccountLogin login : list) {
+                if (null == login.getCookie() || "".equals(login.getCookie().trim())) {
                     pangolinLoginService.pangolinLogin(login.getAccountName(), login.getPassword());
                 }
             }
@@ -52,7 +63,7 @@ public class SampleTest {
     }
 
     @Test
-    public void testPangolinData(){
+    public void testPangolinData() {
         QueryWrapper<BindAccountLogin> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("login_type", "pangolin");
         queryWrapper.eq("status", 1);
@@ -81,23 +92,23 @@ public class SampleTest {
 
 
     @Test
-    public void testflowFund(){
+    public void testflowFund() {
         List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
         if (null == tokens || tokens.size() <= 0) {
             log.error("头条账户流水数据异常:未获取到可用的token");
-            return ;
+            return;
         }
-        CountDownLatch countDownLatch   = new CountDownLatch(tokens.size());
+        CountDownLatch countDownLatch = new CountDownLatch(tokens.size());
         executorService = Executors.newFixedThreadPool(5);
         tokens.forEach(token -> {
             executorService.submit(() -> {
                 try {
-                    for(int i=0;i<60;i++){
-                        String findDate = DateUtils.addDay(DateUtils.formatDate(),-i);
-                        bytedanceFundDailyService.loadFundDataByPage(token, findDate, findDate,1);
+                    for (int i = 0; i < 60; i++) {
+                        String findDate = DateUtils.addDay(DateUtils.formatDate(), -i);
+                        bytedanceFundDailyService.loadFundDataByPage(token, findDate, findDate, 1);
                     }
-                }catch (Exception e){
-                }finally {
+                } catch (Exception e) {
+                } finally {
                     countDownLatch.countDown();
                 }
             });

+ 52 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/controller/KuaiShouMaterialUploadController.java

@@ -0,0 +1,52 @@
+package cn.com.ctop.kuaishou.modules.batch.controller;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/kusiShouUpload")
+public class KuaiShouMaterialUploadController {
+
+    @Autowired
+    private IKuaiShouMaterialUploadService uploadService;
+    @Autowired
+    private ICtopOauthTokenService oauthTokenService;
+
+    @PostMapping(value = "/video", consumes = "multipart/form-data;charset=utf-8")
+    public Map<String, Object> video(@RequestParam("multipartFile") MultipartFile multipartFile, String type, Long accountId, String code, HttpServletRequest request) throws Exception {
+
+        CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
+        if (Check.isNull(oauthToken)) {
+            throw new Exception("未获取到账户信息");
+        }
+
+        uploadService.video(multipartFile, type, accountId, oauthToken.getAccessToken(), code);
+        return null;
+    }
+
+
+    @PostMapping(value = "/image", consumes = "multipart/form-data;charset=utf-8")
+    public Map<String, Object> image(@RequestParam("multipartFile") MultipartFile multipartFile, String type, Long accountId, String code, HttpServletRequest request) throws Exception {
+
+        CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
+        if (Check.isNull(oauthToken)) {
+            throw new Exception("未获取到账户信息");
+        }
+
+        uploadService.video(multipartFile, type, accountId, oauthToken.getAccessToken(), code);
+        return null;
+    }
+
+
+}

+ 9 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IKuaiShouMaterialUploadService.java

@@ -0,0 +1,9 @@
+package cn.com.ctop.kuaishou.modules.batch.service;
+
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+public interface IKuaiShouMaterialUploadService {
+    void video(MultipartFile multipartFile, String type, Long accountId, String accessToken, String code) throws IOException;
+}

+ 101 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaiShouMaterialUploadServiceImpl.java

@@ -0,0 +1,101 @@
+package cn.com.ctop.kuaishou.modules.batch.service.impl;
+
+import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
+import cn.com.ctop.common.module.utils.PropertiesUtils;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+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.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+@Slf4j
+@Service
+public class KuaiShouMaterialUploadServiceImpl implements IKuaiShouMaterialUploadService {
+    /**
+     * 上传文件
+     *
+     * @param multipartFile
+     * @param type
+     * @param accountId
+     */
+    @Override
+    public void video(MultipartFile multipartFile, String type, Long accountId, String accessToken, String code) throws IOException {
+        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.VIDEO_UPLOAD;
+        String s = uploadFile(multipartFile, "D:\\tets\\");
+        System.err.println(s);
+
+        File file = new File(s);
+
+        System.err.println(file);
+        FileSystemResource resource = new FileSystemResource(file);
+        Map<String, String> headerMap = new HashMap<>();
+        headerMap.put("Content-Type", "multipart/form-data");
+        headerMap.put("Access-Token", accessToken);
+        JSONObject requestJson = new JSONObject();
+        requestJson.put("signature", code);
+        requestJson.put("file", resource);
+        requestJson.put("type", type);
+        requestJson.put("advertiser_id", accountId);
+        String result = exceptInfoForRestTemplate(url, requestJson, headerMap);
+        System.err.println(result);
+
+        //
+    }
+
+
+    public String uploadFile(MultipartFile multipartFile, String dirPath) throws IOException {
+        String fileName = multipartFile.getOriginalFilename();
+        String fileSuffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
+        String localFileName = System.currentTimeMillis() + fileSuffix;
+        String filePath = dirPath + File.separator + localFileName;
+        File localFile = new File(filePath);
+        File imagePath = new File(dirPath);
+        if (!imagePath.exists()) {
+            imagePath.mkdirs();
+        }
+        multipartFile.transferTo(localFile);
+        return filePath;
+    }
+
+    @Autowired
+    private RestTemplate rest;
+
+    private String exceptInfoForRestTemplate(String url, Map<String, Object> paramMap, Map<String, String> headerMap) {
+        try {
+            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));
+                    }
+                }
+                HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
+                ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
+                return responseEntity.getBody();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+}