lexlepty commited on
Commit
6f0ada9
·
verified ·
1 Parent(s): 22c1716

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -124,7 +124,6 @@ def download_file(filepath):
124
  try:
125
  url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
126
 
127
- # 获取文件基本信息
128
  response = requests.get(
129
  url,
130
  headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
@@ -135,12 +134,16 @@ def download_file(filepath):
135
  filename = os.path.basename(filepath)
136
  encoded_filename = urllib.parse.quote(filename.encode('utf-8'))
137
 
138
- # 直接流式传输文件内容
 
 
 
 
139
  return Response(
140
  response.iter_content(chunk_size=1048576),
141
  headers={
142
  'Content-Disposition': f'attachment; filename*=UTF-8\'\'{encoded_filename}',
143
- 'Content-Type': response.headers.get('content-type', 'application/octet-stream'),
144
  'Content-Length': response.headers.get('content-length'),
145
  'Accept-Ranges': 'bytes'
146
  }
@@ -149,7 +152,10 @@ def download_file(filepath):
149
  return jsonify({'error': 'File not found'}), 404
150
 
151
  except Exception as e:
 
 
152
  return jsonify({'error': str(e)}), 500
 
153
  @app.route('/api/files/upload', methods=['POST'])
154
  @require_auth
155
  def upload_file():
 
124
  try:
125
  url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
126
 
 
127
  response = requests.get(
128
  url,
129
  headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
 
134
  filename = os.path.basename(filepath)
135
  encoded_filename = urllib.parse.quote(filename.encode('utf-8'))
136
 
137
+ # Explicit content type handling for TXT files
138
+ content_type = response.headers.get('content-type')
139
+ if filename.lower().endswith('.txt'):
140
+ content_type = 'text/plain; charset=utf-8'
141
+
142
  return Response(
143
  response.iter_content(chunk_size=1048576),
144
  headers={
145
  'Content-Disposition': f'attachment; filename*=UTF-8\'\'{encoded_filename}',
146
+ 'Content-Type': content_type or 'application/octet-stream',
147
  'Content-Length': response.headers.get('content-length'),
148
  'Accept-Ranges': 'bytes'
149
  }
 
152
  return jsonify({'error': 'File not found'}), 404
153
 
154
  except Exception as e:
155
+ # Add logging for better error tracking
156
+ print(f"Download error for {filepath}: {str(e)}")
157
  return jsonify({'error': str(e)}), 500
158
+
159
  @app.route('/api/files/upload', methods=['POST'])
160
  @require_auth
161
  def upload_file():