Update tools/upload.py

#16
Files changed (1) hide show
  1. tools/upload.py +23 -6
tools/upload.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
 
3
  UPLOAD_DIR = "uploads"
4
  os.makedirs(UPLOAD_DIR, exist_ok=True)
@@ -6,16 +7,32 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
6
 
7
  def save_uploads(files):
8
  """
9
- Save multiple uploaded files from Streamlit
 
 
 
 
 
 
 
 
 
 
10
  """
11
- saved_paths = []
 
12
 
13
  for file in files:
14
- path = os.path.join(UPLOAD_DIR, file.name)
15
 
16
- with open(path, "wb") as f:
17
  f.write(file.getbuffer())
18
 
19
- saved_paths.append(path)
 
 
 
 
 
20
 
21
- return saved_paths
 
1
  import os
2
+ import mimetypes
3
 
4
  UPLOAD_DIR = "uploads"
5
  os.makedirs(UPLOAD_DIR, exist_ok=True)
 
7
 
8
  def save_uploads(files):
9
  """
10
+ Save uploaded files and return metadata.
11
+
12
+ Returns:
13
+ [
14
+ {
15
+ "name": "...",
16
+ "path": "...",
17
+ "type": "...",
18
+ "size": ...
19
+ }
20
+ ]
21
  """
22
+
23
+ uploaded_files = []
24
 
25
  for file in files:
26
+ filepath = os.path.join(UPLOAD_DIR, file.name)
27
 
28
+ with open(filepath, "wb") as f:
29
  f.write(file.getbuffer())
30
 
31
+ uploaded_files.append({
32
+ "name": file.name,
33
+ "path": filepath,
34
+ "type": mimetypes.guess_type(filepath)[0],
35
+ "size": os.path.getsize(filepath)
36
+ })
37
 
38
+ return uploaded_files