mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
code review changes
Signed-off-by: Florian Brandes <florian.brandes@posteo.de>
This commit is contained in:
parent
bf57b6e4a2
commit
a7b1658ee1
@ -108,23 +108,22 @@ def convert_from_tiff_to_pdf(filepath: str) -> str:
|
|||||||
newpath = os.path.join(tempdir, file_name + ".pdf")
|
newpath = os.path.join(tempdir, file_name + ".pdf")
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Cannot convert from {str(file_extension)} to pdf.")
|
logger.warning(f"Cannot convert from {str(file_extension)} to pdf.")
|
||||||
return ""
|
return None
|
||||||
image = Image.open(filepath)
|
with Image.open(filepath) as image:
|
||||||
images = []
|
images = []
|
||||||
for i, page in enumerate(ImageSequence.Iterator(image)):
|
for i, page in enumerate(ImageSequence.Iterator(image)):
|
||||||
page = page.convert("RGB")
|
page = page.convert("RGB")
|
||||||
images.append(page)
|
images.append(page)
|
||||||
try:
|
try:
|
||||||
if len(images) == 1:
|
if len(images) == 1:
|
||||||
images[0].save(newpath)
|
images[0].save(newpath)
|
||||||
else:
|
else:
|
||||||
images[0].save(newpath, save_all=True, append_images=images[1:])
|
images[0].save(newpath, save_all=True, append_images=images[1:])
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"Could not save the file as pdf. Error: {str(e)}",
|
f"Could not save the file as pdf. Error: {str(e)}",
|
||||||
)
|
)
|
||||||
return ""
|
return None
|
||||||
image.close()
|
|
||||||
return newpath
|
return newpath
|
||||||
|
|
||||||
|
|
||||||
@ -242,23 +241,19 @@ def consume_file(
|
|||||||
f"Unsupported file format for barcode reader: {str(file_extension)}",
|
f"Unsupported file format for barcode reader: {str(file_extension)}",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if file_extension == ".tif" or file_extension == ".tiff":
|
if file_extension in {".tif", ".tiff"}:
|
||||||
converted_tiff = convert_from_tiff_to_pdf(path)
|
file_to_process = convert_from_tiff_to_pdf(path)
|
||||||
if converted_tiff:
|
|
||||||
separators = scan_file_for_separating_barcodes(converted_tiff)
|
|
||||||
else:
|
else:
|
||||||
separators = scan_file_for_separating_barcodes(path)
|
file_to_process = path
|
||||||
|
|
||||||
|
separators = scan_file_for_separating_barcodes(file_to_process)
|
||||||
|
|
||||||
if separators:
|
if separators:
|
||||||
if converted_tiff:
|
logger.debug(
|
||||||
logger.debug(
|
f"Pages with separators found in: {str(path)}",
|
||||||
f"Pages with separators found in: {str(converted_tiff)}",
|
)
|
||||||
)
|
document_list = separate_pages(file_to_process, separators)
|
||||||
document_list = separate_pages(converted_tiff, separators)
|
|
||||||
else:
|
|
||||||
logger.debug(
|
|
||||||
f"Pages with separators found in: {str(path)}",
|
|
||||||
)
|
|
||||||
document_list = separate_pages(path, separators)
|
|
||||||
if document_list:
|
if document_list:
|
||||||
for n, document in enumerate(document_list):
|
for n, document in enumerate(document_list):
|
||||||
# save to consumption dir
|
# save to consumption dir
|
||||||
@ -271,8 +266,8 @@ def consume_file(
|
|||||||
# if we got here, the document was successfully split
|
# if we got here, the document was successfully split
|
||||||
# and can safely be deleted
|
# and can safely be deleted
|
||||||
if converted_tiff:
|
if converted_tiff:
|
||||||
logger.debug("Deleting file {}".format(converted_tiff))
|
logger.debug("Deleting file {}".format(file_to_process))
|
||||||
os.unlink(converted_tiff)
|
os.unlink(file_to_process)
|
||||||
logger.debug("Deleting file {}".format(path))
|
logger.debug("Deleting file {}".format(path))
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
# notify the sender, otherwise the progress bar
|
# notify the sender, otherwise the progress bar
|
||||||
|
@ -225,8 +225,7 @@ class TestTasks(DirectoriesMixin, TestCase):
|
|||||||
)
|
)
|
||||||
dst = os.path.join(settings.SCRATCH_DIR, "simple.pdf")
|
dst = os.path.join(settings.SCRATCH_DIR, "simple.pdf")
|
||||||
shutil.copy(test_file, dst)
|
shutil.copy(test_file, dst)
|
||||||
target_file = tasks.convert_from_tiff_to_pdf(dst)
|
self.assertIsNone(tasks.convert_from_tiff_to_pdf(dst))
|
||||||
self.assertFalse(os.path.isfile(target_file))
|
|
||||||
|
|
||||||
def test_scan_file_for_separating_barcodes(self):
|
def test_scan_file_for_separating_barcodes(self):
|
||||||
test_file = os.path.join(
|
test_file = os.path.join(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user