Skip to content

Commit

Permalink
0.0.9
Browse files Browse the repository at this point in the history
fix upload_file and delete_file to return boolean as it did before switching the ftps client module for A1/P1 devices
  • Loading branch information
jneilliii committed Feb 11, 2024
1 parent 3d92d73 commit ef969d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
79 changes: 43 additions & 36 deletions octoprint_bambu_printer/ftpsclient/ftpsclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,51 +125,58 @@ def download_file(self, source: str, dest: str):
with open(dest, "wb") as file:
self.ftps_session.retrbinary(f"RETR {source}", file.write)

def upload_file(self, source: str, dest: str, callback=None):
def upload_file(self, source: str, dest: str, callback=None) -> bool:
"""upload a file to a path inside the FTPS server"""

file_size = os.path.getsize(source)

block_size = max(file_size // 100, 8192)
rest = None

# Taken from ftplib.storbinary but with custom ssl handling
# due to the shitty bambu p1p ftps server TODO fix properly.
with open(source, "rb") as fp:
self.ftps_session.voidcmd('TYPE I')

with self.ftps_session.transfercmd(f"STOR {dest}", rest) as conn:
while 1:
buf = fp.read(block_size)

if not buf:
break

conn.sendall(buf)

if callback:
callback(buf)

# shutdown ssl layer
if ftplib._SSLSocket is not None and isinstance(conn, ftplib._SSLSocket):
# Yeah this is suposed to be conn.unwrap
# But since we operate in prot p mode
# we can close the connection always.
# This is cursed but it works.
if "vsFTPd" in self.welcome:
conn.unwrap()
else:
conn.shutdown(socket.SHUT_RDWR)

return self.ftps_session.voidresp()

# Old api call.
# self.ftps_session.storbinary(
# f"STOR {dest}", file, blocksize=block_size, callback=callback)
try:
# Taken from ftplib.storbinary but with custom ssl handling
# due to the shitty bambu p1p ftps server TODO fix properly.
with open(source, "rb") as fp:
self.ftps_session.voidcmd('TYPE I')

with self.ftps_session.transfercmd(f"STOR {dest}", rest) as conn:
while 1:
buf = fp.read(block_size)

if not buf:
break

conn.sendall(buf)

if callback:
callback(buf)

# shutdown ssl layer
if ftplib._SSLSocket is not None and isinstance(conn, ftplib._SSLSocket):
# Yeah this is suposed to be conn.unwrap
# But since we operate in prot p mode
# we can close the connection always.
# This is cursed but it works.
if "vsFTPd" in self.welcome:
conn.unwrap()
else:
conn.shutdown(socket.SHUT_RDWR)

return True
except Exception as ex:
print(f"unexpected exception occurred: {ex}")
pass
return False

def delete_file(self, path: str):
def delete_file(self, path: str) -> bool:
"""delete a file from under a path inside the FTPS server"""
self.ftps_session.delete(path)
try:
self.ftps_session.delete(path)
return True
except Exception as ex:
print(f"unexpected exception occurred: {ex}")
pass
return False

def move_file(self, source: str, dest: str):
"""move a file inside the FTPS server to another path inside the FTPS server"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-BambuPrinter"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.0.8"
plugin_version = "0.0.9"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit ef969d3

Please sign in to comment.