diff options
| -rw-r--r-- | src/discord_image_bridge/downloadpool.py | 1 | ||||
| -rw-r--r-- | src/discord_image_bridge/fsmanager.py | 8 | ||||
| -rw-r--r-- | src/discord_image_bridge/utils.py | 15 |
3 files changed, 22 insertions, 2 deletions
diff --git a/src/discord_image_bridge/downloadpool.py b/src/discord_image_bridge/downloadpool.py index fcf11cd..cfb17d4 100644 --- a/src/discord_image_bridge/downloadpool.py +++ b/src/discord_image_bridge/downloadpool.py @@ -47,6 +47,7 @@ class DownloadPool(): try: data["result"]=self.download_fn(*data["args"], **data["kwargs"]) if data["callback"]: + print("Calling callback") data["callback"](*data["result"]) except Exception as e: data["exception"]=e diff --git a/src/discord_image_bridge/fsmanager.py b/src/discord_image_bridge/fsmanager.py index 74b1587..c253917 100644 --- a/src/discord_image_bridge/fsmanager.py +++ b/src/discord_image_bridge/fsmanager.py @@ -20,23 +20,29 @@ class MetaFile: def __enter__(self): fname=hash2meta_fname(self.hash) self.fd=open( fname, "r+" if os.path.isfile(fname) else "x+" ) + print("Locking metadata:",fname) fcntl.lockf(self.fd, fcntl.LOCK_EX) + print("Locked",fname) return self def read(self): self.fd.seek(0) + print("Reading metadata") if self.fd.read(1): self.fd.seek(0) return json.load(self.fd) else: # file is empty + print("Empty") return {} def write(self, data): + print("Writing metadata") self.fd.seek(0) self.fd.truncate() json.dump(data, self.fd) def __exit__(self, type, value, traceback): + print("Unlock") fcntl.lockf(self.fd, fcntl.LOCK_UN) hashlocks:dict[str,Event]={} @@ -46,10 +52,12 @@ class DataFile: self.fd=fd def __enter__(self): + print("Lock datafile") fcntl.lockf(self.fd, fcntl.LOCK_EX) return self.fd def __exit__(self, type, value, exception): + print("Unlock datafile") fcntl.lockf(self.fd, fcntl.LOCK_UN) diff --git a/src/discord_image_bridge/utils.py b/src/discord_image_bridge/utils.py index eb0bf7e..454164f 100644 --- a/src/discord_image_bridge/utils.py +++ b/src/discord_image_bridge/utils.py @@ -12,6 +12,7 @@ from . import fsmanager download_locks={} def safe_mkdir(dirname): + print("Attempting to make dir "+dirname) try: return os.mkdir(dirname) except FileExistsError: @@ -32,12 +33,15 @@ def download_and_cache(url, filename): tmpname=fsmanager.hash2fname(hash,temp=True) dirname=os.path.dirname(tmpname) safe_mkdir(dirname) + print("Making file "+tmpname) with fsmanager.DataFile(open(tmpname,'wb')) as fd: fd.write(resp.content) fname=fsmanager.hash2fname(hash) dirname=os.path.dirname(fname) safe_mkdir(dirname) + print("Moving",tmpname,"to",fname) os.rename(tmpname, fname) + print("Done") return hash, fname @@ -46,20 +50,24 @@ def download_and_cache(url, filename): except OSError as e: cherrypy.log("Error writing "+filename+" to disk: "+repr(e)) return None, None + print("Somehow we ended up here") return None, None def download_uncached(hash): if not os.path.isfile(fsmanager.hash2meta_fname(hash)): + print("No need to download",hash,"already here") return None, None with download_locks.setdefault(hash, Lock()), fsmanager.MetaFile(hash) as metafd: target=fsmanager.hash2fname(hash) if os.path.isfile(target): + print("Aquired lock for downloading, but not needed.") return hash, target data=metafd.read() if not "sources" in data: cherrypy.log("No sources available for "+hash) return None, None for source in data["sources"] + data["sources"]: # try everything twice + print("Trying source",source) match source["type"]: case "discord": newhash, fname=download_uncached_discord( @@ -70,9 +78,11 @@ def download_uncached(hash): case _: # NOTE: maybe log here? + print("Unknown source type",source["type"]) return None, None if newhash.startswith(hash): + print("Hash matches") return hash, fname cherrypy.log("No working sources available for "+hash) return None, None @@ -81,6 +91,7 @@ def download_uncached_discord(channel, msgid, attachmentid): status,data=discord.channel_message_get(channel_id=channel, message_id=msgid) for attachment in data["attachments"]: if attachment["id"]==str(attachmentid): + print("Downloading",attachmentid,"from discord") return download_and_cache(attachment["url"],attachment["filename"]) return (None, None) @@ -127,8 +138,8 @@ def on_ready(plugin:discord.DiscordWsManager, client:discord.DiscordWsClient): "attachment":id }) metafd.write(data) - #cherrypy.log("Done") - + cherrypy.log("Done") + print("Call pool to download",attachment["url"]) download_pool.exec(args=(attachment["url"], fname),callback=callback) |
