-
Notifications
You must be signed in to change notification settings - Fork 11
/
pre_consume_title_change_detector.py
executable file
·30 lines (26 loc) · 1.23 KB
/
pre_consume_title_change_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
import os
from pathlib import Path
import hashlib
#import magic
#import tempfile
#import subprocess
if __name__ == "__main__":
filename = ""
# mime = magic.Magic(mime=True)
# mime_type = mime.from_file(os.environ["DOCUMENT_SOURCE_PATH"])
# if os.environ["DOCUMENT_SOURCE_PATH"][-3:].lower() == "pdf" and mime_type != "application/pdf":
# print("Redoing pdf to get the right mime-type")
# with tempfile.TemporaryDirectory() as tmp_dir:
# temp_filename = os.path.join(tmp_dir, "tmp.pdf")
# subprocess.run(("qpdf", os.environ["DOCUMENT_SOURCE_PATH"], temp_filename))
# os.replace(temp_filename, os.environ["DOCUMENT_SOURCE_PATH"])
# else:
# print(f"Mime_type of {os.environ['DOCUMENT_SOURCE_PATH']} was {mime_type}")
with open(os.environ["DOCUMENT_SOURCE_PATH"], "rb") as the_file:
read_file = the_file.read()
filename = "." + hashlib.sha256(read_file).hexdigest()
#print("preconsume is " + hashlib.sha256(os.environ["DOCUMENT_SOURCE_PATH"])
document_source_path = Path(os.environ["DOCUMENT_SOURCE_PATH"])
with open(filename, "w") as title_file:
title_file.write(document_source_path.name)