Skip to content

Commit 5626c40

Browse files
Merged FTP tasks into ftp.py
1 parent 0dc0da6 commit 5626c40

File tree

2 files changed

+81
-85
lines changed

2 files changed

+81
-85
lines changed

tasks/upload/ftp/retrieve_from_ftp.py renamed to tasks/upload/ftp.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,87 @@
44
from netunicorn.base import Failure, Result, Success, Task
55

66

7+
class UploadToFTP(Task):
8+
"""
9+
Task for uploading a local file to an FTP Server. Establishes a connection to the specified FTP server, navigates to the desired remote directory, and uploads the specified local file.
10+
"""
11+
12+
def __init__(
13+
self,
14+
local_filepath: str,
15+
ftp_url: str,
16+
username: str,
17+
password: str,
18+
destination_dir: str = "/",
19+
timeout: int = 30,
20+
*args,
21+
**kwargs,
22+
):
23+
"""
24+
Initializes the UploadToFTP task with parameters.
25+
26+
Parameters:
27+
local_filepath (str): Path to local file to upload.
28+
ftp_url (str): URL or IP address of FTP.
29+
username (str): Username credential for FTP auth.
30+
password (str): Password credential for FTP auth.
31+
destination_dir (str): Destination directory on the FTP server where the file will be uploaded to. Defaults to "/".
32+
timeout (int, optional): Timeout value for FTP connection measued in seconds. Defaults to 30 seconds.
33+
"""
34+
super().__init__(*args, **kwargs)
35+
self.local_filepath = local_filepath
36+
self.ftp_url = ftp_url
37+
self.username = username
38+
self.password = password
39+
self.destination_dir = destination_dir
40+
self.timeout = timeout
41+
42+
def run(self) -> Result:
43+
"""
44+
Uploads the local file to FTP server.
45+
46+
Steps:
47+
1. Finds the specified local file.
48+
2. Connects to FTP server using provided credentials.
49+
3. Sets remote directory if specified, else defaults to "/".
50+
4. Uploads the local file to FTP server and closes connection.
51+
52+
Returns:
53+
Result:
54+
Success: Contains a success message if successful upload.
55+
--OR--
56+
Failure: Contains an error message if upload fails.
57+
58+
"""
59+
try:
60+
if not os.path.isfile(self.local_filepath):
61+
return Failure(f"Local file does not exist: {self.local_filepath}")
62+
63+
ftp = FTP(self.ftp_url, timeout=self.timeout) # Modify timeout as needed
64+
ftp.login(user=self.username, passwd=self.password)
65+
66+
if self.destination_dir:
67+
ftp.cwd(self.destination_dir)
68+
69+
with open(self.local_filepath, "rb") as f:
70+
remote_filename = os.path.basename(self.local_filepath)
71+
ftp.storbinary(f"STOR {remote_filename}", f)
72+
73+
ftp.quit()
74+
return Success(
75+
f"Successfully uploaded {self.local_filepath} to {self.ftp_url}/{self.destination_dir}"
76+
)
77+
78+
except FileNotFoundError:
79+
return Failure(f"File not found: {self.local_filepath}")
80+
81+
except error_perm as e:
82+
return Failure(f"FTP permission error: {e}")
83+
84+
except Exception as e:
85+
return Failure(f"An unexpected error occurred: {str(e)}")
86+
87+
788
class RetrieveFromFTP(Task):
889
"""
990
Task for retrieving a file from an FTP server to a local directory. Establishes a connection to the specified FTP server, navigates to the desired remote directory and downloads the specified file to a local directory.

tasks/upload/ftp/upload_to_ftp.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)