hmmm
October 17, 2020, 2:07pm
1
Just wondering where I can find information about how filenames are formatted by media().writeData(). I understand the value it returns is a filename, so I assume it sanitises the string somehow. More to the point, is there a way to sanitise a string the same way without writing to media?
It’s all in the source code
If it’s just a raw data, it’ll be formatted with its hash as its filename.
# ext should include dot
def _addPastedImage(self, data: bytes, ext: str) -> str:
# hash and write
csum = checksum(data)
fname = "{}-{}{}".format("paste", csum, ext)
return self._addMediaFromData(fname, data)
Otherwise, it’s quite complicated. Everyting is done in Rust and at the moment, as far I can see, is not exposed and can’t be used with Python.
Take a look at rslib/src/media/files.rs. The whole file with tests in the end is pretty much about filename formatting.
In Anki 2.1.15 it was done in Python. Take a look at
return True
# Adding media
##########################################################################
# opath must be in unicode
def addFile(self, opath):
with open(opath, "rb") as f:
return self.writeData(opath, f.read())
def writeData(self, opath, data, typeHint=None):
# if fname is a full path, use only the basename
fname = os.path.basename(opath)
# if it's missing an extension and a type hint was provided, use that
if not os.path.splitext(fname)[1] and typeHint:
# mimetypes is returning '.jpe' even after calling .init(), so we'll do
# it manually instead
typeMap = {
"image/jpeg": ".jpg",
"image/png": ".png",
hmmm
October 24, 2020, 12:16am
3
Thanks for the comprehensive and considerate response !