package cn.hanghui.outapi.platform.tenant.controller;

import cn.hanghui.outapi.platform.common.controller.BaseController;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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 javax.servlet.ServletException;
import java.io.IOException;

@Slf4j
@Api(tags = "文件上传")
@RestController
@RequestMapping(value = "/api/v1/upload")
public class UploadController extends BaseController {

    @Value("${web.address}")
    private String address;

    @ApiOperation(value = "文件上传接口", notes = "文件上传接口")
    @PostMapping(value = "/android/apk")
    public RestResult<String> uploadApk(@RequestParam MultipartFile file) throws IOException, ServletException {
        String filePath = FileUtils.android_apk_base_path;
        String path = FileUtils.saveByCopy(filePath, file);
        path = address.concat(path);
        return RestResult.ok(path);
    }

    @ApiOperation(value = "图片上传", notes = "图片上传接口")
    @PostMapping(value = "/pic")
    public RestResult<String> uploadPic(@RequestParam MultipartFile file) throws IOException, ServletException {
        String filePath = FileUtils.pic_base_path;
        String path = FileUtils.saveByCopy(filePath, file);
        path = address.concat(path);
        return RestResult.ok(path);
    }

}
