package com.community.huiju.controller;

import com.community.commom.mode.ResponseBean;
import com.community.huiju.service.ImageServiceI;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author weichaochao
 * @Title: ImageController
 * @Description: 图片上传以及获取url
 * @date 2018/10/31
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "图片操作API", description = "图片操作API")
public class ImageController {

    @Autowired
    private ImageServiceI imageService;

    @ApiOperation(value = "图片上传以及获取url", notes = "图片上传以及获取url")
    @PostMapping(value = "/uploadimage", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean uploadImgAndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) MultipartFile[] uploadFiles) throws Exception {
        ResponseBean responseBean = new ResponseBean();
        List<String> urls = new ArrayList<String>();
        for (MultipartFile uploadFile : uploadFiles){
            String url = imageService.getImageUrl(uploadFile);
            urls.add(url);
        }
        responseBean.addSuccess(urls);
        return responseBean;
    }
}
