Set up basic SFTP upload
This commit is contained in:
parent
d7852e281d
commit
dbba2d2285
@ -1,6 +1,9 @@
|
|||||||
package name.nathanmcrae.numbersstation;
|
package name.nathanmcrae.numbersstation;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.ChannelSftp;
|
||||||
|
import com.jcraft.jsch.JSch;
|
||||||
import com.jcraft.jsch.JSchException;
|
import com.jcraft.jsch.JSchException;
|
||||||
|
import com.jcraft.jsch.Session;
|
||||||
import com.jcraft.jsch.SftpException;
|
import com.jcraft.jsch.SftpException;
|
||||||
import com.leakyabstractions.result.api.Result;
|
import com.leakyabstractions.result.api.Result;
|
||||||
import com.leakyabstractions.result.core.Results;
|
import com.leakyabstractions.result.core.Results;
|
||||||
@ -12,6 +15,8 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.logging.FileHandler;
|
import java.util.logging.FileHandler;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -135,11 +140,17 @@ public class Main extends Application {
|
|||||||
try {
|
try {
|
||||||
runStation(parsedArgs);
|
runStation(parsedArgs);
|
||||||
} catch (StationRunException e) {
|
} catch (StationRunException e) {
|
||||||
// TODO: Add time of run
|
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
|
||||||
|
String message = "Attempted to run station '" +
|
||||||
|
parsedArgs.getStationName() +
|
||||||
|
"' at " +
|
||||||
|
LocalDateTime.now().format(dateFormatter) +
|
||||||
|
"\n\n" +
|
||||||
|
e.getMessage();
|
||||||
var notification = new NotificationController.NotificationParameters("Error running station '" + parsedArgs.getStationName() + "'",
|
var notification = new NotificationController.NotificationParameters("Error running station '" + parsedArgs.getStationName() + "'",
|
||||||
e.getMessage(),
|
message,
|
||||||
NotificationController.NotificationType.ERROR,
|
NotificationController.NotificationType.ERROR,
|
||||||
Optional.empty());
|
Optional.empty());
|
||||||
startParams = new StartParameters(Optional.of(notification));
|
startParams = new StartParameters(Optional.of(notification));
|
||||||
|
|
||||||
launch(args);
|
launch(args);
|
||||||
@ -202,10 +213,30 @@ public class Main extends Application {
|
|||||||
messageText = new String(Files.readAllBytes(nextMessagePath));
|
messageText = new String(Files.readAllBytes(nextMessagePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send message using appropriate method
|
|
||||||
switch (loadedStation.getMessageMethod()) {
|
switch (loadedStation.getMessageMethod()) {
|
||||||
case StationSettings.MessageMethod.SFTP:
|
case StationSettings.MessageMethod.SFTP:
|
||||||
//loadedStation.uploadNextMessageToSFTP();
|
//loadedStation.uploadNextMessageToSFTP();
|
||||||
|
JSch jsch = new JSch();
|
||||||
|
try {
|
||||||
|
Session session = jsch.getSession(loadedStation.getUsername(), loadedStation.getAddress(), 22);
|
||||||
|
session.setPassword(loadedStation.getPassword());
|
||||||
|
session.setConfig("StrictHostKeyChecking", "no");
|
||||||
|
session.connect(30000); // 30 seconds timeout
|
||||||
|
|
||||||
|
if (session.isConnected()) {
|
||||||
|
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
|
||||||
|
channel.connect();
|
||||||
|
|
||||||
|
String localFile = loadedStation.nextMessagePath().toString();
|
||||||
|
channel.put(localFile, "www/message.txt");
|
||||||
|
channel.exit();
|
||||||
|
session.disconnect();
|
||||||
|
} else {
|
||||||
|
logger.log(Level.SEVERE, "SFTP connection failed");
|
||||||
|
}
|
||||||
|
} catch (JSchException e) {
|
||||||
|
logger.log(Level.SEVERE, "SFTP connection failed", e);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case StationSettings.MessageMethod.WORDPRESS:
|
case StationSettings.MessageMethod.WORDPRESS:
|
||||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user