新增阿里云vod功能
Showing
9 changed files
with
204 additions
and
5 deletions
| ... | @@ -66,6 +66,19 @@ public class UpgradeController extends BaseController { | ... | @@ -66,6 +66,19 @@ public class UpgradeController extends BaseController { |
| 66 | } | 66 | } |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | @GetMapping("/v2/list") | ||
| 70 | public R<String> list_v2() { | ||
| 71 | try { | ||
| 72 | ResourceVo resourceVo = service.list(); | ||
| 73 | ObjectMapper objectMapper = new ObjectMapper(); | ||
| 74 | String json = objectMapper.writeValueAsString(resourceVo); | ||
| 75 | return R.ok(MessageUtils.message("system.success"),EncryptUtils.encryptByAes(json)); | ||
| 76 | } | ||
| 77 | catch(Exception e){ | ||
| 78 | return R.fail(); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 69 | @GetMapping("/list1") | 82 | @GetMapping("/list1") |
| 70 | public R<ResourceVo> list1() { | 83 | public R<ResourceVo> list1() { |
| 71 | try { | 84 | try { | ... | ... |
| ... | @@ -2,7 +2,11 @@ package com.lego.web.controller.system; | ... | @@ -2,7 +2,11 @@ package com.lego.web.controller.system; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | import cn.dev33.satoken.annotation.SaCheckPermission; | 4 | import cn.dev33.satoken.annotation.SaCheckPermission; |
| 5 | import cn.hutool.core.bean.BeanUtil; | ||
| 5 | import cn.hutool.core.util.ObjectUtil; | 6 | import cn.hutool.core.util.ObjectUtil; |
| 7 | import com.aliyun.vod.upload.impl.UploadVideoImpl; | ||
| 8 | import com.aliyun.vod.upload.req.UploadStreamRequest; | ||
| 9 | import com.aliyun.vod.upload.resp.UploadStreamResponse; | ||
| 6 | import com.lego.common.annotation.Log; | 10 | import com.lego.common.annotation.Log; |
| 7 | import com.lego.common.core.controller.BaseController; | 11 | import com.lego.common.core.controller.BaseController; |
| 8 | import com.lego.common.core.domain.PageQuery; | 12 | import com.lego.common.core.domain.PageQuery; |
| ... | @@ -10,10 +14,16 @@ import com.lego.common.core.domain.R; | ... | @@ -10,10 +14,16 @@ import com.lego.common.core.domain.R; |
| 10 | import com.lego.common.core.page.TableDataInfo; | 14 | import com.lego.common.core.page.TableDataInfo; |
| 11 | import com.lego.common.core.validate.QueryGroup; | 15 | import com.lego.common.core.validate.QueryGroup; |
| 12 | import com.lego.common.enums.BusinessType; | 16 | import com.lego.common.enums.BusinessType; |
| 17 | import com.lego.common.utils.EncryptUtils; | ||
| 18 | import com.lego.common.utils.StringUtils; | ||
| 19 | import com.lego.oss.config.AppConfig; | ||
| 20 | import com.lego.system.domain.SysOss; | ||
| 13 | import com.lego.system.domain.bo.SysOssBo; | 21 | import com.lego.system.domain.bo.SysOssBo; |
| 14 | import com.lego.system.domain.vo.SysOssVo; | 22 | import com.lego.system.domain.vo.SysOssVo; |
| 23 | import com.lego.system.mapper.SysOssMapper; | ||
| 15 | import com.lego.system.service.ISysOssService; | 24 | import com.lego.system.service.ISysOssService; |
| 16 | import lombok.RequiredArgsConstructor; | 25 | import lombok.RequiredArgsConstructor; |
| 26 | import org.springframework.beans.factory.annotation.Autowired; | ||
| 17 | import org.springframework.http.MediaType; | 27 | import org.springframework.http.MediaType; |
| 18 | import org.springframework.validation.annotation.Validated; | 28 | import org.springframework.validation.annotation.Validated; |
| 19 | import org.springframework.web.bind.annotation.*; | 29 | import org.springframework.web.bind.annotation.*; |
| ... | @@ -21,7 +31,11 @@ import org.springframework.web.multipart.MultipartFile; | ... | @@ -21,7 +31,11 @@ import org.springframework.web.multipart.MultipartFile; |
| 21 | 31 | ||
| 22 | import javax.servlet.http.HttpServletResponse; | 32 | import javax.servlet.http.HttpServletResponse; |
| 23 | import javax.validation.constraints.NotEmpty; | 33 | import javax.validation.constraints.NotEmpty; |
| 34 | import java.io.ByteArrayInputStream; | ||
| 35 | import java.io.ByteArrayOutputStream; | ||
| 24 | import java.io.IOException; | 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; | ||
| 38 | import java.net.URL; | ||
| 25 | import java.util.Arrays; | 39 | import java.util.Arrays; |
| 26 | import java.util.HashMap; | 40 | import java.util.HashMap; |
| 27 | import java.util.List; | 41 | import java.util.List; |
| ... | @@ -39,6 +53,9 @@ import java.util.Map; | ... | @@ -39,6 +53,9 @@ import java.util.Map; |
| 39 | public class SysOssController extends BaseController { | 53 | public class SysOssController extends BaseController { |
| 40 | 54 | ||
| 41 | private final ISysOssService iSysOssService; | 55 | private final ISysOssService iSysOssService; |
| 56 | private final SysOssMapper baseMapper; | ||
| 57 | @Autowired | ||
| 58 | public AppConfig config; | ||
| 42 | 59 | ||
| 43 | /** | 60 | /** |
| 44 | * 查询OSS对象存储列表 | 61 | * 查询OSS对象存储列表 |
| ... | @@ -83,6 +100,79 @@ public class SysOssController extends BaseController { | ... | @@ -83,6 +100,79 @@ public class SysOssController extends BaseController { |
| 83 | } | 100 | } |
| 84 | 101 | ||
| 85 | /** | 102 | /** |
| 103 | * 同步Oss至阿里云 | ||
| 104 | * | ||
| 105 | */ | ||
| 106 | @GetMapping("/sync") | ||
| 107 | public R<String> sync() { | ||
| 108 | List<SysOssVo> list = iSysOssService.list(); | ||
| 109 | String accessKeyId = config.getAliyunAccessKeyId(); | ||
| 110 | String accessKeySecret = config.getAliyunAccessKeySecret(); | ||
| 111 | for(SysOssVo vo : list){ | ||
| 112 | if(StringUtils.isBlank(vo.getVideoId())) { | ||
| 113 | if (".mp4".equals(vo.getFileSuffix()) || ".avi".equals(vo.getFileSuffix()) || ".mov".equals(vo.getFileSuffix()) || ".flv".equals(vo.getFileSuffix())) { | ||
| 114 | try { | ||
| 115 | InputStream inputStream = new URL(vo.getUrl()).openStream(); | ||
| 116 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
| 117 | byte[] data = new byte[1024]; | ||
| 118 | int nRead; | ||
| 119 | while ((nRead = inputStream.read(data, 0, data.length)) != -1) { | ||
| 120 | buffer.write(data, 0, nRead); | ||
| 121 | } | ||
| 122 | buffer.flush(); | ||
| 123 | byte[] byteArray = EncryptUtils.decryptByAes(buffer.toByteArray()); | ||
| 124 | UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, vo.getOriginalName(), vo.getFileName(), new ByteArrayInputStream(byteArray)); | ||
| 125 | /* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/ | ||
| 126 | //request.setShowWaterMark(true); | ||
| 127 | /* 自定义消息回调设置,参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */ | ||
| 128 | //request.setUserData(""{\"Extend\":{\"test\":\"www\",\"localId\":\"xxxx\"},\"MessageCallback\":{\"CallbackURL\":\"http://test.test.com\"}}""); | ||
| 129 | /* 视频分类ID(可选) */ | ||
| 130 | //request.setCateId(0); | ||
| 131 | /* 视频标签,多个用逗号分隔(可选) */ | ||
| 132 | //request.setTags("标签1,标签2"); | ||
| 133 | /* 视频描述(可选) */ | ||
| 134 | //request.setDescription("视频描述"); | ||
| 135 | /* 封面图片(可选) */ | ||
| 136 | //request.setCoverURL("http://cover.sample.com/sample.jpg"); | ||
| 137 | /* 模板组ID(可选) */ | ||
| 138 | //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d"); | ||
| 139 | /* 工作流ID(可选) */ | ||
| 140 | //request.setWorkflowId("d4430d07361f0*be1339577859b0177b"); | ||
| 141 | /* 存储区域(可选) */ | ||
| 142 | //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com"); | ||
| 143 | /* 开启默认上传进度回调 */ | ||
| 144 | // request.setPrintProgress(true); | ||
| 145 | /* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */ | ||
| 146 | // request.setProgressListener(new PutObjectProgressListener()); | ||
| 147 | /* 设置应用ID*/ | ||
| 148 | //request.setAppId("app-1000000"); | ||
| 149 | /* 点播服务接入点 */ | ||
| 150 | //request.setApiRegionId("cn-shanghai"); | ||
| 151 | /* ECS部署区域*/ | ||
| 152 | // request.setEcsRegionId("cn-shanghai"); | ||
| 153 | UploadVideoImpl uploader = new UploadVideoImpl(); | ||
| 154 | UploadStreamResponse response = uploader.uploadStream(request); | ||
| 155 | System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID | ||
| 156 | if (response.isSuccess()) { | ||
| 157 | System.out.print("VideoId=" + response.getVideoId() + "\n"); | ||
| 158 | vo.setVideoId(response.getVideoId()); | ||
| 159 | SysOss sysOss = BeanUtil.toBean(vo, SysOss.class); | ||
| 160 | baseMapper.updateById(sysOss); | ||
| 161 | } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 | ||
| 162 | System.out.print("VideoId=" + response.getVideoId() + "\n"); | ||
| 163 | System.out.print("ErrorCode=" + response.getCode() + "\n"); | ||
| 164 | System.out.print("ErrorMessage=" + response.getMessage() + "\n"); | ||
| 165 | } | ||
| 166 | } catch (Exception e) { | ||
| 167 | |||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | return R.ok("同步媒体问题至阿里云成功"); | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 86 | * 下载OSS对象 | 176 | * 下载OSS对象 |
| 87 | * | 177 | * |
| 88 | * @param ossId OSS对象ID | 178 | * @param ossId OSS对象ID | ... | ... |
| ... | @@ -11,6 +11,10 @@ | ... | @@ -11,6 +11,10 @@ |
| 11 | 11 | ||
| 12 | <artifactId>lego-oss</artifactId> | 12 | <artifactId>lego-oss</artifactId> |
| 13 | 13 | ||
| 14 | <properties> | ||
| 15 | <!-- fastjson --> | ||
| 16 | <fastjson.version>1.2.70</fastjson.version> | ||
| 17 | </properties> | ||
| 14 | <description> | 18 | <description> |
| 15 | OSS对象存储模块 | 19 | OSS对象存储模块 |
| 16 | </description> | 20 | </description> |
| ... | @@ -28,6 +32,38 @@ | ... | @@ -28,6 +32,38 @@ |
| 28 | <artifactId>aws-java-sdk-s3</artifactId> | 32 | <artifactId>aws-java-sdk-s3</artifactId> |
| 29 | </dependency> | 33 | </dependency> |
| 30 | 34 | ||
| 35 | <dependency> | ||
| 36 | <groupId>com.aliyun</groupId> | ||
| 37 | <artifactId>aliyun-java-sdk-core</artifactId> | ||
| 38 | <version>4.5.1</version> | ||
| 39 | </dependency> | ||
| 40 | <dependency> | ||
| 41 | <groupId>com.aliyun</groupId> | ||
| 42 | <artifactId>aliyun-java-sdk-vod</artifactId> | ||
| 43 | <version>2.15.11</version> | ||
| 44 | </dependency> | ||
| 45 | <dependency> | ||
| 46 | <groupId>com.aliyun.oss</groupId> | ||
| 47 | <artifactId>aliyun-sdk-oss</artifactId> | ||
| 48 | <version>3.10.2</version> | ||
| 49 | </dependency> | ||
| 50 | <dependency> | ||
| 51 | <groupId>com.aliyun</groupId> | ||
| 52 | <artifactId>aliyun-java-vod-upload</artifactId> | ||
| 53 | <version>1.4.14</version> | ||
| 54 | </dependency> | ||
| 55 | <!-- fastjson --> | ||
| 56 | <dependency> | ||
| 57 | <groupId>com.alibaba</groupId> | ||
| 58 | <artifactId>fastjson</artifactId> | ||
| 59 | <version>${fastjson.version}</version> | ||
| 60 | </dependency> | ||
| 61 | <dependency> | ||
| 62 | <groupId>cn.hutool</groupId> | ||
| 63 | <artifactId>hutool-all</artifactId> | ||
| 64 | <version>5.3.4</version> | ||
| 65 | <scope>compile</scope> | ||
| 66 | </dependency> | ||
| 31 | </dependencies> | 67 | </dependencies> |
| 32 | 68 | ||
| 33 | </project> | 69 | </project> | ... | ... |
| 1 | package com.lego.oss.config; | ||
| 2 | |||
| 3 | import lombok.Data; | ||
| 4 | import org.springframework.beans.factory.annotation.Value; | ||
| 5 | import org.springframework.context.annotation.Configuration; | ||
| 6 | import org.springframework.context.annotation.PropertySource; | ||
| 7 | |||
| 8 | /** | ||
| 9 | * @author chentao | ||
| 10 | * @date 2026-04-16 | ||
| 11 | */ | ||
| 12 | |||
| 13 | @Data | ||
| 14 | @Configuration | ||
| 15 | @PropertySource(value = "classpath:application.properties", encoding = "UTF-8") | ||
| 16 | public class AppConfig { | ||
| 17 | @Value("${aliyun.accessKeyId}") | ||
| 18 | private String aliyunAccessKeyId; | ||
| 19 | |||
| 20 | @Value("${aliyun.accessKeySecret}") | ||
| 21 | private String aliyunAccessKeySecret; | ||
| 22 | |||
| 23 | @Value("${aliyun.regionId}") | ||
| 24 | private String aliyunRegionId; | ||
| 25 | } |
| ... | @@ -3,7 +3,6 @@ package com.lego.system.service.impl; | ... | @@ -3,7 +3,6 @@ package com.lego.system.service.impl; |
| 3 | import cn.hutool.core.bean.BeanUtil; | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | import cn.hutool.core.convert.Convert; | 4 | import cn.hutool.core.convert.Convert; |
| 5 | import cn.hutool.core.io.IoUtil; | 5 | import cn.hutool.core.io.IoUtil; |
| 6 | import cn.hutool.core.util.CharsetUtil; | ||
| 7 | import cn.hutool.core.util.ObjectUtil; | 6 | import cn.hutool.core.util.ObjectUtil; |
| 8 | import cn.hutool.core.util.StrUtil; | 7 | import cn.hutool.core.util.StrUtil; |
| 9 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 8 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| ... | @@ -42,7 +41,6 @@ import javax.servlet.http.HttpServletResponse; | ... | @@ -42,7 +41,6 @@ import javax.servlet.http.HttpServletResponse; |
| 42 | import java.io.File; | 41 | import java.io.File; |
| 43 | import java.io.IOException; | 42 | import java.io.IOException; |
| 44 | import java.io.InputStream; | 43 | import java.io.InputStream; |
| 45 | import java.nio.charset.StandardCharsets; | ||
| 46 | import java.util.*; | 44 | import java.util.*; |
| 47 | import java.util.stream.Collectors; | 45 | import java.util.stream.Collectors; |
| 48 | 46 | ||
| ... | @@ -68,6 +66,12 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -68,6 +66,12 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 68 | return TableDataInfo.build(result); | 66 | return TableDataInfo.build(result); |
| 69 | } | 67 | } |
| 70 | 68 | ||
| 69 | @Override | ||
| 70 | public List<SysOssVo> list() { | ||
| 71 | List<SysOssVo> list = baseMapper.selectVoList(Wrappers.<SysOss>lambdaQuery()); | ||
| 72 | return list; | ||
| 73 | } | ||
| 74 | |||
| 71 | private void repUrl(List<SysOssVo> list) { | 75 | private void repUrl(List<SysOssVo> list) { |
| 72 | String baseUrl = configService.getConfigValue("sys.oss.baseUrl"); | 76 | String baseUrl = configService.getConfigValue("sys.oss.baseUrl"); |
| 73 | for (SysOssVo vo : list) { | 77 | for (SysOssVo vo : list) { |
| ... | @@ -113,7 +117,12 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -113,7 +117,12 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 113 | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); | 117 | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); |
| 114 | if (ObjectUtil.isNotNull(vo)) { | 118 | if (ObjectUtil.isNotNull(vo)) { |
| 115 | try { | 119 | try { |
| 116 | list.add(this.matchingUrl(vo).getUrl()); | 120 | if(StringUtils.isBlank(vo.getVideoId())){ |
| 121 | list.add(this.matchingUrl(vo).getUrl()); | ||
| 122 | } | ||
| 123 | else{ | ||
| 124 | list.add(vo.getVideoId()); | ||
| 125 | } | ||
| 117 | } catch (Exception ignored) { | 126 | } catch (Exception ignored) { |
| 118 | // 如果oss异常无法连接则将数据直接返回 | 127 | // 如果oss异常无法连接则将数据直接返回 |
| 119 | list.add(vo.getUrl()); | 128 | list.add(vo.getUrl()); |
| ... | @@ -124,7 +133,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -124,7 +133,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 124 | List<String> repList = new ArrayList<>(); | 133 | List<String> repList = new ArrayList<>(); |
| 125 | for (String url : list) { | 134 | for (String url : list) { |
| 126 | if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) { | 135 | if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) { |
| 127 | repList.add(url.replace("127.0.0.1:9000", baseUrl)); | 136 | if(url.contains("http")) { |
| 137 | repList.add(url.replace("127.0.0.1:9000", baseUrl)); | ||
| 138 | } | ||
| 128 | } | 139 | } |
| 129 | } | 140 | } |
| 130 | return String.join(StringUtils.SEPARATOR, repList); | 141 | return String.join(StringUtils.SEPARATOR, repList); |
| ... | @@ -210,7 +221,11 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -210,7 +221,11 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 210 | OssClient storage = OssFactory.instance(); | 221 | OssClient storage = OssFactory.instance(); |
| 211 | UploadResult uploadResult; | 222 | UploadResult uploadResult; |
| 212 | try { | 223 | try { |
| 213 | if(".pdf".equals(suffix) || ".mp4".equals(suffix) || ".json".equals(suffix) || ".ldr".equals(suffix)) { | 224 | if(".mov".equals(suffix) || ".mp4".equals(suffix) || ".flv".equals(suffix) || ".avi".equals(suffix)) { |
| 225 | byte[] encryptBytes = EncryptUtils.encryptByAes(file.getBytes()); | ||
| 226 | uploadResult = storage.uploadSuffix(encryptBytes, suffix, file.getContentType()); | ||
| 227 | } | ||
| 228 | else if(".pdf".equals(suffix) || ".json".equals(suffix) || ".ldr".equals(suffix)) { | ||
| 214 | byte[] encryptBytes = EncryptUtils.encryptByAes(file.getBytes()); | 229 | byte[] encryptBytes = EncryptUtils.encryptByAes(file.getBytes()); |
| 215 | uploadResult = storage.uploadSuffix(encryptBytes, suffix, file.getContentType()); | 230 | uploadResult = storage.uploadSuffix(encryptBytes, suffix, file.getContentType()); |
| 216 | } | 231 | } | ... | ... |
-
Please register or sign in to post a comment