package com.enterprise.manage.modules.system.controller;

import com.enterprise.manage.common.base.BaseController;
import com.enterprise.manage.common.constant.ControllerConstant;
import com.enterprise.manage.common.web.response.Result;
import com.enterprise.manage.modules.upload.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
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 java.util.HashMap;
import java.util.Map;

/**
 * system upload controller
 *
 * @author PeiZhi Guo
 * 2022-6-7 9:54
 */
@RestController
@RequestMapping(ControllerConstant.SYSTEM + "upload/v1")
public class UploadController extends BaseController {

    private IUploadService uploadService;

    @Autowired
    public UploadController(IUploadService uploadService) {
        this.uploadService = uploadService;
    }

    @PostMapping
    public Result upload(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        try {
            String path = uploadService.upload(file,"avatar");
            map.put("path", path);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("服务器上传异常：" + e.getMessage());
        }
        return success(map);
    }
}
