16
16
MYSQL_USER ,
17
17
)
18
18
from app .models import FileScanRequest
19
-
19
+ from app . rabbitmq_sender import send_message
20
20
21
21
def load_yara_rules (directory ):
22
22
rule_files = []
@@ -74,7 +74,7 @@ def stream_file_from_s3(s3_key):
74
74
raise
75
75
76
76
77
- def save_scan_result (file_id , detect , detail ):
77
+ def save_scan_result (uploadId : int , stored_file_id , detect , detail ):
78
78
try :
79
79
conn = mysql .connector .connect (
80
80
host = MYSQL_HOST , user = MYSQL_USER , password = MYSQL_PASSWORD , database = MYSQL_DB
@@ -84,7 +84,7 @@ def save_scan_result(file_id, detect, detail):
84
84
try :
85
85
cursor .execute (
86
86
"INSERT INTO scan_table (file_id, detect, step2_detail) VALUES (%s, %s, %s)" ,
87
- (file_id , detect , detail ),
87
+ (stored_file_id , detect , detail ),
88
88
)
89
89
conn .commit () # 첫 번째 쿼리 커밋
90
90
except Exception as e :
@@ -94,9 +94,10 @@ def save_scan_result(file_id, detect, detail):
94
94
95
95
try :
96
96
cursor .execute (
97
- "UPDATE file_status SET gscan_status = 1 WHERE file_id = %s" , (file_id ,)
97
+ "UPDATE file_status SET gscan_status = 1 WHERE file_id = %s" , (stored_file_id ,)
98
98
)
99
99
conn .commit () # 두 번째 쿼리 커밋
100
+ send_message (uploadId ) # RabbitMQ 전송: Alerts
100
101
except Exception as e :
101
102
conn .rollback () # 두 번째 쿼리 롤백
102
103
logging .error (f"Failed to update file_status: { e } " )
@@ -147,21 +148,17 @@ def yara_test_match(file_path, yara_rules):
147
148
return detect , detail
148
149
149
150
150
- def scan_file (file_id : int , yara_rules ):
151
+ def scan_file (upload_id : int , yara_rules ):
151
152
try :
152
- conn = mysql .connector .connect (
153
- host = MYSQL_HOST , user = MYSQL_USER , password = MYSQL_PASSWORD , database = MYSQL_DB
154
- )
155
- cursor = conn .cursor (dictionary = True )
156
- cursor .execute ("SELECT * FROM stored_file WHERE id = %s" , (file_id ,))
157
- file_record = cursor .fetchone ()
158
- cursor .close ()
159
- conn .close ()
160
-
161
- if not file_record :
162
- raise HTTPException (status_code = 404 , detail = "File not found" )
163
-
164
- s3_key = file_record ["save_path" ]
153
+ # 파일 업로드 정보 가져오기
154
+ file_record = get_file_upload (upload_id )
155
+ salted_hash = file_record ["salted_hash" ]
156
+
157
+ # 저장된 파일 정보 가져오기
158
+ stored_file_record = get_stored_file (salted_hash )
159
+ stored_file_id = stored_file_record ["id" ]
160
+ s3_key = stored_file_record ["save_path" ]
161
+
165
162
file_stream = stream_file_from_s3 (s3_key )
166
163
167
164
# 파일 전체를 한 번에 읽음
@@ -182,7 +179,49 @@ def scan_file(file_id: int, yara_rules):
182
179
logging .info (f"detail: { detail } " )
183
180
logging .info (f"most_common_keyword: { most_common_keyword } " )
184
181
185
- save_scan_result (file_id , detect , most_common_keyword )
182
+ save_scan_result (upload_id , stored_file_id , detect , most_common_keyword )
186
183
except Exception as e :
187
184
logging .error (f"Error scanning file: { e } " )
188
185
raise HTTPException (status_code = 500 , detail = "Error scanning file" )
186
+
187
+
188
+ def get_stored_file (hash :str ):
189
+ try :
190
+ conn = mysql .connector .connect (
191
+ host = MYSQL_HOST , user = MYSQL_USER , password = MYSQL_PASSWORD , database = MYSQL_DB
192
+ )
193
+ cursor = conn .cursor (dictionary = True )
194
+ cursor .execute ("SELECT * FROM stored_file WHERE salted_hash = %s" , (hash ,))
195
+ stored_file_record = cursor .fetchone ()
196
+ cursor .close ()
197
+ conn .close ()
198
+
199
+ if not stored_file_record :
200
+ raise HTTPException (status_code = 404 , detail = "File not found in stored_file table" )
201
+
202
+ return stored_file_record
203
+
204
+ except Exception as e :
205
+ logging .error (f"Error fetching stored file record: { e } " )
206
+ raise HTTPException (status_code = 500 , detail = "Error fetching stored file record" )
207
+
208
+
209
+ def get_file_upload (file_id : int ):
210
+ try :
211
+ conn = mysql .connector .connect (
212
+ host = MYSQL_HOST , user = MYSQL_USER , password = MYSQL_PASSWORD , database = MYSQL_DB
213
+ )
214
+ cursor = conn .cursor (dictionary = True )
215
+ cursor .execute ("SELECT * FROM file_upload WHERE id = %s" , (file_id ,))
216
+ file_record = cursor .fetchone ()
217
+ cursor .close ()
218
+ conn .close ()
219
+
220
+ if not file_record :
221
+ raise HTTPException (status_code = 404 , detail = "File not found in file_upload table" )
222
+
223
+ return file_record
224
+
225
+ except Exception as e :
226
+ logging .error (f"Error fetching file upload record: { e } " )
227
+ raise HTTPException (status_code = 500 , detail = "Error fetching file upload record" )
0 commit comments