All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.file.adapter.FileController Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.file.adapter;

import com.github.rexsheng.springboot.faster.io.file.FileSystemService;
import com.github.rexsheng.springboot.faster.io.file.model.DownloadFileRequest;
import com.github.rexsheng.springboot.faster.io.file.model.DownloadFileResponse;
import com.github.rexsheng.springboot.faster.logging.RequestLog;
import com.github.rexsheng.springboot.faster.security.IgnorePermission;
import com.github.rexsheng.springboot.faster.system.file.application.FileService;
import com.github.rexsheng.springboot.faster.system.file.application.dto.FileDetailResponse;
import com.github.rexsheng.springboot.faster.system.file.application.dto.FileDownloadRequest;
import com.github.rexsheng.springboot.faster.system.file.application.dto.FileLinkDTO;
import com.github.rexsheng.springboot.faster.util.ServletUtils;
import jakarta.annotation.Resource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/sys/file")
@ConditionalOnProperty(prefix = "app.module.file",name = "controller",havingValue = "true",matchIfMissing = true)
@ConditionalOnClass({Authentication.class, SqlSessionFactoryBean.class})
public class FileController {

    @Autowired(required = false)
    private FileSystemService fileSystemService;

    @Autowired(required = false)
    private FileService fileService;

    @GetMapping("/preview")
    @IgnorePermission
    @RequestLog(enabled = false)
    public void showImage(@RequestParam String url,@RequestParam(required = false) String name) {
        FileLinkDTO file= FileLinkDTO.parseDownloadLink(url);
        if(file==null){
            throw new IllegalArgumentException("参数无效");
        }
        DownloadFileResponse fileResponse= null;
        try {
            fileResponse = fileSystemService.downloadFile(new DownloadFileRequest(file.getBucket(),file.getObjectKey()));
            if(fileResponse==null){
                throw new IOException("文件异常");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件异常");
        }
        ServletUtils.responseWithStream(new ByteArrayInputStream(fileResponse.getContent()),name==null?file.getObjectName():name);
    }

    @PostMapping("/download")
    @PreAuthorize("@ss.denyApi()")
    public void downloadFile(@RequestBody @Validated FileDownloadRequest request) {
        FileDetailResponse fileResponse=fileService.getFile(request.getId());
        if(fileResponse==null){
            throw new IllegalArgumentException("参数无效");
        }
        if(fileResponse.getContent()==null){
            throw new IllegalArgumentException("文件不存在");
        }
        ServletUtils.responseWithStream(new ByteArrayInputStream(fileResponse.getContent()),request.getName());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy