package net.tensortec.imagestock.task;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.StreamGobbler;
import net.tensortec.imagestock.bean.Message;
import net.tensortec.imagestock.entity.AliyunImage;
import net.tensortec.imagestock.entity.UploadLog;
import net.tensortec.imagestock.mapper.AliyunImageMapper;
import net.tensortec.imagestock.mapper.UploadLogMapper;
import net.tensortec.imagestock.util.ImageUtil3;
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class AliyunImageTask{
/*public static void main(String[] args) throws Exception{//例子
Calendar now = Calendar.getInstance();
Integer year = now.get(Calendar.YEAR);//获取当前的年
Integer month = now.get(Calendar.MONTH)+1;//获取当前的月
String month2 = "";
if(month<10) {
month2="0"+month.toString();
}
String downDate = year.toString()+month2;//当天的日期
Connection con = new Connection("服务器ip");
ConnectionInfo connect = con.connect();
boolean isAuthed = con.authenticateWithPassword("root", "密码");
if(!isAuthed) {
System.out.println("远程服务器连接失败");
}
SCPClient scpClient = con.createSCPClient();//【2-1】建立SCP客户端,执行封装的方法
//scpClient.get("/mnt/pics/pics/2019080100/*", "/home/imgdb/imagestock/imagestock/aliyun_zip"); //从服务器获取文件
Session session = con.openSession(); //【2-2.1】开启会话,执行原生linux命令
session.execCommand("ls /mnt/pics/pics/2019080100/");//进入目录
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
scpClient.get("/mnt/pics/pics/2019080100/"+line, "/home/imgdb/imagestock/imagestock/aliyun_zip");
System.out.println("line:"+line);
}
br.close();
System.out.println("ExitCode: " + session.getExitStatus()); //获得退出状态
session.close();//关闭会话
con.close();//【3】关闭连接
}*/
@Autowired
private UploadLogMapper uploadLogMapper;//日志mapper
@Autowired
private AliyunImageMapper aliyunImageMapper;//存放图片表的mapper
//@Scheduled(cron = "0 42 9 ? * *")
//@Scheduled(cron = "*/60 * * ? * *")
@Scheduled(cron = "0 0 8,16,23 * * ?")//8,16,23点分别执行
@Transactional
public void sss()throws Exception {
System.out.println("------------阿里云服务器自动下载图片开始----------");
Calendar now = Calendar.getInstance();
Integer year = now.get(Calendar.YEAR);//获取当前的年
Integer month = now.get(Calendar.MONTH)+1;//获取当前的月
Integer day = now.get(Calendar.DAY_OF_MONTH);
String month2 = month.toString();
String day2=day.toString();
if(month<10) {//如果小于10月就在前面加0,如07月
month2="0"+month2;
}
if(day<10) {//和月同理
day2="0"+day2;
}
String downDate = year.toString()+month2+day2;//当天的日期
Connection con = new Connection("要连接的服务器ip");//要去连接的服务器
con.connect();
System.out.println("链接中");
boolean isAuthed = con.authenticateWithPassword("用户名", "密码");
if(!isAuthed) {
System.out.println("远程服务器连接失败");
}
SCPClient scpClient = con.createSCPClient();//【2-1】建立SCP客户端,执行封装的方法
Session session = con.openSession(); //【2-2.1】开启会话,执行原生linux命令
session.execCommand("ls /mnt/pics/pics/"+downDate+"*/*.jpg");//进入目录
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
UploadLog uploadLog = new UploadLog();
uploadLog.setUploadBy(18);
uploadLog.setAction("从120服务器下载当天的图片");
uploadLog.setRemark("自动下载:"+new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
uploadLogMapper.insert(uploadLog);
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
if (line.toString().contains(":")) {
continue;
}
scpClient.get("//"+line, "/home/imgdb/imagestock/imagestock/aliyun_zip");
}
InputStream in = null;
//从当前目录上传文件并删除当前文件
File file = new File("/home/imgdb/imagestock/imagestock/aliyun_zip/");
File[] files=file.listFiles();//获取当前文件下的所有文件
for(int k=0;k<files.length;k++){
String fromName=files[k].getName();
in = new BufferedInputStream(new FileInputStream("/home/imgdb/imagestock/imagestock/aliyun_zip/"+fromName+""));
MultipartFile multipartFile = new MockMultipartFile("/home/imgdb/imagestock/imagestock/aliyun_zip/"+fromName+"", in);
upload(multipartFile, 18,uploadLog);
files[k].delete();
}
System.out.println("------------阿里云服务器自动下载图片结束----------");
br.close();
System.out.println("ExitCode: " + session.getExitStatus()); //获得退出状态
session.close();//关闭会话
con.close();//【3】关闭连接
}
@Transactional
public Message<AliyunImage> upload(MultipartFile multipartFile, Integer userId, UploadLog uploadLog) throws IOException {
Message<AliyunImage> msg = Message.success();
ImageUtil3 imageUtil3 = new ImageUtil3(multipartFile);
boolean already = !aliyunImageMapper.selectList(new QueryWrapper<AliyunImage>().eq("hash_code", imageUtil3.getHashCode())).isEmpty();
if (already) {
msg.addError("图片已存在 " + imageUtil3.getFileName());
return msg;
}
String imageName = multipartFile.getName();
int idx = imageName.lastIndexOf(".");
String originalName = imageName.substring(0,idx);
String extendName = imageName.substring(idx+1);
imageUtil3.saveFile(extendName);
AliyunImage image = new AliyunImage();
image.setHashCode(imageUtil3.getHashCode());
image.setPath(imageUtil3.relativePath());
image.setOriginalName(originalName);
image.setExtendName(extendName);
image.setFormat("image/"+extendName);
image.setUploadBy(userId);
image.setVersion(1);
image.setLogId(uploadLog.getId());
if (aliyunImageMapper.insert(image) >= 1) {
} else {
File file = new File(imageUtil3.saveLocation());
if (file.exists()) {
FileUtils.deleteDirectory(file);
}
}
return msg;
}
}