xiongzhu 4 years ago
parent
commit
064130342c

+ 10 - 0
src/main/java/com/izouma/nineth/repo/AssetRepo.java

@@ -62,4 +62,14 @@ public interface AssetRepo extends JpaRepository<Asset, Long>, JpaSpecificationE
             "order by modified_at")
     List<Asset> findColdAsset(String name, int number);
 
+    @Query(value = "select c.id, c.pic, c.model3d, c.minter_avatar, c.owner_avatar, c.detail from asset c", nativeQuery = true)
+    List<List<String>> selectResource();
+
+    @Modifying
+    @Transactional
+    @Query(value = "update asset c set c.pic = ?2, c.model3d = ?3, c.minter_avatar = ?4, " +
+            "c.owner_avatar = ?5, c.detail = ?6 where c.id = ?1", nativeQuery = true)
+    int updateCDN(Long id, String pic, String model3d, String minterAvatar,
+                  String ownerAvatar, String detail);
+
 }

+ 13 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -130,4 +130,17 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @Transactional
     @Modifying
     int deleteByAssetId(Long assetId);
+
+    @Query(value = "select c.id, c.pic, c.model3d, c.minter_avatar, c.owner_avatar, c.detail from collection_info c", nativeQuery = true)
+    List<List<String>> selectResource();
+
+    @Query(value = "select c.id, c.pic, c.model3d, c.minter_avatar, c.owner_avatar, c.detail from collection_info c where c.id = ?1", nativeQuery = true)
+    List<List<String>> selectResource(Long id);
+
+    @Modifying
+    @Transactional
+    @Query(value = "update collection_info c set c.pic = ?2, c.model3d = ?3, c.minter_avatar = ?4, " +
+            "c.owner_avatar = ?5, c.detail = ?6 where c.id = ?1", nativeQuery = true)
+    int updateCDN(Long id, String pic, String model3d, String minterAvatar,
+                  String ownerAvatar, String detail);
 }

+ 37 - 0
src/main/java/com/izouma/nineth/service/AssetService.java

@@ -32,6 +32,8 @@ import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
 import java.util.stream.Collectors;
 
 @Service
@@ -454,4 +456,39 @@ public class AssetService {
         });
         return result;
     }
+
+    public void transferCDN() throws ExecutionException, InterruptedException {
+        ForkJoinPool customThreadPool = new ForkJoinPool(100);
+        customThreadPool.submit(() -> {
+            collectionRepo.selectResource().parallelStream().forEach(list -> {
+                for (int i = 0; i < list.size(); i++) {
+                    list.set(i, replaceCDN(list.get(i)));
+                }
+                collectionRepo.updateCDN(Long.parseLong(list.get(0)),
+                        list.get(1),
+                        list.get(2),
+                        list.get(3),
+                        list.get(4),
+                        list.get(5));
+            });
+
+            assetRepo.selectResource().parallelStream().forEach(list -> {
+                for (int i = 0; i < list.size(); i++) {
+                    list.set(i, replaceCDN(list.get(i)));
+                }
+                assetRepo.updateCDN(Long.parseLong(list.get(0)),
+                        list.get(1),
+                        list.get(2),
+                        list.get(3),
+                        list.get(4),
+                        list.get(5));
+            });
+        }).get();
+    }
+
+    public String replaceCDN(String url) {
+        if (url == null) return null;
+        return url.replaceAll("https://raex-meta\\.oss-cn-shenzhen\\.aliyuncs\\.com",
+                "https://cdn.raex.vip");
+    }
 }

+ 15 - 0
src/main/java/com/izouma/nineth/web/AssetController.java

@@ -21,6 +21,7 @@ import lombok.AllArgsConstructor;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
@@ -29,6 +30,7 @@ import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutionException;
 
 @RestController
 @RequestMapping("/asset")
@@ -143,6 +145,19 @@ public class AssetController extends BaseController {
         }
         return "ok";
     }
+
+    @PostMapping("/cdn")
+    @PreAuthorize("hasRole('ADMIN')")
+    public String cdn() throws ExecutionException, InterruptedException {
+        new Thread(() -> {
+            try {
+                assetService.transferCDN();
+            } catch (ExecutionException | InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+        return "ok";
+    }
 }