forked from LiveCarta/BookConverter
Rewrite the processing of images
This commit is contained in:
@@ -13,18 +13,18 @@ def save_image_to_aws(access: Access, img_file_path: str, img_content: bytes, bo
|
||||
return link_path
|
||||
|
||||
|
||||
def save_image_locally(img_file_path: str, img_content: bytes, book_id: str):
|
||||
def save_image_locally(img_file_path: str, img_content: bytes, book_id: str) -> pathlib.Path:
|
||||
"""Function saves all images locally"""
|
||||
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
new_path = pathlib.Path(os.path.join(
|
||||
folder_path, f"../books/json/img_{book_id}/"))
|
||||
new_path.mkdir(exist_ok=True)
|
||||
|
||||
new_img_path = new_path / os.path.basename(img_file_path)
|
||||
f = open(new_img_path, "wb+")
|
||||
img_folder_path = new_path / os.path.basename(img_file_path)
|
||||
f = open(img_folder_path, "wb+")
|
||||
f.write(img_content)
|
||||
f.close()
|
||||
return new_img_path
|
||||
return img_folder_path
|
||||
|
||||
|
||||
def update_images_src_links(body_tag: BeautifulSoup,
|
||||
@@ -47,20 +47,17 @@ def update_images_src_links(body_tag: BeautifulSoup,
|
||||
img_content: bytes = img_href2img_content[path_to_img_from_root]
|
||||
if access is not None:
|
||||
if path_to_img_from_root in path2aws_path:
|
||||
new_folder = path2aws_path[path_to_img_from_root]
|
||||
img_folder_path = path2aws_path[path_to_img_from_root]
|
||||
else:
|
||||
new_folder = save_image_to_aws(
|
||||
img_folder_path = save_image_to_aws(
|
||||
access, path_to_img_from_root, img_content, book_id)
|
||||
path2aws_path[path_to_img_from_root] = new_folder
|
||||
path2aws_path[path_to_img_from_root] = img_folder_path
|
||||
else:
|
||||
new_folder = save_image_locally(
|
||||
img_folder_path = save_image_locally(
|
||||
path_to_img_from_root, img_content, book_id)
|
||||
|
||||
img.attrs["src"] = str(new_folder)
|
||||
if img.attrs.get("width"):
|
||||
del img.attrs["width"]
|
||||
if img.attrs.get("height"):
|
||||
del img.attrs["height"]
|
||||
if img.attrs.get("style"):
|
||||
del img.attrs["style"]
|
||||
img.attrs["src"] = str(img_folder_path)
|
||||
for attr in ["width", "height", "style"]:
|
||||
if img.attrs.get(attr):
|
||||
del img.attrs[attr]
|
||||
return path2aws_path
|
||||
|
||||
Reference in New Issue
Block a user