package com.hikcreate.service.fdfs.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.hikcreate.service.fdfs.FastDFSClient;
import com.hikcreate.service.fdfs.service.FileService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author 赵东
 * @create 2019/3/22 13:45
 */
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private FastDFSClient fastDFSClient;

    @Autowired
    private FastFileStorageClient storageClient;

    @Override
    public String uploadFile(byte[] file, String fileName) {
        try {
            //将文件上装到fast文件服务器上，成功则返回文件保存的路径
            String path = fastDFSClient.uploadFile(file, fileName);
            if (!StringUtils.hasText(path)) {
               // log.error("Upload Img Error");
            }
           // log.info("Upload Img Success. path {}", ("/group" + path.split("group")[1]));

            return "/group" + path.split("group")[1];
        } catch (Exception e) {
          // log.error("Upload Img Error, msg ={}", e);
           // throw new BusinessException("上传图片失败");
        }
    }

    @Override
    public byte[] downloadFile(String path) {
        return fastDFSClient.download(path);
    }
}
