package com.izouma.uwip.web;

import com.izouma.uwip.exception.BusinessException;
import com.izouma.uwip.service.storage.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
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.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;


@RestController
@RequestMapping("/upload")
@Slf4j
public class FileUploadController {

    @Autowired
    private StorageService storageService;

    @PostMapping("/file")
    public String uploadFile(@RequestParam("file") MultipartFile file,
                             @RequestParam(value = "   ", required = false) String path) {
        if (path == null) {
            String basePath = "application";
            try {
                basePath = file.getContentType().split("/")[0];
            } catch (Exception ignored) {
            }
            path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
                   + RandomStringUtils.randomAlphabetic(8)
                   + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        }
        InputStream is;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            log.error("上传失败", e);
            throw new BusinessException("上传失败", e.getMessage());
        }
        return storageService.uploadFromInputStream(is, path);
    }

    @PostMapping("/base64")
    public String uploadImage(@RequestParam("base64") String base64,
                              @RequestParam(value = "path", required = false) String path) {
        base64 = base64.substring(base64.indexOf(',') + 1);
        Base64.Decoder decoder = Base64.getDecoder();
        if (path == null) {
            String ext = ".jpg";
            try {
                InputStream is = new ByteArrayInputStream(decoder.decode(base64.getBytes()));
                String type = URLConnection.guessContentTypeFromStream(is);
                ext = type.replace("image/", ".").replace("jpeg", "jpg");
            } catch (Exception ignored) {
            }
            path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
                   + RandomStringUtils.randomAlphabetic(8) + ext;
        }
        InputStream is;
        try {
            is = new ByteArrayInputStream(decoder.decode(base64.getBytes()));
        } catch (Exception e) {
            log.error("上传失败", e);
            throw new BusinessException("上传失败", e.getMessage());
        }
        return storageService.uploadFromInputStream(is, path);
    }
}
