def upload_file_for_admin(conn, admin_name, file_name, file_size, file_md5): # 上传文件至服务器 flag, path_in_server = is_file_exist(file_md5, 0, admin_name) if flag: return True file_path = os.path.join(setting.BASE_DIR, 'db', 'file_upload') if not os.path.exists(file_path): os.makedirs(file_path) path = os.path.join(setting.BASE_DIR, 'db', 'file_upload', file_name) if not os.path.exists(path): f = open(path, 'w') f.close() f = open(path, 'ab') has_received = 0 while has_received != file_size: data_once = conn.recv(1024) f.write(data_once) has_received += len(data_once) f.close() file_md5_finish = server_interface.get_file_md5(path) if file_md5_finish == file_md5: file_upload = models.File(file_name, file_size, file_md5, path, admin_name) save_upload_file_message(file_upload, 1, admin_name) logging.info('{} upload {}, the md5 is {}'.format( admin_name, file_name, file_md5)) return True else: return False
def upload_file_for_user(conn, user_name, file_name, file_size, file_md5, upload_file_path): file_path = os.path.join(setting.BASE_DIR, 'db', 'user', 'user_' + user_name, 'upload_files') if not os.path.exists(file_path): os.makedirs(file_path) path = os.path.join(file_path, upload_file_path) if not os.path.exists(path): os.makedirs(path) path = os.path.join(path, file_name) flag, path_in_server = is_file_exist(file_md5, 0, user_name) if flag: flag_copy = file_copy(path_in_server, path) if flag_copy: msg = '1'.encode('utf-8') conn.send(msg) return True else: return False msg = '0'.encode('utf-8') conn.send(msg) if not os.path.exists(path): # 创建空文件 f = open(path, 'w') f.close() f = open(path, 'ab') has_received = 0 while has_received != file_size: data_once = conn.recv(1024) f.write(data_once) has_received += len(data_once) # 发送传输进度 process_rate = int((has_received / file_size) * 1000) msg = str(process_rate).encode('utf-8') conn.send(msg) f.close() file_md5_finish = server_interface.get_file_md5(path) if file_md5_finish == file_md5: file_upload = models.File(file_name, file_size, file_md5, path, user_name) save_upload_file_message(file_upload, 0, user_name) logging.info('{} upload {}, the md5 is {}'.format( user_name, file_name, file_md5)) return True else: return False
def upload_file(): # 接收文件 file_path = os.path.join(BASE_DIR, 'db', 'file_upload') if not os.path.exists(file_path): os.makedirs(file_path) path = os.path.join(BASE_DIR, 'db', 'file_upload', file_name) if not os.path.exists(path): f = open(path, 'w') f.close() f = open(path, 'ab') has_received = 0 while has_received != file_size: data_once = conn.recv(1024) f.write(data_once) has_received += len(data_once) f.close() file_md5_finish = common_interface.get_file_md5(path) if file_md5_finish == file_md5: file_upload = models.File(file_name, file_size, file_md5, admin_name) db_handler.save_upload_file_message(file_upload) logging.info('{} upload {}, the md5 is {}'.format( admin_name, file_name, file_md5)) print('{} upload {}, the md5 is {}'.format(admin_name, file_name, file_md5))