Skip to content

Commit

Permalink
feat(ClientFile): Add helper to handle multi-part upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Aug 29, 2022
1 parent 7073f5a commit d00907f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
11 changes: 8 additions & 3 deletions examples/00_howdoi/upload.py
Expand Up @@ -4,6 +4,7 @@
"""

from trame.app import get_server
from trame.app.file_upload import ClientFile
from trame.ui.vuetify import VAppLayout
from trame.widgets import vuetify

Expand All @@ -21,13 +22,17 @@ def file_uploaded(file_exchange, **kwargs):
if file_exchange is None:
return

file = ClientFile(file_exchange)
file_name = file_exchange.get("name")
file_size = file_exchange.get("size")
file_time = file_exchange.get("lastModified")
file_mime_type = file_exchange.get("type")
file_binary_content = file_exchange.get("content")
print(f"Got file {file_name} of size {file_size}")
print(file_binary_content)
file_binary_content = file_exchange.get(
"content"
) # can be either list(bytes, ...), or bytes
print(file.info)
print(type(file.content)) # will always be bytes
# print(file_binary_content)


if __name__ == "__main__":
Expand Down
49 changes: 49 additions & 0 deletions trame/app/file_upload.py
@@ -0,0 +1,49 @@
class ClientFile:
def __init__(self, file_state_variable=None):
self._name = None
self._size = None
self._time = None
self._mime_type = None
self._content = None
if file_state_variable is not None:
self._name = file_state_variable.get("name")
self._size = file_state_variable.get("size")
self._time = file_state_variable.get("lastModified")
self._mime_type = file_state_variable.get("type")
self._content = file_state_variable.get("content")
if isinstance(self._content, list):
self._content = b"".join(self._content)

@property
def is_empty(self):
"""Return true if the file is empty"""
return self._content is None

@property
def name(self):
"""File name"""
return self._name

@property
def size(self):
"""File size in Bytes"""
return self._size

@property
def modified_time(self):
"""Modified time"""
return self._time

@property
def mime_type(self):
"""Mime type of the file"""
return self._mime_type

@property
def content(self):
"""Bytes content of the file"""
return self._content

@property
def info(self):
return f"File: {self.name} of size {self.size} and type {self.mime_type}"

0 comments on commit d00907f

Please sign in to comment.