pudongliang 4 месяцев назад
Родитель
Сommit
0b5aab409c

+ 1 - 0
src/main/java/com/moka/gdtauto/service/impl/OssFileServiceImpl.java

@@ -89,6 +89,7 @@ public class OssFileServiceImpl extends ServiceImpl<OssFileMapper, OssFile> impl
             save(ossFile);
             if (businessType == 1) {
                 tencentAssetService.uploadAsset(ossFile);
+                updateById(ossFile);
             }
             
             log.info("文件上传成功,ID: {}, URL: {}", ossFile.getId(), fileUrl);

+ 9 - 210
src/test/java/com/moka/gdtauto/service/OssFileTencentUploadTest.java

@@ -35,6 +35,8 @@ public class OssFileTencentUploadTest {
     private OssFileService ossFileService;
     @Autowired
     private TencentAdsApiClientFactory clientFactory;
+    @Autowired
+    private TencentAssetService tencentAssetService;
 
     private static final Long ORG_ACCOUNT_ID = 44035150L;
     private static final String TEST_JOB_NUMBER = "12458";
@@ -43,222 +45,19 @@ public class OssFileTencentUploadTest {
     public void testUploadFile() throws Exception {
         List<OssFile> ossFileList = ossFileService.lambdaQuery()
         .eq(OssFile::getBusinessType, 1)
+        .in(OssFile::getId, List.of(79))
         .list();
         TencentAds tencentAds = clientFactory.getTencentAds(TEST_JOB_NUMBER, ORG_ACCOUNT_ID);
             tencentAds.setDebug(true);
             ossFileList.forEach(ossFile -> {
-            String fileUrl = ossFile.getFileUrl();;
-            String fileMd5 = ossFile.getFileMd5();
-            //1:创意 2:品牌形象
-            Integer businessType = ossFile.getBusinessType();
-            //image/video
-            String fileType = ossFile.getFileType();
-
-            if (fileType.equals("image")) {
-                try  {
-                    byte[] fileBytes = downloadFileToBytes(fileUrl);
-                    uploadImageFromBytes(tencentAds, ORG_ACCOUNT_ID, fileBytes, fileMd5, fileType, ossFile);
-                } catch (Exception e) {
-                    log.error("上传文件失败,URL: {}", fileUrl, e);
-                }
-            } else if(fileType.equals("video")) {
-                try  {
-                    uploadVideoFromUrl(tencentAds, ORG_ACCOUNT_ID, fileUrl, fileMd5, fileType, ossFile);
-                } catch (Exception e) {
-                    log.error("上传文件失败,URL: {}", fileUrl, e);
-                }
+            try {
+                tencentAssetService.uploadAsset(ossFile);
+                ossFileService.updateById(ossFile);
+            } catch(Exception e) {
+                e.printStackTrace();
             }
         });
-        ossFileService.updateBatchById(ossFileList);
-    }
-
-    private byte[] downloadFileToBytes(String fileUrl) throws Exception {
-        HttpURLConnection connection = null;
-        InputStream inputStream = null;
-        try {
-            URL url = new URL(fileUrl);
-            connection = (HttpURLConnection) url.openConnection();
-            connection.setConnectTimeout(10000);
-            connection.setReadTimeout(30000);
-            connection.setRequestMethod("GET");
-            connection.connect();
-
-            int responseCode = connection.getResponseCode();
-            if (responseCode != HttpURLConnection.HTTP_OK) {
-                throw new Exception("下载文件失败,HTTP状态码: " + responseCode);
-            }
-
-            inputStream = connection.getInputStream();
-            byte[] fileBytes = inputStream.readAllBytes();
-            log.info("文件下载成功,URL: {}, 大小: {} bytes", fileUrl, fileBytes.length);
-            return fileBytes;
-        } finally {
-            if (inputStream != null) {
-                try {
-                    inputStream.close();
-                } catch (IOException e) {
-                    log.warn("关闭输入流失败", e);
-                }
-            }
-            if (connection != null) {
-                connection.disconnect();
-            }
-        }
+        
     }
-
-    /**
-     * 使用字节数组上传图片到腾讯广告
-     */
-    private Long uploadImageFromBytes(TencentAds tencentAds, Long accountId, byte[] imageBytes, String fileMd5, String description, OssFile ossFile)
-            throws Exception {
-        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
-
-        ImagesAddResponseData response = tencentAds.images()
-                .imagesAdd(
-                        "UPLOAD_TYPE_BYTES",
-                        fileMd5,
-                        null,
-                        ORG_ACCOUNT_ID,
-                        null,
-                        base64Image,
-                        null,
-                        description,
-                        null,
-                        null,
-                        null
-                );
-
-        if (response == null || response.getImageId() == null) {
-            throw new Exception("图片上传返回结果为空");
-        }
-
-        log.info("图片上传成功,imageId: {}, 宽度: {}px, 高度: {}px",
-                response.getImageId(), response.getImageWidth(), response.getImageHeight());
-        ossFile.setAssetId(response.getImageId());
-        ossFile.setWidth(response.getImageWidth());
-        ossFile.setHeight(response.getImageHeight());
-        ossFile.setImageFileSize(response.getImageFileSize());
-        ossFile.setImageType(response.getImageType().getValue());
-        ossFile.setImageSignature(response.getImageSignature());
-        ossFile.setOuterImageId(response.getOuterImageId());
-        ossFile.setPreviewUrl(response.getPreviewUrl());
-        ossFile.setDescription(response.getDescription());
-        return Long.parseLong(response.getImageId());
-    }
-
-      private record VideoUploadResult(Long videoId, Long coverImageId) {}
-
-
-    private VideoUploadResult uploadVideoFromUrl(TencentAds tencentAds, Long accountId, String videoUrl, String fileMd5, String description, OssFile ossFile)
-            throws Exception {
-        File tempFile = null;
-        try {
-            // 1. 从URL下载视频到临时文件
-            tempFile = downloadFileFromUrl(videoUrl);
-
-            // 2. 检查文件大小(最大100MB)
-            long fileSizeInMB = tempFile.length() / (1024 * 1024);
-            if (fileSizeInMB > 100) {
-                throw new Exception("视频文件过大: " + fileSizeInMB + "MB,超过 100MB 限制");
-            }
-
-            // 3. 计算视频MD5签名
-            String signature = fileMd5;
-            log.info("视频MD5签名: {}, 文件大小: {}MB", signature, fileSizeInMB);
-
-            // 4. 调用腾讯广告API上传视频
-            VideosAddResponseData response = tencentAds.videos()
-                    .videosAdd(
-                            tempFile,           // videoFile
-                            signature,          // signature
-                            null,          // accountId
-                            ORG_ACCOUNT_ID,               // organizationId
-                            description,        // description
-                            null                // adcreativeTemplateId
-                    );
-
-            if (response == null || response.getVideoId() == null) {
-                throw new Exception("视频上传返回结果为空");
-            }
-
-            log.info("视频上传成功,videoId: {}, 封面图片ID: {}",
-                    response.getVideoId(), response.getCoverImageId());
-            ossFile.setAssetId(String.valueOf(response.getVideoId()));
-            ossFile.setCoverImageId(response.getCoverImageId());
-            return new VideoUploadResult(response.getVideoId(), response.getCoverImageId());
-        } finally {
-            // 5. 删除临时文件
-            if (tempFile != null && tempFile.exists()) {
-                tempFile.delete();
-            }
-        }
-    }
-
-    private File downloadFileFromUrl(String fileUrl) throws Exception {
-        HttpURLConnection connection = null;
-        InputStream inputStream = null;
-        FileOutputStream outputStream = null;
-
-        try {
-            // 创建临时文件
-            String suffix = getFileSuffix(fileUrl);
-            File tempFile = File.createTempFile("gdt_upload_", suffix);
-
-            // 打开HTTP连接
-            URL url = new URL(fileUrl);
-            connection = (HttpURLConnection) url.openConnection();
-            connection.setConnectTimeout(10000);
-            connection.setReadTimeout(30000);
-            connection.setRequestMethod("GET");
-            connection.connect();
-
-            int responseCode = connection.getResponseCode();
-            if (responseCode != HttpURLConnection.HTTP_OK) {
-                throw new Exception("下载文件失败,HTTP状态码: " + responseCode);
-            }
-
-            // 读取文件内容
-            inputStream = connection.getInputStream();
-            outputStream = new FileOutputStream(tempFile);
-
-            byte[] buffer = new byte[8192];
-            int bytesRead;
-            while ((bytesRead = inputStream.read(buffer)) != -1) {
-                outputStream.write(buffer, 0, bytesRead);
-            }
-
-            log.info("文件下载成功,URL: {}, 大小: {} bytes", fileUrl, tempFile.length());
-            return tempFile;
-
-        } finally {
-            if (outputStream != null) {
-                try {
-                    outputStream.close();
-                } catch (Exception e) {
-                }
-            }
-            if (inputStream != null) {
-                try {
-                    inputStream.close();
-                } catch (Exception e) {
-                }
-            }
-            if (connection != null) {
-                connection.disconnect();
-            }
-        }
-    }
-
-    private String getFileSuffix(String fileUrl) {
-        int lastDotIndex = fileUrl.lastIndexOf('.');
-        int lastSlashIndex = fileUrl.lastIndexOf('/');
-
-        if (lastDotIndex > lastSlashIndex && lastDotIndex < fileUrl.length() - 1) {
-            return fileUrl.substring(lastDotIndex);
-        }
-
-        return ".mp4"; // 默认后缀
-    }
-
     
 }