Skip to content

Commit 8c8f3f2

Browse files
committed
guess_content_type_from_bytes method in helpers module
1 parent a407b39 commit 8c8f3f2

File tree

11 files changed

+107
-1
lines changed

11 files changed

+107
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
### Added
55
- `is_binary` method in `helpers` module, by @HardNorth
6+
- `guess_content_type_from_bytes` method in `helpers` module, by @HardNorth
67

78
## [5.5.4]
89
### Added

reportportal_client/helpers.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,59 @@ def is_binary(iterable: Union[bytes, bytearray, str]) -> bool:
407407
if 0x00 in byte_iterable:
408408
return True
409409
return False
410+
411+
412+
def guess_content_type_from_bytes(data: Union[bytes, bytearray, List[int]]) -> str:
413+
"""Guess content type from bytes.
414+
415+
:param data: bytes or bytearray
416+
:return: content type
417+
"""
418+
my_data = data
419+
if isinstance(data, list):
420+
my_data = bytes(my_data)
421+
422+
if not is_binary(my_data):
423+
return 'text/plain'
424+
425+
# images
426+
if my_data.startswith(b'\xff\xd8\xff'):
427+
return 'image/jpeg'
428+
if my_data.startswith(b'\x89PNG\r\n\x1a\n'):
429+
return 'image/png'
430+
if my_data.startswith(b'GIF8'):
431+
return 'image/gif'
432+
if my_data.startswith(b'BM'):
433+
return 'image/bmp'
434+
if my_data.startswith(b'\x00\x00\x01\x00'):
435+
return 'image/vnd.microsoft.icon'
436+
if my_data.startswith(b'RIFF') and b'WEBP' in my_data:
437+
return 'image/webp'
438+
439+
# audio
440+
if my_data.startswith(b'ID3'):
441+
return 'audio/mpeg'
442+
if my_data.startswith(b'RIFF') and b'WAVE' in my_data:
443+
return 'audio/wav'
444+
445+
# video
446+
if my_data.startswith(b'\x00\x00\x01\xba'):
447+
return 'video/mpeg'
448+
if my_data.startswith(b'RIFF') and b'AVI LIST' in my_data:
449+
return 'video/avi'
450+
if my_data.startswith(b'\x1aE\xdf\xa3'):
451+
return 'video/webm'
452+
453+
# archives
454+
if my_data.startswith(b'PK\x03\x04'):
455+
if my_data.startswith(b'PK\x03\x04\x14\x00\x08'):
456+
return 'application/java-archive'
457+
return 'application/zip'
458+
if my_data.startswith(b'PK\x05\x06'):
459+
return 'application/zip'
460+
461+
# office
462+
if my_data.startswith(b'%PDF'):
463+
return 'application/pdf'
464+
465+
return 'application/octet-stream'

test_res/files/demo.zip

126 Bytes
Binary file not shown.

test_res/files/image.png

19.2 KB
Loading

test_res/files/simple.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
HTTP/1.1 407 Proxy Authentication Required
2+
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
3+
Expires: 0
4+
Pragma: no-cache
5+
X-Content-Type-Options: nosniff
6+
X-Frame-Options: DENY
7+
X-Xss-Protection: 1; mode=block
8+
Content-Length: 0
9+

test_res/files/test.bin

8 Bytes
Binary file not shown.

test_res/files/test.jar

779 Bytes
Binary file not shown.

test_res/files/test.pdf

424 KB
Binary file not shown.

test_res/pug/lucky.jpg

99 KB
Loading

test_res/pug/unlucky.jpg

88.3 KB
Loading

0 commit comments

Comments
 (0)