Skip to content

Commit b099079

Browse files
authored
Merge pull request #23 from achimoraites/fix/CVE-2023-4863
Fix/CVE 2023 4863
2 parents 673a451 + 6aba968 commit b099079

File tree

7 files changed

+103
-18
lines changed

7 files changed

+103
-18
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ We are using pytest, the `tests` folder contains our unit tests.
5555
pytest tests
5656
```
5757

58+
For improving our Unit Tests we are using [codium.ai](https://www.codium.ai/)
59+
5860
## Reporting Issues
5961

6062
- Check if the issue has already been reported.

raw_image_converter/__main__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
import os
33
from raw_image_converter.utils import (
4-
check_extension,
4+
check_file_type,
55
convert_file,
66
convert_raw,
77
image_not_exists,
@@ -83,8 +83,10 @@ def main():
8383
)
8484
print("Converting -> " + srcDir + " Directory !\n")
8585
for file in os.listdir(srcDir):
86+
file = file.lower()
8687
if image_not_exists(file, tgtDir, args.ext):
87-
if "RAW" == check_extension(file):
88+
type = check_file_type(file)
89+
if "RAW" == type:
8890
executor.submit(
8991
convert_raw,
9092
file,
@@ -94,7 +96,7 @@ def main():
9496
resolution,
9597
)
9698

97-
if "NOT_RAW" == check_extension(file):
99+
if "NOT_RAW" == type:
98100
executor.submit(
99101
convert_file,
100102
file,

raw_image_converter/utils.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from datetime import datetime
77
import shutil
88

9+
def split_file_extension(file):
10+
return os.path.splitext(file)
911

1012
def message(file, converted):
1113
current_time = datetime.now().time().strftime("%H:%M:%S")
@@ -20,7 +22,7 @@ def message(file, converted):
2022
# convert RAW images function
2123
def convert_raw(file, srcDir, tgtDir, extension=".jpg", resolution=("100%", "100%")):
2224
try:
23-
ext = "." + file.split(".")[-1].lower()
25+
filename, _ = split_file_extension(file)
2426
print(datetime.now().time().strftime("%H:%M:%S") + " Converting: " + file)
2527
source = os.path.join(srcDir, file)
2628
with rawpy.imread(source) as raw:
@@ -34,7 +36,7 @@ def convert_raw(file, srcDir, tgtDir, extension=".jpg", resolution=("100%", "100
3436
pil_image = pil_image.resize((width, height))
3537

3638
rgb = np.array(pil_image)
37-
imageio.imsave(os.path.join(tgtDir, file.replace(ext, "") + extension), rgb)
39+
imageio.imsave(os.path.join(tgtDir, filename + extension), rgb)
3840
message(file, True)
3941
except Exception as e:
4042
print(e)
@@ -59,15 +61,15 @@ def convert_file(file, srcDir, tgtDir, extension=".jpg", resolution=("100%", "10
5961
}
6062
save_format = mappings.get(extension, "JPEG")
6163
try:
62-
ext = "." + file.split(".")[-1].lower()
64+
filename, _ = split_file_extension(file)
6365
message(file, False)
6466
path = os.path.join(srcDir, file)
6567
im = Image.open(path)
6668
if resolution:
6769
width = calculate_image_dimension(im.width, resolution[0])
6870
height = calculate_image_dimension(im.height, resolution[1])
6971
im = im.resize((width, height))
70-
im.save(os.path.join(tgtDir, file.replace(ext, "") + extension), save_format)
72+
im.save(os.path.join(tgtDir, filename + extension), save_format)
7173
message(file, True)
7274
except:
7375
pass
@@ -86,27 +88,26 @@ def ai_2_pdf(file):
8688

8789
# IT IS POINTLESS TO CONVERT WHAT IS ALREADY CONVERTED!!!!
8890
def image_not_exists(image, tgtDir, tgtExt):
89-
ext = image.split(".")[-1].lower()
90-
target = os.path.join(tgtDir, image.replace(ext, tgtExt.replace(".", "")))
91+
filename, _ = split_file_extension(image)
92+
target = os.path.join(tgtDir, filename + tgtExt)
9193
if os.path.isfile(target):
9294
return False
9395
else:
9496
return True
9597

9698

9799
# here we check each file to decide what to do
98-
def check_extension(file):
100+
def check_file_type(file):
99101
# get the extension as a String and check if the string is contained in the array extensionsForRawConversion
100-
ext = "." + file.split(".")[-1].lower()
102+
_, ext = split_file_extension(file)
101103
# set supported raw conversion extensions!
102-
extensionsForRawConversion = [
104+
raw = [
103105
".dng",
104106
".raw",
105107
".cr2",
106108
".crw",
107109
".erf",
108110
".raf",
109-
".tif",
110111
".kdc",
111112
".dcr",
112113
".mos",
@@ -124,11 +125,11 @@ def check_extension(file):
124125
".mrw",
125126
]
126127
# set supported imageio conversion extensions
127-
extensionsForConversion = [".ppm", ".psd", ".tif", ".webp"]
128+
not_raw = [".ppm", ".psd", ".tiff", ".webp"]
128129

129-
if ext in extensionsForRawConversion:
130+
if ext in raw:
130131
return "RAW"
131-
if ext in extensionsForConversion:
132+
if ext in not_raw:
132133
return "NOT RAW"
133134
# check if an .ai exists and rename it to .pdf !
134135
ai_2_pdf(file)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
imageio==2.16.2
2-
Pillow==9.3.0
2+
Pillow==10.0.1
33
rawpy==0.17.1
44
numpy==1.22.3
55
colorama==0.4.6

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
keywords='cli, converter, raw, images',
2828
packages=["raw_image_converter"],
2929
install_requires=["numpy==1.22.3", "rawpy==0.17.1",
30-
"imageio==2.16.2", "Pillow==9.3.0", "colorama==0.4.6"],
30+
"imageio==2.16.2", "Pillow==10.0.1", "colorama==0.4.6"],
3131
entry_points={
3232
"console_scripts": [
3333
"raw_image_converter=raw_image_converter.__main__:main",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from unittest.mock import patch
2+
from raw_image_converter.utils import check_file_type
3+
4+
5+
class TestCheckFileType:
6+
# Case: Returns "RAW" if file extension is raw
7+
def test_returns_raw_if_extension_in_raw_conversion_array(self):
8+
extensions= [
9+
".dng",
10+
".raw",
11+
".cr2",
12+
".crw",
13+
".erf",
14+
".raf",
15+
".kdc",
16+
".dcr",
17+
".mos",
18+
".mef",
19+
".nef",
20+
".orf",
21+
".rw2",
22+
".pef",
23+
".x3f",
24+
".srw",
25+
".srf",
26+
".sr2",
27+
".arw",
28+
".mdc",
29+
".mrw",
30+
]
31+
32+
for ext in extensions:
33+
assert check_file_type(f"file{ext}") == "RAW"
34+
35+
# Case: Returns "NOT RAW" if file extension is not raw
36+
def test_returns_not_raw_if_extension_in_conversion_array(self):
37+
extensions2 = [".ppm", ".psd", ".webp", ".tiff"]
38+
39+
for ext in extensions2:
40+
assert check_file_type(f"file{ext}") == "NOT RAW"
41+
42+
# Case: Calls ai_2_pdf function if file extension is ".ai"
43+
def test_calls_ai_2_pdf_if_extension_is_ai(self):
44+
with patch("os.rename") as mock_rename:
45+
check_file_type("file.ai")
46+
mock_rename.assert_called_once_with("file.ai", "file.ai.pdf")
47+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from raw_image_converter.utils import split_file_extension
2+
3+
4+
class TestSplitFileExtension:
5+
# Case: Test with a file name with an extension
6+
def test_extension(self):
7+
file = "example.jpg"
8+
result = split_file_extension(file)
9+
assert result == ("example", ".jpg")
10+
11+
# Case: Test with a file name with no extension
12+
def test_no_extension(self):
13+
file = "example"
14+
result = split_file_extension(file)
15+
assert result == ("example", "")
16+
17+
# Case: Test with an empty file name
18+
def test_empty_file_name(self):
19+
file = ""
20+
result = split_file_extension(file)
21+
assert result == ("", "")
22+
23+
# Case: Test with a file name starting with a dot
24+
def test_starting_with_dot(self):
25+
file = ".example"
26+
result = split_file_extension(file)
27+
assert result == (".example", "")
28+
29+
# Case: Test with a file name containing multiple dots
30+
def test_multiple_dots(self):
31+
file = "example.file.with.dots"
32+
result = split_file_extension(file)
33+
assert result == ("example.file.with", ".dots")

0 commit comments

Comments
 (0)