|
35 | 35 | ) |
36 | 36 |
|
37 | 37 | __author__ = "Chris Griffith" |
38 | | -__version__ = "2.1.0" |
| 38 | +__version__ = "2.1.1" |
39 | 39 | __all__ = [ |
40 | 40 | "magic_file", |
41 | 41 | "magic_string", |
@@ -212,13 +212,12 @@ def perform_magic(header: bytes, footer: bytes, mime: bool, ext=None, filename=N |
212 | 212 | raise PureValueError("Input was empty") |
213 | 213 | infos = identify_all(header, footer, ext) |
214 | 214 | if filename and os.path.isfile(filename) and os.getenv("PUREMAGIC_DEEPSCAN") != "0": |
215 | | - results = run_deep_scan(infos, filename, header, footer, raise_on_none=True) |
216 | | - if results: |
217 | | - if results[0].extension == "": |
218 | | - raise PureError("Could not identify file") |
| 215 | + results = run_deep_scan(infos, filename, header, footer, raise_on_none=not infos) |
| 216 | + if results and results[0].extension != "": |
219 | 217 | if mime: |
220 | 218 | return results[0].mime_type |
221 | 219 | return results[0].extension or "" |
| 220 | + # Deep scan returned empty extension or no results — fall through to original matches |
222 | 221 | if not infos: |
223 | 222 | raise PureError("Could not identify file") |
224 | 223 | info = infos[0] |
@@ -556,13 +555,18 @@ def run_deep_scan( |
556 | 555 |
|
557 | 556 | # No specific scanner matched — try the catch-all text scanner |
558 | 557 | # Only override when existing matches are very low confidence (e.g. 2-byte BOM signatures) |
559 | | - if matches[0].confidence < 0.5: |
| 558 | + # Only let the catch-all text scanner override when existing matches are |
| 559 | + # generic text types (e.g. BOM-only signatures). If the magic database |
| 560 | + # already identified a specific non-text file type, trust it over a generic text guess. |
| 561 | + best_mime = matches[0].mime_type or "" |
| 562 | + is_generic = best_mime.startswith("text/") or best_mime == "application/octet-stream" or not best_mime |
| 563 | + if matches[0].confidence < 0.5 and is_generic: |
560 | 564 | try: |
561 | 565 | result = catch_all_deep_scan(filename, head, foot) |
562 | 566 | except Exception: |
563 | 567 | pass |
564 | 568 | else: |
565 | | - if result and result.confidence > matches[0].confidence: |
| 569 | + if result and result.extension and result.confidence > matches[0].confidence: |
566 | 570 | return [ |
567 | 571 | PureMagicWithConfidence( |
568 | 572 | confidence=result.confidence, |
|
0 commit comments