Skip to content

Commit

Permalink
add support to upload thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Aug 30, 2024
1 parent a950078 commit 440641c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.7.2

- Support to assign thumbnail file in deploy

## 2.7.1

- Support for subfolders in `hal9` deployment
Expand Down
Binary file added apps/flux/thumbnail.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/hal9/thumbnail.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 31 additions & 3 deletions python/hal9/targets/hal9.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
import time
import base64
import json
import base64
import mimetypes

def file_to_dataurl(file_path):
if not os.path.exists(file_path):
return None
mime_type, _ = mimetypes.guess_type(file_path)
if mime_type not in ['image/jpeg', 'image/png']:
raise ValueError("Unsupported file type. Only JPEG and PNG are allowed.")

# Read the file content in binary mode
with open(file_path, 'rb') as file:
file_data = file.read()
base64_data = base64.b64encode(file_data).decode('utf-8')
data_url = f"data:{mime_type};base64,{base64_data}"
return data_url

def complete_filename(directory, filename):
for file in os.listdir(directory):
if file.startswith(filename):
return os.path.abspath(os.path.join(directory, file))
return None

def project_from_path(path :str) -> str:
return os.path.basename(os.path.abspath(path))
Expand All @@ -22,10 +44,13 @@ def create_deployment(path :str) -> str:

return zip_path

def read_files(path):
def read_files(path :str, exclude :str = None):
files_dict = {}
for root, dirs, files in os.walk(path):
for file in files:
if exclude and file.startswith(exclude):
continue

relative_path = os.path.relpath(os.path.join(root, file), path)
with open(os.path.join(root, file), 'rb') as f:
encoded_content = base64.b64encode(f.read()).decode('utf-8')
Expand All @@ -34,15 +59,18 @@ def read_files(path):
return files_dict

def request_deploy(path :str, url :str, name :str, typename :str, data :str, access :str, main :str) -> str:
thumbnail = file_to_dataurl(complete_filename(path, "thumbnail."))

payload = {
'filename': main,
'type': typename,
'name': name,
'format': 'b64',
'files': read_files(path),
'files': read_files(path, "thumbnail."),
'schemapath': data,
'access': access,
'sourcefile': main
'sourcefile': main,
'thumbnail': thumbnail
}

headers = {
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.7.1"
version = "2.7.2"
description = ""
authors = ["Javier Luraschi <javier@hal9.ai>"]
readme = "README.md"
Expand Down

0 comments on commit 440641c

Please sign in to comment.