Skip to content

Commit

Permalink
fix(api): add signature generation for image previews (langgenius#9893)
Browse files Browse the repository at this point in the history
  • Loading branch information
laipz8200 authored Oct 26, 2024
1 parent 1144732 commit dd3ac7a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions api/models/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,28 @@ def next_segment(self):
)

def get_sign_content(self):
pattern = r"/files/([a-f0-9\-]+)/file-preview"
signed_urls = []
text = self.content

# For data before v0.10.0
pattern = r"/files/([a-f0-9\-]+)/image-preview"
matches = re.finditer(pattern, text)
for match in matches:
upload_file_id = match.group(1)
nonce = os.urandom(16).hex()
timestamp = str(int(time.time()))
data_to_sign = f"image-preview|{upload_file_id}|{timestamp}|{nonce}"
secret_key = dify_config.SECRET_KEY.encode() if dify_config.SECRET_KEY else b""
sign = hmac.new(secret_key, data_to_sign.encode(), hashlib.sha256).digest()
encoded_sign = base64.urlsafe_b64encode(sign).decode()

params = f"timestamp={timestamp}&nonce={nonce}&sign={encoded_sign}"
signed_url = f"{match.group(0)}?{params}"
signed_urls.append((match.start(), match.end(), signed_url))

# For data after v0.10.0
pattern = r"/files/([a-f0-9\-]+)/file-preview"
matches = re.finditer(pattern, text)
signed_urls = []
for match in matches:
upload_file_id = match.group(1)
nonce = os.urandom(16).hex()
Expand Down

0 comments on commit dd3ac7a

Please sign in to comment.