|
|
@ -1,9 +1,12 @@ |
|
|
|
package org.dromara.sample.media.controller; |
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import com.amazonaws.util.IOUtils; |
|
|
|
import io.swagger.v3.oas.annotations.Operation; |
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag; |
|
|
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
|
import org.dromara.common.core.exception.ServiceException; |
|
|
|
import org.dromara.common.core.utils.file.FileUtils; |
|
|
|
import org.dromara.common.oss.core.OssClient; |
|
|
|
import org.dromara.common.oss.factory.OssFactory; |
|
|
|
import org.dromara.common.satoken.utils.LoginHelper; |
|
|
@ -11,15 +14,19 @@ import org.dromara.common.sdk.common.HttpResultResponse; |
|
|
|
import org.dromara.common.sdk.common.PaginationData; |
|
|
|
import org.dromara.sample.manage.service.IDeviceProService; |
|
|
|
import org.dromara.sample.media.model.MediaFileDTO; |
|
|
|
import org.dromara.sample.media.model.MediaFileEntity; |
|
|
|
import org.dromara.sample.media.service.IFileService; |
|
|
|
import org.dromara.sample.wayline.service.IWaylineJobService; |
|
|
|
import org.dromara.system.api.model.LoginUser; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
|
|
|
@ -36,6 +43,9 @@ public class FileController { |
|
|
|
@Autowired |
|
|
|
private IFileService fileService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IWaylineJobService waylineJobService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
@ -87,40 +97,68 @@ public class FileController { |
|
|
|
@RequestParam(name = "type", required = false) Integer type) { |
|
|
|
PaginationData<MediaFileDTO> filesList = fileService.getMediaFilesPaginationByJobId(workspaceId, page, pageSize,jobId,type); |
|
|
|
for (MediaFileDTO mediaFileDTO :filesList.getList()){ |
|
|
|
mediaFileDTO.setUrl(fileService.getObjectUrl(workspaceId, mediaFileDTO.getFileId()).toString()); |
|
|
|
mediaFileDTO.setUrl(fileService.getObjectUrl(mediaFileDTO.getObjectKey()).toString()); |
|
|
|
} |
|
|
|
return HttpResultResponse.success(filesList); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{workspace_id}/file/downloadZip/{job_id}") |
|
|
|
public void downloadZip(HttpServletResponse response, @PathVariable(name = "workspace_id") String workspaceId, |
|
|
|
@PathVariable(name = "job_id") String jobId, @RequestParam(name = "jobName", required = false) String jobName) throws UnsupportedEncodingException { |
|
|
|
List<MediaFileDTO> mediaFileDTO = fileService.getFilesByWorkspaceAndJobId(workspaceId, jobId); |
|
|
|
String fileName = "jobName.zip"; |
|
|
|
public void downloadZip( HttpServletResponse response, @PathVariable(name = "workspace_id") String workspaceId, |
|
|
|
@PathVariable(name = "job_id") String jobId, @RequestParam(name = "jobName", required = false) String jobName) throws IOException { |
|
|
|
List<String> mediaFileDTO = fileService.getFilesByWorkspaceAndJobId(workspaceId, jobId).stream().map(MediaFileDTO::getObjectKey).toList(); |
|
|
|
if(mediaFileDTO.size() == 0){ |
|
|
|
throw new ServiceException("文件不存在"); |
|
|
|
} |
|
|
|
OssClient storage = OssFactory.instance("mediafile"); |
|
|
|
response.setContentType("application/zip"); |
|
|
|
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); |
|
|
|
try (OutputStream out = response.getOutputStream(); |
|
|
|
ZipOutputStream zs = new ZipOutputStream(new BufferedOutputStream(out))) { |
|
|
|
if(ObjectUtil.isNull(jobName)){ |
|
|
|
jobName = waylineJobService.getJobByJobId(workspaceId,jobId).get().getJobName(); |
|
|
|
} |
|
|
|
String encodedJobName = URLEncoder.encode(jobName, "UTF-8"); |
|
|
|
response.setHeader("Content-Disposition", "attachment; filename="+encodedJobName+".zip"); |
|
|
|
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) { |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
for (String objectKey : mediaFileDTO) { |
|
|
|
try (InputStream inputStream = storage.getObjectContent(objectKey)) { |
|
|
|
// 创建一个新的 ZIP 条目
|
|
|
|
zipOut.putNextEntry(new ZipEntry(objectKey)); |
|
|
|
|
|
|
|
zs.setMethod(ZipOutputStream.DEFLATED); // 设置压缩方法
|
|
|
|
int len; |
|
|
|
// 读取文件内容并写入到 ZIP 输出流
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) { |
|
|
|
zipOut.write(buffer, 0, len); |
|
|
|
} |
|
|
|
|
|
|
|
for (MediaFileDTO mediaFile : mediaFileDTO) { |
|
|
|
OssClient storage = OssFactory.instance("mediafile"); |
|
|
|
//String privateUrlURL = storage.getPrivateUrl(mediaFile.getObjectKey(), 3600);
|
|
|
|
InputStream is = storage.getObjectContent(mediaFile.getObjectKey()); |
|
|
|
String uniqueFileName = mediaFile.getFileName(); // 假设MediaFileDTO有getFileName方法
|
|
|
|
ZipEntry entry = new ZipEntry(uniqueFileName); // 使用每个文件的实际名称
|
|
|
|
zs.putNextEntry(entry); |
|
|
|
IOUtils.copy(is, zs); |
|
|
|
zs.closeEntry(); |
|
|
|
// 完成当前条目的写入
|
|
|
|
zipOut.closeEntry(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); // 如果有某个文件出错,跳过该文件并继续处理其他文件
|
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
} |
|
|
|
|
|
|
|
zipOut.flush(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{workspace_id}/file/download/{file_id}") |
|
|
|
public void downloadZip( HttpServletResponse response, @PathVariable(name = "workspace_id") String workspaceId, |
|
|
|
@PathVariable(name = "file_id") String fileId) throws IOException { |
|
|
|
Optional<MediaFileEntity> mediaFileDTO = fileService.getObjectByFileId(workspaceId,fileId); |
|
|
|
|
|
|
|
OssClient storage = OssFactory.instance("mediafile"); |
|
|
|
String jobName = waylineJobService.getJobByJobId(workspaceId,mediaFileDTO.get().getJobId()).get().getJobName(); |
|
|
|
String[] filePaths = mediaFileDTO.get().getObjectKey().split("\\."); |
|
|
|
FileUtils.setAttachmentResponseHeader(response, jobName+"."+filePaths[filePaths.length-1]); |
|
|
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); |
|
|
|
long contentLength = storage.download(mediaFileDTO.get().getObjectKey(), response.getOutputStream()); |
|
|
|
response.setContentLengthLong(contentLength); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 根据文件id查询图片的偏航角 |
|
|
|
* @param fileId |
|
|
|