Spaces:
Sleeping
Sleeping
Update tools/upload.py
#16
by Muthuraja18 - opened
- 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
for file in files:
|
| 14 |
-
|
| 15 |
|
| 16 |
-
with open(
|
| 17 |
f.write(file.getbuffer())
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
return
|
|
|
|
| 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
|