wangmengmeng
2024-12-24 24432a361d5c6bd6f3d8c008693e9f1155d62517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.dji.sample.media.controller;
 
import com.dji.sample.component.oss.service.IOssService;
import com.dji.sample.component.oss.service.impl.MinIOServiceImpl;
import com.dji.sample.media.model.MediaFileDTO;
import com.dji.sample.media.model.param.MediaFilesParam;
import com.dji.sample.media.model.param.PresignedUrlParam;
import com.dji.sample.media.service.IFileService;
import com.dji.sdk.common.HttpResultResponse;
import com.dji.sdk.common.PaginationData;
import io.minio.credentials.Credentials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
 
/**
 * @author sean
 * @version 0.2
 * @date 2021/12/9
 */
@RestController
@RequestMapping("${url.media.prefix}${url.media.version}/files")
public class FileController {
 
    @Autowired
    private IFileService fileService;
 
 
    /**
     * Get information about all the media files in this workspace based on the workspace id.
     * @return
     */
    @PostMapping("/get-file-list")
    public HttpResultResponse<PaginationData<MediaFileDTO>> getFilesList(@RequestParam(defaultValue = "1") Long page,
                                                                         @RequestParam(name = "page_size", defaultValue = "10") Long pageSize,
                                                                         @RequestBody MediaFilesParam mediaFilesParam) {
        PaginationData<MediaFileDTO> filesList = fileService.getMediaFilesPagination(page, pageSize,mediaFilesParam);
        return HttpResultResponse.success(filesList);
    }
 
    /**
     * Get information about all the media files in this workspace based on the workspace id.
     * @return
     */
    @PostMapping("/get-presigned-url")
    public HttpResultResponse<String> getPresignedUrl(@RequestBody PresignedUrlParam PresignedUrlParam) {
        try{
            String fileUrl = MinIOServiceImpl.getPresignedObjectUrl(PresignedUrlParam.getObjectKey(), PresignedUrlParam.getCredentials());
 
            return HttpResultResponse.success(fileUrl);
        }catch (Exception e){
            System.out.println("异常:"+e.getMessage());
 
            return HttpResultResponse.error(e.toString());
        }
//        return HttpResultResponse.success(filesList);
    }
 
    /**
     * Query the download address of the file according to the media file id,
     * and redirect to this address directly for download.
     * @param workspaceId
     * @param fileId
     * @param response
     */
    @GetMapping("/{workspace_id}/file/{file_id}/url")
    public void getFileUrl(@PathVariable(name = "workspace_id") String workspaceId,
                           @PathVariable(name = "file_id") String fileId, HttpServletResponse response) {
 
        try {
            URL url = fileService.getObjectUrl(workspaceId, fileId);
            response.sendRedirect(url.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}