package com.trade.service.filestorage.controller;

import com.trade.service.filestorage.enums.FileModuleEnum;
import com.trade.service.filestorage.service.LocalFileService;
import org.springframework.http.ResponseEntity;
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.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 本地文件存储 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/file_storage_local")
public class LocalFileStorageController {

    @Resource
    private LocalFileService fileStorageService;

    /**
     * 文件上传
     */
    @RequestMapping(value = "/upload")
    public void uploadFile(HttpServletRequest request, FileModuleEnum moduleDict) throws IOException {
        fileStorageService.uploadFile(request, moduleDict);
    }

    /**
     * 文件下载
     */
    @RequestMapping(value = "/download/{fileId}")
    public void downloadOneFile(HttpServletRequest request, HttpServletResponse res, @PathVariable Long fileId) {
        fileStorageService.downloadFile(request, res, fileId);
    }

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