package com.trade.service.filestorage.controller;

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

import com.trade.service.filestorage.dto.FileAbstractDto;
import com.trade.service.filestorage.util.watermark.PreviewUtil;
import org.springframework.stereotype.Controller;
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 com.trade.service.filestorage.enums.FileModuleEnum;
import com.trade.service.filestorage.service.OSSFileService;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * oss文件存储 前端控制器
 * </p>
 */
@Controller
@RequestMapping("/file_storage_oss1")
public class OSSFileStorageController1 {

    @Resource
    private OSSFileService fileService;

    /**
     * 单个文件上传
     */
    @RequestMapping(value = "/singleUpload")
    public FileAbstractDto uploadFile(@RequestParam("file") MultipartFile file, FileModuleEnum moduleDict) {
        return fileService.uploadFile(file, moduleDict);
    }

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

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

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