Skip to content

Commit

Permalink
feature: Add mime_type field to ByteStream (#6154)
Browse files Browse the repository at this point in the history
* Add mime_type field to ByteStream

* Add release notes

* Update tests
  • Loading branch information
silvanocerza authored Oct 23, 2023
1 parent dcc7e63 commit 31fb5b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions haystack/preview/dataclasses/byte_stream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Any
from typing import Optional, Dict, Any


@dataclass(frozen=True)
Expand All @@ -11,27 +11,28 @@ class ByteStream:

data: bytes
metadata: Dict[str, Any] = field(default_factory=dict, hash=False)
mime_type: Optional[str] = field(default=None)

def to_file(self, destination_path: Path):
with open(destination_path, "wb") as fd:
fd.write(self.data)

@classmethod
def from_file_path(cls, filepath: Path) -> "ByteStream":
def from_file_path(cls, filepath: Path, mime_type: Optional[str] = None) -> "ByteStream":
"""
Create a ByteStream from the contents read from a file.
:param filepath: A valid path to a file.
"""
with open(filepath, "rb") as fd:
return cls(data=fd.read())
return cls(data=fd.read(), mime_type=mime_type)

@classmethod
def from_string(cls, text: str, encoding: str = "utf-8") -> "ByteStream":
def from_string(cls, text: str, encoding: str = "utf-8", mime_type: Optional[str] = None) -> "ByteStream":
"""
Create a ByteStream encoding a string.
:param text: The string to encode
:param encoding: The encoding used to convert the string into bytes
"""
return cls(data=text.encode(encoding))
return cls(data=text.encode(encoding), mime_type=mime_type)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
preview:
- |
Add `mime_type` field to `ByteStream` dataclass.
10 changes: 10 additions & 0 deletions test/preview/dataclasses/test_byte_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ def test_from_file_path(tmp_path, request):

b = ByteStream.from_file_path(test_path)
assert b.data == test_bytes
assert b.mime_type == None

b = ByteStream.from_file_path(test_path, mime_type="text/plain")
assert b.data == test_bytes
assert b.mime_type == "text/plain"


@pytest.mark.unit
def test_from_string():
test_string = "Hello, world!"
b = ByteStream.from_string(test_string)
assert b.data.decode() == test_string
assert b.mime_type == None

b = ByteStream.from_string(test_string, mime_type="text/plain")
assert b.data.decode() == test_string
assert b.mime_type == "text/plain"


@pytest.mark.unit
Expand Down

0 comments on commit 31fb5b8

Please sign in to comment.