package com.xmomen.module.resource.service;

import com.xmomen.module.resource.api.DfsPath;
import com.xmomen.module.resource.api.DfsSdk;
import com.xmomen.module.resource.api.DfsService;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * @author tanxinzheng
 * @version 1.0.0
 * @date 2017-4-10 23:26:20
 */
public class ResourceUtilsService {

    /**
     * 获取资源的完整http路径
     * @param resourcePath 资源路径
     * @return 完整http路径
     */
    public static String getWholeHttpPath(String resourcePath) {
        if (StringUtils.isEmpty(resourcePath)) {
            return "";
        }
        DfsService dfsService = DfsSdk.getDfsInstance();
        // M00 是FDFS文件路径的一个标识
        if (resourcePath.indexOf("M00/") > -1) {
            return dfsService.getHttpPath(resourcePath);
        }
        else {
            return dfsService.getDmsHttpPath(resourcePath);
        }
    }

    /**
     * 上传文件
     * @param file 文件
     * @return 远程路径
     */
    public static String uploadFile(File file) {
        DfsService dfsServcie = DfsSdk.getDfsInstance();
        DfsPath path = dfsServcie.putObject(file, null, null);
        if (path == null) return null;
        return path.getRemotePath();
    }

    /**
     * 获取默认图片路径
     * @return 默认图片路径
     */
    public static String getDefaultPicPath() {
        DfsService dfsServcie = DfsSdk.getDfsInstance();
        return dfsServcie.getDefaultPath("PICTURE");
    }
}
