|
|
@@ -1,15 +1,16 @@
|
|
|
package cn.reghao.dfs.store.rpc;
|
|
|
|
|
|
+import cn.reghao.dfs.api.dto.EncryptParam;
|
|
|
+import cn.reghao.dfs.api.dto.FileAccess;
|
|
|
import cn.reghao.dfs.api.iface.MediaUrlService;
|
|
|
+import cn.reghao.dfs.store.config.DfsProperties;
|
|
|
import cn.reghao.dfs.store.db.mapper.FileUserMapper;
|
|
|
import cn.reghao.dfs.store.db.mapper.VideoUrlMapper;
|
|
|
import cn.reghao.dfs.api.dto.VideoUrlDto;
|
|
|
-import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
+import cn.reghao.jutil.jdk.security.crypto.AesEncrypt;
|
|
|
import org.apache.dubbo.config.annotation.DubboService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -21,10 +22,13 @@ import java.util.stream.Collectors;
|
|
|
@DubboService
|
|
|
@Service
|
|
|
public class MediaUrlServiceImpl implements MediaUrlService {
|
|
|
+ private final String encryptKey;
|
|
|
private final VideoUrlMapper videoUrlMapper;
|
|
|
private final FileUserMapper fileUserMapper;
|
|
|
|
|
|
- public MediaUrlServiceImpl(VideoUrlMapper videoUrlMapper, FileUserMapper fileUserMapper) {
|
|
|
+ public MediaUrlServiceImpl(DfsProperties dfsProperties, VideoUrlMapper videoUrlMapper,
|
|
|
+ FileUserMapper fileUserMapper) {
|
|
|
+ this.encryptKey = dfsProperties.getEncryptKey();
|
|
|
this.videoUrlMapper = videoUrlMapper;
|
|
|
this.fileUserMapper = fileUserMapper;
|
|
|
}
|
|
|
@@ -36,16 +40,17 @@ public class MediaUrlServiceImpl implements MediaUrlService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<VideoUrlDto> getEncryptMp4Urls(String videoFileId, String key) throws NoSuchAlgorithmException {
|
|
|
+ public List<VideoUrlDto> getEncryptMp4Urls(EncryptParam encryptParam) {
|
|
|
+ String videoFileId = encryptParam.getVideoFileId();
|
|
|
List<VideoUrlDto> urls = videoUrlMapper.findByVideoFileId(videoFileId);
|
|
|
return urls.stream()
|
|
|
.map(videoUrlDto -> {
|
|
|
String url = videoUrlDto.getUrl();
|
|
|
try {
|
|
|
- String encryptUrl = encryptUrl(videoFileId, url);
|
|
|
+ String encryptUrl = encryptUrl(encryptParam.getAccessUserId(), url);
|
|
|
videoUrlDto.setUrl(encryptUrl);
|
|
|
return videoUrlDto;
|
|
|
- } catch (NoSuchAlgorithmException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
@@ -54,18 +59,17 @@ public class MediaUrlServiceImpl implements MediaUrlService {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
- private String encryptUrl(String videoFileId, String url) throws NoSuchAlgorithmException {
|
|
|
+ private String encryptUrl(long accessUserId, String url) throws Exception {
|
|
|
String key = "abcdABCD1234";
|
|
|
// 链接过期时间戳,8 小时后过期
|
|
|
long t = System.currentTimeMillis() + 8*3600*1000;
|
|
|
- byte[] bytes = (key + t).getBytes(StandardCharsets.UTF_8);
|
|
|
- String sign = DigestUtil.md5sum(bytes);
|
|
|
- String encryptUrl = String.format("%s?t=%s&sign=%s", url, t, sign);
|
|
|
- return encryptUrl;
|
|
|
+ String sign = String.format("%s_%s_%s", key, accessUserId, t);
|
|
|
+ String encryptSign = AesEncrypt.encrypt(sign, encryptKey);
|
|
|
+ return String.format("%s?sign=%s", url, encryptSign);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void activateFile(String uploadId) {
|
|
|
- fileUserMapper.updateSetActivate(uploadId);
|
|
|
+ public void activateFileAccess(FileAccess fileAccess) {
|
|
|
+ fileUserMapper.updateSetFileAccess(fileAccess);
|
|
|
}
|
|
|
}
|