refactor: replace imghdr with filetype (#20680)
* refactor: replace `imaghdr` with `filetype` ``` 11:52:06 worker.1 | /home/ankush/benches/develop/apps/frappe/frappe/core/doctype/file/utils.py:2: DeprecationWarning: 'imghdr' is deprecated and slated for removal in Python 3.13 ``` * feat: improved extension guessing using filecontent
This commit is contained in:
parent
f46d1aefa9
commit
0cab0b830d
5 changed files with 17 additions and 6 deletions
|
|
@ -17,6 +17,7 @@ from frappe.core.api.file import (
|
|||
move_file,
|
||||
unzip_file,
|
||||
)
|
||||
from frappe.core.doctype.file.utils import get_extension
|
||||
from frappe.exceptions import ValidationError
|
||||
from frappe.tests.utils import FrappeTestCase
|
||||
from frappe.utils import get_files_path
|
||||
|
|
@ -739,3 +740,10 @@ class TestFileOptimization(FrappeTestCase):
|
|||
size_after_rollback = os.stat(image_path).st_size
|
||||
|
||||
self.assertEqual(size_before_optimization, size_after_rollback)
|
||||
|
||||
def test_image_header_guessing(self):
|
||||
file_path = frappe.get_app_path("frappe", "tests/data/sample_image_for_optimization.jpg")
|
||||
with open(file_path, "rb") as f:
|
||||
file_content = f.read()
|
||||
|
||||
self.assertEqual(get_extension("", None, file_content), "jpeg")
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import hashlib
|
||||
import imghdr
|
||||
import mimetypes
|
||||
import os
|
||||
import re
|
||||
|
|
@ -7,6 +6,7 @@ from io import BytesIO
|
|||
from typing import TYPE_CHECKING, Optional
|
||||
from urllib.parse import unquote
|
||||
|
||||
import filetype
|
||||
import requests
|
||||
import requests.exceptions
|
||||
from PIL import Image
|
||||
|
|
@ -76,9 +76,11 @@ def get_extension(
|
|||
|
||||
mimetype = mimetypes.guess_type(filename + "." + extn)[0]
|
||||
|
||||
if mimetype is None or not mimetype.startswith("image/") and content:
|
||||
# detect file extension by reading image header properties
|
||||
extn = imghdr.what(filename + "." + (extn or ""), h=content)
|
||||
if mimetype is None and extn is None and content:
|
||||
# detect file extension by using filetype matchers
|
||||
_type_info = filetype.match(content)
|
||||
if _type_info:
|
||||
extn = _type_info.extension
|
||||
|
||||
return extn
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import datetime
|
|||
import hashlib
|
||||
import re
|
||||
from http import cookies
|
||||
from urllib.parse import unquote, urlparse, urljoin
|
||||
from urllib.parse import unquote, urljoin, urlparse
|
||||
|
||||
import jwt
|
||||
import pytz
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ class Workflow(Document):
|
|||
|
||||
@frappe.whitelist()
|
||||
def get_workflow_state_count(doctype, workflow_state_field, states):
|
||||
frappe.has_permission(doctype=doctype, ptype='read', throw=True)
|
||||
frappe.has_permission(doctype=doctype, ptype="read", throw=True)
|
||||
states = frappe.parse_json(states)
|
||||
result = frappe.get_all(
|
||||
doctype,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ dependencies = [
|
|||
"Babel~=2.12.1",
|
||||
"Click~=8.1.3",
|
||||
"filelock~=3.8.0",
|
||||
"filetype~=1.2.0",
|
||||
"GitPython~=3.1.30",
|
||||
"Jinja2~=3.1.2",
|
||||
"Pillow~=9.3.0",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue