package com.ff.file.controller;

import com.ff.base.annotation.Anonymous;
import com.ff.base.constant.Constants;
import com.ff.base.core.domain.AjaxResult;
import com.ff.base.exception.base.BaseException;
import com.ff.base.utils.MessageUtils;
import com.ff.file.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 文件管理
 *
 * @author ff
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private Map<String, ISysFileService> fileServiceMap;

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload/{service}")
    @Anonymous
    public AjaxResult uploadFile(MultipartFile file, @PathVariable String service) {

        ISysFileService sysFileService = fileServiceMap.get(service + Constants.SERVICE);
        if (sysFileService == null) {
            throw new BaseException(MessageUtils.message("upload.file.server.not.exist"));
        }
        return sysFileService.uploadFile(file);

    }

}
