liyuntian 10 mesi fa
parent
commit
0085e0c72b

+ 5 - 0
mcp/pom.xml

@@ -110,6 +110,11 @@
             <artifactId>bcprov-jdk16</artifactId>
             <version>1.46</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.3</version>
+        </dependency>
 
     </dependencies>
 

+ 8 - 0
mcp/src/main/java/com/chuanxia/mcp/bean/model/CdnDomainCity.java

@@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 
+import java.util.Date;
+
 @Data
 @TableName("cdn_domain_city")
 public class CdnDomainCity {
@@ -13,6 +15,12 @@ public class CdnDomainCity {
     private Integer id;
     @TableField("domain_id")
     private Integer domainId;
+    @TableField("domain")
+    private String domain;
     @TableField("city_id")
     private Integer cityId;
+    @TableField("cname")
+    private String cname;
+    @TableField("create_time")
+    private Date createTime;
 }

+ 7 - 0
mcp/src/main/java/com/chuanxia/mcp/bean/model/CdnDomainTimeRule.java

@@ -13,6 +13,13 @@ public class CdnDomainTimeRule {
     private Integer id;
     @TableField("domain_id")
     private Integer domainId;
+    @TableField("domain")
+    private String domain;
     @TableField("time_rule_id")
     private Integer timeRuleId;
+    @TableField("start_time")
+    private Integer startTime;
+    @TableField("end_time")
+    private Integer endTime;
+
 }

+ 0 - 13
mcp/src/main/java/com/chuanxia/mcp/config/ResttemplateConfig.java

@@ -1,13 +0,0 @@
-package com.chuanxia.mcp.config;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.stereotype.Component;
-import org.springframework.web.client.RestTemplate;
-
-@Component
-public class ResttemplateConfig {
-    @Bean
-    public RestTemplate getResttemplate() {
-        return new RestTemplate();
-    }
-}

+ 12 - 1
mcp/src/main/java/com/chuanxia/mcp/controller/CdnM3u8Controller.java

@@ -1,13 +1,24 @@
 package com.chuanxia.mcp.controller;
 
+import com.chuanxia.mcp.bean.vo.Result;
+import com.chuanxia.mcp.sched.M3U8Utils;
 import io.swagger.annotations.Api;
 import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 @Api("m3u8")
-@RequestMapping("cdnDomain")
+@RequestMapping("cdnM3U8")
 @RequiredArgsConstructor
 public class CdnM3u8Controller {
+    private final M3U8Utils m3U8Utils;
+
+    @GetMapping("test")
+    public Result test(@RequestParam("url")String url,@RequestParam("videoId")String videoId) {
+        String s = m3U8Utils.downloadM3U8(url, videoId);
+        return Result.success(s);
+    }
 }

+ 2 - 0
mcp/src/main/java/com/chuanxia/mcp/mapper/CdnDomainCityMapper.java

@@ -11,4 +11,6 @@ import org.springframework.stereotype.Repository;
 @Repository
 public interface CdnDomainCityMapper extends BaseMapper<CdnDomainCity> {
     IPage<CdnDomainCityVo> listPage(IPage<CdnDomainCityVo> page, @Param("queryDto") DomainCityQueryDto queryDto);
+
+    void updateByDomainId(@Param("domainId") Integer domainId, @Param("domain") String domain);
 }

+ 2 - 0
mcp/src/main/java/com/chuanxia/mcp/mapper/CdnDomainTimeRuleMapper.java

@@ -11,4 +11,6 @@ import org.springframework.stereotype.Repository;
 @Repository
 public interface CdnDomainTimeRuleMapper extends BaseMapper<CdnDomainTimeRule> {
     IPage<CdnDomainTimeRuleVo> listPage(IPage<CdnDomainTimeRuleVo> page, @Param("queryDto") DomainTimeRuleQueryDto queryDto);
+
+    void updateDomainByDomainId(@Param("domainId")Integer domainId,@Param("domain")String domain);
 }

+ 43 - 0
mcp/src/main/java/com/chuanxia/mcp/sched/M3U8Utils.java

@@ -0,0 +1,43 @@
+package com.chuanxia.mcp.sched;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.util.Objects;
+
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class M3U8Utils {
+
+    private final RestTemplate restTemplate;
+
+    @Value("${m3u8.downLoadPath}")
+    private String downLoadPath;
+    @Value("${m3u8.tsDownLoadPath}")
+    private String tsDownLoadPath;
+
+    /**
+     * 下载m3u8文件,包含key的下载地址(如果有) 和 ts文件名
+     */
+    public String downloadM3U8(String url, String videoId) {
+        log.info("-- 开始下载 m3u8 --");
+        ResponseEntity<byte[]> forEntity = restTemplate.getForEntity(url, byte[].class);
+        //log.info("m3u8 内容 = {}", new String(forEntity.getBody()));
+        String m3u8Path = downLoadPath + videoId + ".m3u8";
+        File file = new File(m3u8Path);
+        try {
+            Files.write(file.toPath(), Objects.requireNonNull(forEntity.getBody(), "未获取到文件"));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return new String(forEntity.getBody());
+    }
+
+}

+ 76 - 0
mcp/src/main/java/com/chuanxia/mcp/sched/RestTemplateConfig.java

@@ -0,0 +1,76 @@
+package com.chuanxia.mcp.sched;
+
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.ssl.SSLContexts;
+import org.apache.http.ssl.TrustStrategy;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.client.ClientHttpRequestFactory;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.web.client.RestTemplate;
+import javax.net.ssl.SSLContext;
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+
+/**
+ *  restTemplate配置类
+ *
+ * @author Json
+ */
+
+@Configuration
+public class RestTemplateConfig {
+
+    @Bean
+    public RestTemplate restTemplate(ClientHttpRequestFactory factory) throws Exception {
+        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
+
+        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
+                .loadTrustMaterial(null, acceptingTrustStrategy)
+                .build();
+
+        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
+
+        CloseableHttpClient httpClient = HttpClients.custom()
+                .setSSLSocketFactory(csf)
+                .build();
+
+        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
+
+        requestFactory.setHttpClient(httpClient);
+        requestFactory.setConnectionRequestTimeout(6000);
+        requestFactory.setConnectTimeout(6000);
+        requestFactory.setReadTimeout(6000);
+        return new RestTemplate(requestFactory);
+    }
+
+    @Bean
+    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
+        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
+        factory.setConnectTimeout(15000);
+        factory.setReadTimeout(5000);
+        return factory;
+    }
+
+    public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()
+            throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException
+    {
+        TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
+        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
+        SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
+
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
+        factory.setHttpClient(httpClient);
+        return factory;
+    }
+}
+

+ 7 - 2
mcp/src/main/java/com/chuanxia/mcp/sched/ScheduledService.java

@@ -19,14 +19,16 @@ import java.util.List;
 @Component
 @RequiredArgsConstructor
 public class ScheduledService {
+
+    private final M3U8Utils m3U8Utils;
     private final RestTemplate restTemplate;
 
     /**
      * 增量同步
      */
-    @Scheduled(cron = "0/5 * * * * *")
+    //@Scheduled(cron = "0/5 * * * * *")
     public void scheduled() {
-
+        log.info("执行定时任务");
     }
 
     public ArrayList<UserSystemVideoVo> getM3u8Vo(Date startTime, Date endTime) {
@@ -44,4 +46,7 @@ public class ScheduledService {
         return null;
     }
 
+    private void downLoad(String url) {
+    }
+
 }

+ 12 - 4
mcp/src/main/java/com/chuanxia/mcp/service/impl/CdnDoaminServiceImpl.java

@@ -14,9 +14,7 @@ import com.chuanxia.mcp.bean.enums.DeviceUaLevel;
 import com.chuanxia.mcp.bean.model.*;
 import com.chuanxia.mcp.bean.vo.Result;
 import com.chuanxia.mcp.exception.ResultException;
-import com.chuanxia.mcp.mapper.CdnDomainMapper;
-import com.chuanxia.mcp.mapper.CdnDomainOperatorMapper;
-import com.chuanxia.mcp.mapper.CdnUaMapper;
+import com.chuanxia.mcp.mapper.*;
 import com.chuanxia.mcp.service.CdnDoaminService;
 import com.chuanxia.mcp.service.CdnDomainCityService;
 import com.chuanxia.mcp.service.UserService;
@@ -46,6 +44,9 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
     private final CdnDomainCityService domainCityService;
     private final CdnUaMapper cdnUaMapper;
     private final CdnDomainOperatorMapper domainOperatorMapper;
+    private final CdnDomainCityMapper domainCityMapper;
+    private final CdnDomainTimeRuleMapper domainTimeRuleMapper;
+    private final CdnCityMapper cityMapper;
 
     @Override
     public Result<IPage<CdnDomain>> listPage(DomainQueryDto queryDto) {
@@ -97,8 +98,11 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
         List<Integer> cityIds = bindDto.getCityIds();
         List<CdnDomainCity> domainCityList = new ArrayList<>(cityIds.size());
         for (Integer cityId : cityIds) {
+            CdnCity cdnCity = cityMapper.selectById(cityId);
             CdnDomainCity domainCity = new CdnDomainCity();
+            domainCity.setCname(cdnCity.getCname());
             domainCity.setCityId(cityId);
+            domainCity.setDomain(domain.getDomain());
             domainCity.setDomainId(domainId);
             domainCityList.add(domainCity);
         }
@@ -122,7 +126,11 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
             }
         }
         this.updateById(domain);
-        domainOperatorMapper.updateDomainByDomainId(domain.getId(),domain.getDomain());
+
+        domainOperatorMapper.updateDomainByDomainId(domain.getId(), domain.getDomain());
+        domainCityMapper.updateByDomainId(domain.getId(), domain.getDomain());
+        domainTimeRuleMapper.updateDomainByDomainId(domain.getId(), domain.getDomain());
+
         return Result.success("操作成功");
     }
 

+ 7 - 0
mcp/src/main/java/com/chuanxia/mcp/service/impl/CdnTimeRuleServiceImpl.java

@@ -7,11 +7,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.chuanxia.mcp.bean.dto.DomainTimeRuleBindDto;
 import com.chuanxia.mcp.bean.dto.DomainTimeRuleQueryDto;
+import com.chuanxia.mcp.bean.model.CdnDomain;
 import com.chuanxia.mcp.bean.model.CdnDomainTimeRule;
 import com.chuanxia.mcp.bean.model.CdnTimeRule;
 import com.chuanxia.mcp.bean.vo.CdnDomainTimeRuleVo;
 import com.chuanxia.mcp.bean.vo.Result;
 import com.chuanxia.mcp.exception.ResultException;
+import com.chuanxia.mcp.mapper.CdnDomainMapper;
 import com.chuanxia.mcp.mapper.CdnDomainTimeRuleMapper;
 import com.chuanxia.mcp.mapper.CdnTimeRuleMapper;
 import com.chuanxia.mcp.service.CdnTimeRuleService;
@@ -26,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
 @Slf4j
 public class CdnTimeRuleServiceImpl extends ServiceImpl<CdnTimeRuleMapper, CdnTimeRule> implements CdnTimeRuleService {
     private final UserService userService;
+    private final CdnDomainMapper domainMapper;
     private final CdnTimeRuleMapper ruleMapper;
     private final CdnDomainTimeRuleMapper domainTimeRuleMapper;
 
@@ -52,8 +55,12 @@ public class CdnTimeRuleServiceImpl extends ServiceImpl<CdnTimeRuleMapper, CdnTi
             ruleMapper.insert(cdnTimeRule);
         }
         CdnDomainTimeRule rule = new CdnDomainTimeRule();
+        CdnDomain domain = domainMapper.selectById(timeRule.getDomainId());
         rule.setDomainId(timeRule.getDomainId());
+        rule.setDomain(domain.getDomain());
         rule.setTimeRuleId(cdnTimeRule.getId());
+        rule.setStartTime(cdnTimeRule.getStartTime());
+        rule.setEndTime(cdnTimeRule.getEndTime());
         domainTimeRuleMapper.insert(rule);
         return Result.success("操作成功");
     }

+ 4 - 1
mcp/src/main/resources/application-local.yml

@@ -6,4 +6,7 @@ spring:
     password: root
 mybatis-plus:
   configuration:
-    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+m3u8:
+  downLoadPath: D:\\m3u8Download
+  tsDownLoadPath: D:\\m3u8TsDownLoad

+ 20 - 16
mcp/src/main/resources/com/chuanxia/mcp/mapper/CdnDomainCityMapper.xml

@@ -3,24 +3,28 @@
         "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.chuanxia.mcp.mapper.CdnDomainCityMapper">
+    <update id="updateByDomainId">
+        update cdn_domain_city set `domain` =#{domain}  where domain_id =#{domainId};
+    </update>
+
     <select id="listPage" resultType="com.chuanxia.mcp.bean.vo.CdnDomainCityVo">
         SELECT
-        cdc.id,
-        cdc.`city_id` as cityId,
-        cdc.`domain_id` as domainId,
-        cd.`domain`,
-        cc.`cname` as cityName
+        id,
+        domain_id AS domainId,
+        `domain`,
+        city_id AS cityId,
+        cname as cityName,
+        create_time
         FROM
-        `cdn_domain_city` cdc
-        LEFT JOIN `cdn_domain` cd
-        ON cdc.`domain_id` = cd.`id`
-        LEFT JOIN `cdn_city` cc
-        ON cdc.`city_id` = cc.`id`
-        <if test="queryDto.cname != null and queryDto.cname != ''">
-            and cc.cname = #{queryDto.cname}
-        </if>
-        <if test="queryDto.domain != null and queryDto.domain != ''">
-          and cd.domain = #{queryDto.domain}
-        </if>
+        cdn_domain_city
+        <where>
+            <if test="queryDto.cname != null and queryDto.cname != ''">
+                and cname = #{queryDto.cname}
+            </if>
+            <if test="queryDto.domain != null and queryDto.domain != ''">
+                and `domain` = #{queryDto.domain}
+            </if>
+        </where>
+        ORDER BY create_time DESC
     </select>
 </mapper>

+ 8 - 6
mcp/src/main/resources/com/chuanxia/mcp/mapper/CdnDomainOperatorMapper.xml

@@ -17,12 +17,14 @@
         `create_time` AS createTime
         FROM
         `cdn_domain_operator` cdo
-        <if test="queryDto.operatorName != null and queryDto.operatorName!='' ">
-            and operator_name = #{queryDto.operatorName}
-        </if>
-        <if test="queryDto.domain != null and queryDto.domain!='' ">
-            and `domain` = #{queryDto.domain}
-        </if>
+        <where>
+            <if test="queryDto.operatorName != null and queryDto.operatorName!='' ">
+                and operator_name = #{queryDto.operatorName}
+            </if>
+            <if test="queryDto.domain != null and queryDto.domain!='' ">
+                and `domain` = #{queryDto.domain}
+            </if>
+        </where>
         order by create_time desc
     </select>
 </mapper>

+ 22 - 20
mcp/src/main/resources/com/chuanxia/mcp/mapper/CdnDomainTimeRuleMapper.xml

@@ -3,28 +3,30 @@
         "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.chuanxia.mcp.mapper.CdnDomainTimeRuleMapper">
+    <update id="updateDomainByDomainId">
+        update cdn_domain_time_rule set  `domain`=#{domain}  where domain_id = #{domainId};
+    </update>
     <select id="listPage" resultType="com.chuanxia.mcp.bean.vo.CdnDomainTimeRuleVo">
         SELECT
-        dtr.`id`,
-        dtr.`domain_id` AS domainId,
-        dtr.`time_rule_id` AS timeRuleId,
-        cd.`domain`,
-        ctr.`start_time` AS startTime,
-        ctr.`end_time` AS endTime
+        id,
+        domain_id AS domainId,
+        `domain`,
+        time_rule_id AS timeRuleId,
+        start_time AS startTime,
+        end_time AS endTime
         FROM
-        `cdn_domain_time_rule` dtr
-        LEFT JOIN `cdn_domain` cd
-        ON dtr.`domain_id` = cd.`id`
-        LEFT JOIN `cdn_time_rule` ctr
-        ON dtr.`time_rule_id` = ctr.`id`
-        <if test="queryDto.domain != null and queryDto.domain != ''">
-            and cd.domain = #{queryDto.domain}
-        </if>
-        <if test="queryDto.startTime != null">
-            and ctr.start_time &gt;= #{queryDto.startTime}
-        </if>
-        <if test="queryDto.endTime != null">
-            and ctr.end_time &lt;= #{queryDto.endTime}
-        </if>
+        `cdn_domain_time_rule`
+        <where>
+            <if test="queryDto.domain != null and queryDto.domain != ''">
+                and `domain` = #{queryDto.domain}
+            </if>
+            <if test="queryDto.startTime != null">
+                and start_time &gt;= #{queryDto.startTime}
+            </if>
+            <if test="queryDto.endTime != null">
+                and end_time &lt;= #{queryDto.endTime}
+            </if>
+        </where>
+
     </select>
 </mapper>