diff --git a/sf_base/commons/common.py b/sf_base/commons/common.py index aa27c46f..30c06b6d 100644 --- a/sf_base/commons/common.py +++ b/sf_base/commons/common.py @@ -217,5 +217,4 @@ class PrintingUtils(models.AbstractModel): if os.path.exists(qr_temp_path): os.remove(qr_temp_path) if os.path.exists(output_temp_path): - os.remove(output_temp_path) os.remove(output_temp_path) \ No newline at end of file diff --git a/sf_machine_connect/models/ftp_operate.py b/sf_machine_connect/models/ftp_operate.py index 8265b351..b6724588 100644 --- a/sf_machine_connect/models/ftp_operate.py +++ b/sf_machine_connect/models/ftp_operate.py @@ -261,8 +261,50 @@ def transfer_files( target_path = f"{target_dir}/{relative_path}/{item}" else: target_path = f"{target_dir}/{item}" - with open(temp_path, 'rb') as f: - target_ftp.ftp.storbinary(f'STOR {target_path}', f) + + # 规范化路径 + target_path = target_path.replace('\\', '/').strip('/') + + # 确保目标目录存在 + target_dir_path = '/'.join(target_path.split('/')[:-1]) + try: + target_ftp.ftp.cwd('/') # 回到根目录 + for dir_part in target_dir_path.split('/'): + if dir_part: + try: + target_ftp.ftp.cwd(dir_part) + except: + try: + target_ftp.ftp.mkd(dir_part) + target_ftp.ftp.cwd(dir_part) + except Exception as e: + logging.error(f"创建目录失败 {dir_part}: {str(e)}") + raise + except Exception as e: + logging.error(f"处理目标目录失败: {str(e)}") + raise + + # 检查FTP连接状态 + try: + target_ftp.ftp.voidcmd('NOOP') + except: + logging.error("FTP连接已断开,尝试重新连接") + target_ftp.ftp.connect(target_ftp_info['host'], target_ftp_info['port']) + target_ftp.ftp.login(target_ftp_info['username'], target_ftp_info['password']) + + # 上传文件 + try: + with open(temp_path, 'rb') as f: + # 检查文件是否可读 + content = f.read() + if not content: + raise Exception("临时文件为空") + f.seek(0) # 重置文件指针 + target_ftp.ftp.storbinary(f'STOR {target_path}', f) + except Exception as e: + logging.error(f"上传文件失败: {str(e)}") + logging.error(f"目标路径: {target_path}") + raise transfered_file_list.append(item) # 删除临时文件 @@ -270,37 +312,37 @@ def transfer_files( logging.info(f"已传输文件: {item}") # 清空目标目录下的所有内容 - try: - target_ftp.ftp.cwd(target_dir) - files = target_ftp.ftp.nlst() + # try: + # target_ftp.ftp.cwd(target_dir) + # files = target_ftp.ftp.nlst() - for f in files: - try: - # 尝试删除文件 - target_ftp.ftp.delete(f) - except: - try: - # 如果删除失败,可能是目录,递归删除目录 - def remove_dir(path): - target_ftp.ftp.cwd(path) - sub_files = target_ftp.ftp.nlst() - for sf in sub_files: - try: - target_ftp.ftp.delete(sf) - except: - remove_dir(f"{path}/{sf}") - target_ftp.ftp.cwd('..') - target_ftp.ftp.rmd(path) + # for f in files: + # try: + # # 尝试删除文件 + # target_ftp.ftp.delete(f) + # except: + # try: + # # 如果删除失败,可能是目录,递归删除目录 + # def remove_dir(path): + # target_ftp.ftp.cwd(path) + # sub_files = target_ftp.ftp.nlst() + # for sf in sub_files: + # try: + # target_ftp.ftp.delete(sf) + # except: + # remove_dir(f"{path}/{sf}") + # target_ftp.ftp.cwd('..') + # target_ftp.ftp.rmd(path) - remove_dir(f"{target_dir}/{f}") - except: - logging.error(f"无法删除 {f}") - pass + # remove_dir(f"{target_dir}/{f}") + # except: + # logging.error(f"无法删除 {f}") + # pass - logging.info(f"已清空目标目录 {target_dir}") - except Exception as e: - logging.error(f"清空目标目录失败: {str(e)}") - raise Exception(f"清空目标目录失败: {str(e)}") + # logging.info(f"已清空目标目录 {target_dir}") + # except Exception as e: + # logging.error(f"清空目标目录失败: {str(e)}") + # raise Exception(f"清空目标目录失败: {str(e)}") # 开始遍历 traverse_dir(source_dir)