Skip to content

Commit

Permalink
Add util for obtaining file mimetype
Browse files Browse the repository at this point in the history
Summary: as titled

Reviewed By: SimonWan

Differential Revision: D64142934

fbshipit-source-id: 75a882924d8cb4e96acbdbaeb483fda329df728d
  • Loading branch information
onionymous authored and facebook-github-bot committed Oct 11, 2024
1 parent 79b3234 commit b85bf58
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CybersecurityBenchmarks/benchmark/benchmark_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import base64
import io
import subprocess

from typing import Optional

from PIL import Image

Expand Down Expand Up @@ -62,3 +65,13 @@ def image_to_b64(image_path: str, image_format: str = "PNG") -> str:
image.save(buffer, format=image_format)
img_base64 = base64.b64encode(buffer.getvalue())
return img_base64.decode("utf-8")


def get_mime_type(file_path: str) -> Optional[str]:
result = subprocess.run(
["file", "--mime-type", "-b", file_path], capture_output=True, text=True
)
if result.returncode == 0:
return result.stdout.strip()
else:
return None

0 comments on commit b85bf58

Please sign in to comment.