package com.philisense.www.cloud;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.philisense.www.exception.StorageException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @description : FastDF存储
 **/
public class FastDFSStorageService extends OssStorageService {

    private static Logger log = LogManager.getLogger(FastDFSStorageService.class);

    public FastDFSStorageService(OssStorageConfig config) {
        this.config = config;
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        try {
            //声明参数集合
            HashMap<String, Object> paramMap = new HashMap<>();
            //文件
            paramMap.put("file", inputStream);
            paramMap.put("path", path);
            paramMap.put("output","json");
            //上传
            String fastdfsUrl = HttpUtil.post(config.getDomain() + "/upload", paramMap);
            JSONObject jsonObject = JSONObject.parseObject(fastdfsUrl);

            return jsonObject.getString("url");
        } catch (Exception e) {
            throw new StorageException("上传文件失败", e);
        }
    }

    @Override
    public String upload(byte[] data, String path) {
        return StrUtil.EMPTY;
    }

    @Override
    public void delete(String path) {
        try {
            // 删除远程服务存储文件
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("path", path);
            String fastdfsUrl = HttpUtil.post(config.getDomain() + "/delete?path=" + path, paramMap);
            JSONObject jsonObject = JSON.parseObject(fastdfsUrl);
        } catch (Exception e) {
            throw new StorageException("删除文件失败", e);
        }
    }

    @Override
    public String getFileUrl(String path) {
        return path;
    }

    @Override
    public Map<String, Object> chunksCheck(String path, String name, String hash, Integer fileSize, Integer total) {
        return null;
    }

    @Override
    public Map<String, Object> chunksUpload(MultipartFile file, String path, String hash, String uploadId, Integer index, String fileSize) {
        return null;
    }

    @Override
    public String chunksMerge(String path, String name, String hash, String uploadId, String fileSize) {
        return null;
    }
}
