77import json
88import re
99
10+
1011def new_input (data , file_path ):
1112 """Take new input"""
1213 zip_file = data ['zip_files' ][0 ]
@@ -27,71 +28,57 @@ def new_input(data, file_path):
2728def sanitize (data , file_path ):
2829 """
2930 Validates the 'zip_file' data from the given dictionary.
30-
31+
3132 This function checks:
3233 1. That the 'name' attribute exists and ends with ".zip".
3334 2. That the 'size' attribute is a float, greater than 2, and has exactly two decimal places.
3435 3. That the 'md5' attribute is a valid 32-character hexadecimal MD5 hash.
3536 4. That the 'url' attribute matches one of the allowed URL patterns.
36-
37+
3738 Args:
3839 data (dict): The input dictionary containing 'zip_files' data.
39-
40+
4041 Exits:
4142 Exits the program with status 1 if any validation fails.
4243 """
43-
44- # Extract the first zip file data
4544 zip_file = data ['zip_files' ][0 ]
46-
47- # Validate attributes
48- if not zip_file .get ('name' ):
49- print ("The 'name' attribute is missing." )
50- new_input (data , file_path )
51- return
52-
53- if not isinstance (zip_file .get ('size' ), float ):
54- print ("The 'size' attribute is missing." )
55- new_input (data , file_path )
56- return
57-
58- if not zip_file .get ('md5' ):
59- print ("The 'md5' attribute is missing." )
60- new_input (data , file_path )
61- return
62-
63- if not zip_file .get ('url' ):
64- print ("The 'url' attribute is missing." )
65- new_input (data , file_path )
66- return
67-
68- # Validate name end with ".zip"
69- if not zip_file ['name' ].endswith (".zip" ) or "$" in zip_file ['name' ]:
70- print (f"The file { zip_file ['name' ]} is not a .zip file." )
71- new_input (data , file_path )
72- return
73-
74- # Validate 'size' attribute - must be a float, >2, and have exactly two decimal places
75- if zip_file ['size' ] <= 2 :
76- print ("The 'size' attribute is smaller then 2 MiB" )
77- new_input (data , file_path )
78- return
79-
80- # Validate 'md5' attribute - must be a valid MD5 hash (32 hexadecimal characters)
81- if not re .fullmatch (r"^[a-fA-F0-9]{32}$" , zip_file .get ('md5' , '' )):
82- print ("The 'md5' attribute is not a valid 32-character hexadecimal MD5 hash." )
83- new_input (data , file_path )
84- return
85-
86- # Validate 'url' attribute - must match one of the expected patterns
45+ errors = []
46+
47+ # Validate name
48+ name = zip_file .get ('name' )
49+ if not name :
50+ errors .append ("The 'name' attribute is missing." )
51+ elif not name .endswith (".zip" ) or "$" in name :
52+ errors .append (f"The file '{ name } ' is not a valid .zip file." )
53+
54+ # Validate size
55+ size = zip_file .get ('size' )
56+ if not isinstance (size , float ):
57+ errors .append ("The 'size' attribute is missing or not a float." )
58+ elif size <= 2 :
59+ errors .append ("The 'size' attribute is smaller than 2 MiB." )
60+
61+ # Validate md5
62+ md5 = zip_file .get ('md5' )
63+ if not md5 :
64+ errors .append ("The 'md5' attribute is missing." )
65+ elif not re .fullmatch (r"^[a-fA-F0-9]{32}$" , md5 ):
66+ errors .append ("The 'md5' attribute is not a valid 32-character hexadecimal MD5 hash." )
67+
68+ # Validate url
69+ url = zip_file .get ('url' )
8770 pattern1 = r"^https://trcustoms\.org/api/level_files/\d+/download$"
8871 pattern2 = r"^https://www\.trle\.net/levels/levels/\d{4}/\d{4}/[a-zA-Z0-9%-_\.$]+\.zip$"
89-
90- if not re .match (pattern1 , zip_file .get ('url' , '' )) \
91- and not re .match (pattern2 , zip_file .get ('url' , '' )):
92- print ("The 'url' attribute does not match any of the expected patterns." )
72+ if not url :
73+ errors .append ("The 'url' attribute is missing." )
74+ elif not re .match (pattern1 , url ) and not re .match (pattern2 , url ):
75+ errors .append ("The 'url' attribute does not match any of the expected patterns." )
76+
77+ # Handle errors
78+ if errors :
79+ for error in errors :
80+ print (error )
9381 new_input (data , file_path )
94- return
9582
9683
9784def safe_string_to_int (id_str ):
0 commit comments