-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_transfer_function.py
More file actions
250 lines (210 loc) · 8.92 KB
/
ftp_transfer_function.py
File metadata and controls
250 lines (210 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
@app.route('/api/sftp/transfer', methods=['POST'])
def sftp_transfer_images():
"""Transfer all images to FTP or sFTP server"""
global app_settings
try:
# Check if FTP/sFTP is configured
if not all([app_settings.get('ftp_server'),
app_settings.get('ftp_username'),
app_settings.get('ftp_password'),
app_settings.get('ftp_remote_path')]):
return jsonify({
"status": "error",
"message": "FTP/sFTP not configured. Please fill in all FTP settings."
}), 400
protocol = app_settings.get('ftp_protocol', 'ftp').lower()
print("="*80)
print(f"{protocol.upper()} TRANSFER REQUESTED")
print("="*80)
app.logger.info(f"{protocol.upper()} transfer started")
ftp_server = app_settings['ftp_server']
ftp_port = app_settings.get('ftp_port', 21 if protocol == 'ftp' else 22)
ftp_username = app_settings['ftp_username']
ftp_password = app_settings['ftp_password']
ftp_remote_path = app_settings['ftp_remote_path']
print(f"Connecting to {ftp_username}@{ftp_server}:{ftp_port}")
app.logger.info(f"Connecting to {ftp_username}@{ftp_server}:{ftp_port}")
# Get all images
image_pattern = os.path.join(IMAGE_DIR, "*_exp*ms.png")
image_files = glob.glob(image_pattern)
if not image_files:
return jsonify({
"status": "error",
"message": "No images found to transfer"
}), 404
print(f"Found {len(image_files)} images to transfer")
app.logger.info(f"Found {len(image_files)} images to transfer")
# Validate connection parameters
print(f"Protocol: {protocol.upper()}")
print(f"Server: {ftp_server}")
print(f"Port: {ftp_port}")
print(f"Username: {ftp_username}")
print(f"Remote path: {ftp_remote_path}")
transferred = 0
skipped = 0
errors = 0
if protocol == 'sftp':
# sFTP transfer using paramiko
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Connect to SSH server
print(f"Attempting sFTP connection...")
ssh.connect(
hostname=ftp_server,
port=ftp_port,
username=ftp_username,
password=ftp_password,
timeout=30,
allow_agent=False,
look_for_keys=False
)
print(f"SSH connected successfully")
app.logger.info("Connected to sFTP server successfully")
# Open SFTP session
sftp = ssh.open_sftp()
# Create remote directory if it doesn't exist
try:
sftp.chdir(ftp_remote_path)
except IOError:
# Directory doesn't exist, create it
dirs = []
current_path = ftp_remote_path
while current_path and current_path != '/':
dirs.insert(0, current_path)
current_path = os.path.dirname(current_path)
for dir_path in dirs:
try:
sftp.stat(dir_path)
except IOError:
sftp.mkdir(dir_path)
print(f"Created remote directory: {dir_path}")
sftp.chdir(ftp_remote_path)
print(f"Changed to remote directory: {ftp_remote_path}")
app.logger.info(f"Changed to remote directory: {ftp_remote_path}")
# Upload files
for image_path in image_files:
try:
filename = os.path.basename(image_path)
# Check if file already exists on remote
try:
sftp.stat(filename)
print(f"Skipping (already exists): {filename}")
skipped += 1
continue
except IOError:
pass
# Upload the file
print(f"Uploading: {filename}")
sftp.put(image_path, filename)
transferred += 1
except Exception as e:
print(f"Error transferring {filename}: {str(e)}")
app.logger.error(f"Error transferring {filename}: {str(e)}")
errors += 1
# Close connections
sftp.close()
ssh.close()
finally:
try:
sftp.close()
except:
pass
try:
ssh.close()
except:
pass
else:
# Regular FTP transfer
from ftplib import FTP
ftp = None
try:
print(f"Attempting FTP connection...")
ftp = FTP()
ftp.connect(ftp_server, ftp_port, timeout=30)
ftp.login(ftp_username, ftp_password)
print(f"FTP connected successfully")
app.logger.info("Connected to FTP server successfully")
# Create and change to remote directory
try:
ftp.cwd(ftp_remote_path)
except:
# Try to create the directory
dirs = ftp_remote_path.strip('/').split('/')
current = ''
for dir_name in dirs:
current += '/' + dir_name
try:
ftp.cwd(current)
except:
try:
ftp.mkd(current)
ftp.cwd(current)
print(f"Created remote directory: {current}")
except Exception as e:
print(f"Could not create directory {current}: {str(e)}")
print(f"Changed to remote directory: {ftp_remote_path}")
app.logger.info(f"Changed to remote directory: {ftp_remote_path}")
# Get list of existing files
existing_files = []
try:
existing_files = ftp.nlst()
except:
pass
# Upload files
for image_path in image_files:
try:
filename = os.path.basename(image_path)
# Check if file already exists
if filename in existing_files:
print(f"Skipping (already exists): {filename}")
skipped += 1
continue
# Upload the file
print(f"Uploading: {filename}")
with open(image_path, 'rb') as f:
ftp.storbinary(f'STOR {filename}', f)
transferred += 1
except Exception as e:
print(f"Error transferring {filename}: {str(e)}")
app.logger.error(f"Error transferring {filename}: {str(e)}")
errors += 1
# Close connection
ftp.quit()
except Exception as e:
if ftp:
try:
ftp.quit()
except:
pass
raise
print("="*80)
print(f"Transfer complete: {transferred} uploaded, {skipped} skipped, {errors} errors")
print("="*80)
app.logger.info(f"Transfer complete: {transferred} uploaded, {skipped} skipped, {errors} errors")
return jsonify({
"status": "success",
"message": f"Transfer complete: {transferred} uploaded, {skipped} skipped, {errors} errors",
"transferred": transferred,
"skipped": skipped,
"errors": errors
})
except ImportError as e:
error_msg = f"Required library not installed: {str(e)}"
print(error_msg)
app.logger.error(error_msg)
return jsonify({
"status": "error",
"message": error_msg
}), 500
except Exception as e:
error_msg = f"{protocol.upper() if 'protocol' in locals() else 'FTP'} transfer failed: {str(e)}"
print(error_msg)
app.logger.error(error_msg)
import traceback
traceback.print_exc()
return jsonify({
"status": "error",
"message": error_msg
}), 500