|
|
@ -1,10 +1,14 @@ |
|
|
|
package org.dromara.sample.wayline.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.crypto.digest.MD5; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import io.seata.common.util.CollectionUtils; |
|
|
|
import org.apache.dubbo.config.annotation.DubboReference; |
|
|
|
import org.dom4j.Document; |
|
|
|
import org.dom4j.DocumentException; |
|
|
|
import org.dom4j.Node; |
|
|
@ -21,9 +25,13 @@ import org.dromara.common.sdk.cloudapi.wayline.GetWaylineListResponse; |
|
|
|
import org.dromara.common.sdk.cloudapi.wayline.WaylineTypeEnum; |
|
|
|
import org.dromara.common.sdk.common.Pagination; |
|
|
|
import org.dromara.common.sdk.common.PaginationData; |
|
|
|
import org.dromara.resource.api.RemoteFileService; |
|
|
|
import org.dromara.resource.api.domain.RemoteFile; |
|
|
|
import org.dromara.sample.wayline.mapper.IAudioFileMapper; |
|
|
|
import org.dromara.sample.wayline.mapper.IWaylineFileMapper; |
|
|
|
import org.dromara.sample.wayline.model.dto.KmzFileProperties; |
|
|
|
import org.dromara.sample.wayline.model.dto.WaylineFileDTO; |
|
|
|
import org.dromara.sample.wayline.model.entity.AudioFileEntity; |
|
|
|
import org.dromara.sample.wayline.model.entity.WaylineFileEntity; |
|
|
|
import org.dromara.sample.wayline.service.IWaylineFileService; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -32,12 +40,16 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.util.DigestUtils; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import ws.schild.jave.Encoder; |
|
|
|
import ws.schild.jave.MultimediaObject; |
|
|
|
import ws.schild.jave.encode.AudioAttributes; |
|
|
|
import ws.schild.jave.encode.EncodingAttributes; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.*; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.security.MessageDigest; |
|
|
|
import java.sql.SQLException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
@ -58,6 +70,12 @@ public class WaylineFileServiceImpl implements IWaylineFileService { |
|
|
|
@Autowired |
|
|
|
private IWaylineFileMapper mapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IAudioFileMapper audioFileMapper; |
|
|
|
|
|
|
|
@DubboReference |
|
|
|
private RemoteFileService remoteFileService; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PaginationData<GetWaylineListResponse> getWaylinesByParam(String workspaceId, GetWaylineListRequest param) { |
|
|
|
// Paging Query
|
|
|
@ -239,6 +257,118 @@ public class WaylineFileServiceImpl implements IWaylineFileService { |
|
|
|
return new PaginationData<>(records, new Pagination(page.getCurrent(), page.getSize(), page.getTotal())); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public AudioFileEntity transformation(MultipartFile multipartFile, String workspaceId) { |
|
|
|
File file = new File(multipartFile.getOriginalFilename()); |
|
|
|
AudioFileEntity audioFileEntity = new AudioFileEntity(); |
|
|
|
RemoteFile upload = new RemoteFile(); |
|
|
|
try { |
|
|
|
// multipartFile.transferTo(file);
|
|
|
|
// 1. 获取项目根目录路径
|
|
|
|
String projectRoot = System.getProperty("user.dir"); |
|
|
|
|
|
|
|
// 2. 创建目标文件对象
|
|
|
|
file = new File(projectRoot + File.separator + multipartFile.getOriginalFilename()); |
|
|
|
|
|
|
|
// 3. 写入MP3文件(这里假设已有字节数据)
|
|
|
|
Files.write(file.toPath(), multipartFile.getBytes()); |
|
|
|
|
|
|
|
// 4. 这里使用文件(例如播放、处理等操作)
|
|
|
|
System.out.println("文件已保存至:" + file.getAbsolutePath()); |
|
|
|
// TODO: 在此处添加实际使用文件的代码
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException("保存到本地失败"); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
File target = new File(multipartFile.getOriginalFilename().replaceAll("\\.[^.]*$", "") +".opus"); |
|
|
|
audioFileEntity.setOldName(multipartFile.getOriginalFilename()); |
|
|
|
AudioAttributes audio = new AudioAttributes(); |
|
|
|
audio.setCodec("libopus"); |
|
|
|
audio.setBitRate(32_000); // 32kbps
|
|
|
|
audio.setChannels(2); |
|
|
|
audio.setSamplingRate(48000); |
|
|
|
EncodingAttributes attrs = new EncodingAttributes(); |
|
|
|
attrs.setOutputFormat("opus"); |
|
|
|
attrs.setAudioAttributes(audio); |
|
|
|
|
|
|
|
Encoder encoder = new Encoder(); |
|
|
|
encoder.encode(new MultimediaObject(file), target, attrs); |
|
|
|
System.out.println("转换成功"); |
|
|
|
upload = remoteFileService.upload(target.getName(), target.getName(), multipartFile.getContentType(), Files.readAllBytes(target.toPath())); |
|
|
|
//转md5
|
|
|
|
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
|
|
try (FileInputStream fis = new FileInputStream(target)) { |
|
|
|
byte[] buffer = new byte[8192]; // 缓冲区大小
|
|
|
|
int bytesRead; |
|
|
|
// 分块更新哈希值
|
|
|
|
while ((bytesRead = fis.read(buffer)) != -1) { |
|
|
|
md.update(buffer, 0, bytesRead); |
|
|
|
} |
|
|
|
} |
|
|
|
// 转换为十六进制字符串
|
|
|
|
byte[] digest = md.digest(); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
for (byte b : digest) { |
|
|
|
sb.append(String.format("%02x", b & 0xff)); |
|
|
|
} |
|
|
|
String md5 = sb.toString(); |
|
|
|
//转流
|
|
|
|
audioFileEntity.setInputStream(Files.readAllBytes(target.toPath())); |
|
|
|
//刪除臨時文件
|
|
|
|
Files.delete(file.toPath()); |
|
|
|
Files.delete(target.toPath()); |
|
|
|
//插入文件数据库
|
|
|
|
audioFileEntity.setFileId(UUID.randomUUID().toString()); |
|
|
|
audioFileEntity.setFileName(upload.getOriginalName()); |
|
|
|
audioFileEntity.setFilePath(upload.getUrl()); |
|
|
|
audioFileEntity.setSign(md5); |
|
|
|
audioFileEntity.setWorkspaceId(workspaceId); |
|
|
|
audioFileEntity.setObjectKey(upload.getName()); |
|
|
|
audioFileEntity.setOssId(upload.getOssId()); |
|
|
|
if (audioFileEntity.getInputStream() != null){ |
|
|
|
String base64Data = Base64.getEncoder().encodeToString(audioFileEntity.getInputStream()); |
|
|
|
audioFileEntity.setInputStreamBase(base64Data); |
|
|
|
} |
|
|
|
audioFileMapper.insert(audioFileEntity); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
throw new RuntimeException("转换file失败"); |
|
|
|
} |
|
|
|
return audioFileEntity; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void audioDeleted(String workspaceId, String fileId) { |
|
|
|
AudioFileEntity audioFileEntity = audioFileMapper.selectOne(new QueryWrapper<AudioFileEntity>().eq("workspace_id", workspaceId).eq("file_id", fileId)); |
|
|
|
List<Long> list = new ArrayList<>(); |
|
|
|
list.add(audioFileEntity.getOssId()); |
|
|
|
remoteFileService.remove(list, true); |
|
|
|
audioFileMapper.delete(new QueryWrapper<AudioFileEntity>().eq("workspace_id",workspaceId).eq("file_id",fileId)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public PaginationData<AudioFileEntity> getAudioPage(String workspaceId, int pageNum, int pageSize) { |
|
|
|
Page<AudioFileEntity> pageQuery = new Page<>(pageNum, pageSize); |
|
|
|
Page<AudioFileEntity> page1 = audioFileMapper.selectPage( |
|
|
|
pageQuery, |
|
|
|
new QueryWrapper<AudioFileEntity>() |
|
|
|
.eq("workspace_id", workspaceId)); |
|
|
|
|
|
|
|
// Wrap the results of a paging query into a custom paging object.
|
|
|
|
List<AudioFileEntity> records = page1.getRecords(); |
|
|
|
for (AudioFileEntity audioFileEntity : records){ |
|
|
|
if (io.seata.common.util.StringUtils.isNotBlank(audioFileEntity.getInputStreamBase())){ |
|
|
|
byte[] data = Base64.getDecoder().decode(audioFileEntity.getInputStreamBase()); |
|
|
|
audioFileEntity.setInputStream(data); |
|
|
|
} |
|
|
|
} |
|
|
|
return new PaginationData<>(records, new Pagination(page1.getCurrent(), page1.getSize(), page1.getTotal())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private Optional<WaylineFileDTO> validKmzFile(MultipartFile file) { |
|
|
|
String filename = file.getOriginalFilename(); |
|
|
|
if (Objects.nonNull(filename) && !filename.endsWith(WAYLINE_FILE_SUFFIX)) { |
|
|
|