diff --git a/src/artwork/writer.py b/src/artwork/writer.py index 9526f6e..28cd383 100644 --- a/src/artwork/writer.py +++ b/src/artwork/writer.py @@ -116,18 +116,24 @@ def write_artworkdb( state["index"] += 1 state["offset"] = 0 filename = _ithmb_filename(fmt_id, state["index"]) - ithmb_files[fmt_id] = open(os.path.join(artwork_dir, filename), "wb") + ithmb_files[fmt_id] = open(os.path.join(artwork_dir, filename), "ab") def _write_payload(fmt_id: int, data: bytes) -> IthmbLocation: state = ithmb_state[fmt_id] if fmt_id not in ithmb_files: _open_next(fmt_id) - elif state["offset"] > 0 and state["offset"] + len(data) > ITHMB_MAX_SIZE_BYTES: - _open_next(fmt_id) + else: + f = ithmb_files[fmt_id] + f.seek(0, 2) + cur_size = f.tell() + if cur_size > 0 and cur_size + len(data) > ITHMB_MAX_SIZE_BYTES: + _open_next(fmt_id) + f = ithmb_files[fmt_id] + f.seek(0, 2) + offset = f.tell() + f.write(data) + state["offset"] = offset + len(data) filename = _ithmb_filename(fmt_id, state["index"]) - offset = state["offset"] - ithmb_files[fmt_id].write(data) - state["offset"] += len(data) return IthmbLocation(filename, offset) try: