package com.nantian.cloud.jljc.service.impl;

import com.nantian.cloud.jljc.service.NginxService;
import com.nantian.cloud.jljc.tools.FtpUtil;
import com.nantian.cloud.jljc.tools.IDUtils;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
@Slf4j
public class NginxServiceImpl implements NginxService {

    public Object uploadPicture(MultipartFile uploadFile) {
        //给上传的文件生成新的文件名
        //获取原始文件名
        String oldName = uploadFile.getOriginalFilename();
        //使用IDUtils工具类生成新的文件名，新文件名 = newName + 文件后缀
        String newName = IDUtils.genImageName();
        assert oldName != null;
        newName = newName + oldName.substring(oldName.lastIndexOf("."));
        //生成文件在服务器端存储的子目录
        String filePath = new DateTime().toString("/yyyyMMdd/");

        //把文件上传到文件服务器
        //获取上传的io流
        InputStream input = null;
        try {
            input = uploadFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //调用FtpUtil工具类进行上传
        return FtpUtil.putImages(input, filePath, newName);
    }

}
