Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/CR-1799-Support data from uploads in ftp-file-upload addon #70

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ftp_file_upload 2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>ftp_file_upload</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
import org.apache.commons.net.ftp.FTPClient;
import org.openqa.selenium.NoSuchElementException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.nio.file.Paths;

@Data
@Action(actionText = "FTP: Connect to FTP server and upload a file. FTP server details: Host: Host-Name, Port: Port-No UserName: User-Name, Password: User-Password, Upload from Local-File-Path to Remote-Directory(ex: /Users/username/Downloads) with file name Remote-File-Name. Uploaded file path will be stored in runtime variable: variable-name",
description = "Uploads a file from local system to remote server using FTP, and stores the uploaded file path in a runtime variable.",
@Action(actionText = "FTP: Connect to FTP server and upload a file. FTP server details: Host: Host-Name, Port: Port-No, UserName: User-Name, Password: User-Password, Upload from Local-File-Path to Remote-Directory(ex: /Users/username/Downloads) with file name Remote-File-Name. Uploaded file path will be stored in runtime variable: variable-name",
description = "Uploads a file from a local file or URL to a remote server using FTP, and stores the uploaded file path in a runtime variable.",
applicationType = ApplicationType.WEB,
useCustomScreenshot = false)
public class FTPUploadFile extends WindowsAction {
Expand All @@ -36,7 +35,7 @@ public class FTPUploadFile extends WindowsAction {
private com.testsigma.sdk.TestData userPassword;

@TestData(reference = "Local-File-Path")
private com.testsigma.sdk.TestData localFilePath; // Local file to upload
private com.testsigma.sdk.TestData localFilePath; // Local file path or URL

@TestData(reference = "Remote-Directory")
private com.testsigma.sdk.TestData remoteDirectory; // Remote directory to upload to
Expand All @@ -63,29 +62,36 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
String remoteFile = remoteFileName.getValue().toString();

FTPClient ftpClient = new FTPClient();
File localFileToUpload = null;
try {
// Determine if the localFile is a URL or a local path
if (localFile.startsWith("http://") || localFile.startsWith("https://") || localFile.startsWith("file://")) {
localFileToUpload = downloadFile(localFile);
} else {
localFileToUpload = new File(localFile);
}


// Verify the local file exists
File firstLocalFile = new File(localFile);
if (!firstLocalFile.exists()) {
if (!localFileToUpload.exists()) {
setErrorMessage("Local file not found: " + localFile);
return Result.FAILED;
}

// Append extension if remote file name has no extension
if (!remoteFile.contains(".")) {
String localFileName = firstLocalFile.getName();
String localFileName = localFileToUpload.getName();
int dotIndex = localFileName.lastIndexOf('.');
if (dotIndex > 0) { // Local file has an extension
String extension = localFileName.substring(dotIndex); // Extract the extension
remoteFile += extension; // Append the extension to remote file name
if (dotIndex > 0) {
String extension = localFileName.substring(dotIndex);
remoteFile += extension;
logger.info("Remote file name updated to include extension: " + remoteFile);
} else {
setErrorMessage("Local file does not have an extension: " + localFileName);
return Result.FAILED;
}
}

// Connect and login to the server
ftpClient.connect(host, Integer.parseInt(port));
boolean loginSuccess = ftpClient.login(user, password);

Expand All @@ -97,7 +103,6 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

// Set the target directory on the FTP server
boolean changedDir = ftpClient.changeWorkingDirectory(remoteDir);
if (changedDir) {
logger.info("Changed to directory: " + remoteDir);
Expand All @@ -106,8 +111,7 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
return Result.FAILED;
}

// Upload the file
try (InputStream inputStream = new FileInputStream(firstLocalFile)) {
try (InputStream inputStream = new FileInputStream(localFileToUpload)) {
logger.info("Start uploading file to " + remoteDir + "/" + remoteFile);
boolean done = ftpClient.storeFile(remoteFile, inputStream);

Expand Down Expand Up @@ -137,6 +141,24 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
} catch (IOException ex) {
logger.warn("Error while closing FTP connection: " + ex);
}
if (localFileToUpload != null && localFile.startsWith("http")) {
localFileToUpload.delete(); // delete temp file if it was a download
}
}
}

private File downloadFile(String fileUrl) throws IOException {
URL url = new URL(fileUrl);
String fileName = Paths.get(url.getPath()).getFileName().toString();
File tempFile = File.createTempFile("downloaded-", fileName);
try (InputStream in = url.openStream();
OutputStream out = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
return tempFile;
}
}
}