Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,7 +29,7 @@ if not HF_TOKEN:
|
|
| 29 |
hf_api = HfApi(token=HF_TOKEN)
|
| 30 |
logger.info(f"Public File Upload API Initialized. Target repo: {HF_REPO_ID}")
|
| 31 |
|
| 32 |
-
# --- توابع کمکی
|
| 33 |
def get_folder_by_mimetype(mimetype):
|
| 34 |
if not mimetype: return "others"
|
| 35 |
main_type = mimetype.split('/')[0]
|
|
@@ -64,7 +64,7 @@ def serve_html():
|
|
| 64 |
@app.route('/upload', methods=['POST'])
|
| 65 |
def upload_file_endpoint():
|
| 66 |
try:
|
| 67 |
-
# آپلود از کامپیوتر یا ربات (حالت FormData)
|
| 68 |
if 'file' in request.files:
|
| 69 |
uploaded_file = request.files['file']
|
| 70 |
if uploaded_file.filename == '':
|
|
@@ -72,8 +72,32 @@ def upload_file_endpoint():
|
|
| 72 |
file_content = uploaded_file.read()
|
| 73 |
direct_url = process_and_upload(file_content, uploaded_file.filename, uploaded_file.mimetype)
|
| 74 |
return jsonify({"message": "File uploaded successfully!", "hf_url": direct_url}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
else:
|
| 76 |
-
return jsonify({"error": "Invalid request. 'file'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
logger.error(f"An internal server error occurred: {e}", exc_info=True)
|
| 79 |
return jsonify({"error": f"An internal server error occurred: {str(e)}"}), 500
|
|
|
|
| 29 |
hf_api = HfApi(token=HF_TOKEN)
|
| 30 |
logger.info(f"Public File Upload API Initialized. Target repo: {HF_REPO_ID}")
|
| 31 |
|
| 32 |
+
# --- توابع کمکی ---
|
| 33 |
def get_folder_by_mimetype(mimetype):
|
| 34 |
if not mimetype: return "others"
|
| 35 |
main_type = mimetype.split('/')[0]
|
|
|
|
| 64 |
@app.route('/upload', methods=['POST'])
|
| 65 |
def upload_file_endpoint():
|
| 66 |
try:
|
| 67 |
+
# حالت اول: آپلود از کامپیوتر یا ربات (حالت FormData)
|
| 68 |
if 'file' in request.files:
|
| 69 |
uploaded_file = request.files['file']
|
| 70 |
if uploaded_file.filename == '':
|
|
|
|
| 72 |
file_content = uploaded_file.read()
|
| 73 |
direct_url = process_and_upload(file_content, uploaded_file.filename, uploaded_file.mimetype)
|
| 74 |
return jsonify({"message": "File uploaded successfully!", "hf_url": direct_url}), 200
|
| 75 |
+
|
| 76 |
+
# ***** این بخش دوباره اضافه شد *****
|
| 77 |
+
# حالت دوم: آپلود از طریق لینک در وب سایت (حالت JSON)
|
| 78 |
+
elif request.is_json and 'url' in request.json:
|
| 79 |
+
file_url = request.json['url']
|
| 80 |
+
if not file_url:
|
| 81 |
+
return jsonify({"error": "No URL provided"}), 400
|
| 82 |
+
|
| 83 |
+
logger.info(f"Downloading file from URL: {file_url}")
|
| 84 |
+
response = requests.get(file_url, stream=True, timeout=30)
|
| 85 |
+
response.raise_for_status()
|
| 86 |
+
|
| 87 |
+
file_content = response.content
|
| 88 |
+
mimetype = response.headers.get('Content-Type')
|
| 89 |
+
original_filename = file_url.split('/')[-1].split('?')[0]
|
| 90 |
+
|
| 91 |
+
direct_url = process_and_upload(file_content, original_filename, mimetype)
|
| 92 |
+
return jsonify({"message": "File uploaded successfully!", "hf_url": direct_url}), 200
|
| 93 |
+
# ***** پایان بخش اضافه شده *****
|
| 94 |
+
|
| 95 |
else:
|
| 96 |
+
return jsonify({"error": "Invalid request. Expected 'file' (form-data) or 'url' (json)."}), 400
|
| 97 |
+
|
| 98 |
+
except requests.exceptions.RequestException as e:
|
| 99 |
+
logger.error(f"Failed to download from URL: {e}", exc_info=True)
|
| 100 |
+
return jsonify({"error": f"Failed to download from URL: {str(e)}"}), 400
|
| 101 |
except Exception as e:
|
| 102 |
logger.error(f"An internal server error occurred: {e}", exc_info=True)
|
| 103 |
return jsonify({"error": f"An internal server error occurred: {str(e)}"}), 500
|