package com.trade.admin.file;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.trade.common.core.domain.ResponseResult;
import com.trade.service.filestorage.dto.FileAbstractDto;
import com.trade.service.filestorage.enums.FileModuleEnum;
import com.trade.service.filestorage.service.OSSFileService;

/**
 * <p>
 * oss文件存储 前端控制器 - B端
 * </p>
 */
@RestController
@RequestMapping("/file_storage_oss_backend")
public class OSSFileStorageController {

    @Resource
    private OSSFileService fileService;

    /**
     * 单个文件上传
     */
    @RequestMapping(value = "/singleUpload")
    public ResponseResult<FileAbstractDto> uploadFile(@RequestParam("file") MultipartFile file, String module) {
        return ResponseResult.success(fileService.uploadFile(file, module));
    }

    /**
     * 文件上传
     */
    @RequestMapping(value = "/upload")
    public ResponseResult<Void> uploadFile(HttpServletRequest request, FileModuleEnum moduleDict) {
        fileService.uploadFile(request, moduleDict);
        return ResponseResult.success();
    }

    /**
     * 文件下载
     */
    @RequestMapping(value = "/download/{fileId}")
    public ResponseResult<Void> downloadOneFile(HttpServletRequest request, HttpServletResponse res,
            @PathVariable Long fileId) {
        fileService.downloadFile(request, res, fileId);
        return ResponseResult.success();
    }

    /**
     * 预览非图片的</br>
     * 文档预览，不使用流直接输出到页面上，因为浏览器不支持这样的格式。通常word、excel都是用流下载的</br>
     * 根据文件的类型，用不同的处理方式来预览文件
     */
    @RequestMapping("/preview/{fileId}")
    public Object previewFile(@PathVariable Long fileId, @RequestParam(value = "name") String name,
            HttpServletRequest request, HttpServletResponse response, Model model) {
        String result = fileService.previewFile(fileId, request, response, model, name);
        return null == result ? null : ResponseResult.success(result);
    }
}
