/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.service.sysmgr.service.impl;

import com.crunii.micro.common.ftp.RemoteFile;
import com.crunii.micro.common.utils.DateUtil;
import com.crunii.micro.common.utils.FileUtil;
import com.crunii.micro.common.utils.NanoIdUtil;
import com.crunii.micro.service.sysmgr.service.UpDownDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @author 田平 create 2019年3月29日下午3:58:24
 */
@Service(value = "upDownDemoService")
public class UpDownDemoServiceImpl implements UpDownDemoService {

	@Autowired
	private RemoteFile remoteFile;

	@Value("${image.store.path}")
	private String imageStorePath;

	public byte[] getFileBytes(String path, String name) {
		byte[] bytes = remoteFile.getFileBytes(path, name);
		return bytes;
	}

	@Override
	public void storeFile(MultipartFile configFile) {
		String id = NanoIdUtil.randomLowerNanoId();
		try {
			// 远程目录不存在会自动创建目录
			remoteFile.uploadFile(FileUtil.getRealPath(imageStorePath) + DateUtil.format(new Date(), "yyyyMMdd"), id + ".txt",
					configFile.getInputStream());
		} catch (IOException e) {
			throw new RuntimeException("sftpTool.uploadFileSftp error!", e);
		}
	}
}
