Jelajahi Sumber

Database connection test passed.

wangxq 2 bulan lalu
induk
melakukan
e78d818c43
1 mengubah file dengan 62 tambahan dan 46 penghapusan
  1. 62 46
      app/api/meta_data/routes.py

+ 62 - 46
app/api/meta_data/routes.py

@@ -32,7 +32,7 @@ minio_client = Minio(
     Config.MINIO_HOST,
     access_key=Config.MINIO_USER,
     secret_key=Config.MINIO_PASSWORD,
-    secure=True
+    secure=Config.MINIO_SECURE
 )
 
 # 配置文件上传相关
@@ -336,81 +336,97 @@ def upload_file():
 # 文件下载显示
 @bp.route('/resource/display', methods=['POST'])
 def upload_file_display():
+    response = None
     try:
         object_name = request.json.get('objectName')
         if not object_name:
             return jsonify(failed("文件路径不能为空"))
             
         # 获取文件内容
-        try:
-            response = minio_client.get_object(bucket_name, object_name)
-            file_data = response.read()
-            
-            # 获取文件名
-            file_name = object_name.split('/')[-1]
-            
-            # 确定文件类型
-            file_extension = file_name.split('.')[-1].lower()
-            
-            # 为不同文件类型设置合适的MIME类型
-            mime_types = {
-                'pdf': 'application/pdf',
-                'doc': 'application/msword',
-                'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
-                'xls': 'application/vnd.ms-excel',
-                'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
-                'txt': 'text/plain',
-                'csv': 'text/csv'
-            }
-            
-            content_type = mime_types.get(file_extension, 'application/octet-stream')
-            
-            # 返回结果
-            return jsonify(success({
-                "filename": file_name,
-                "type": file_extension,
-                "contentType": content_type,
-                "size": len(file_data),
-                "url": f"/api/meta/resource/download?objectName={object_name}"
-            }))
-        finally:
-            response.close()
-            response.release_conn()
+        response = minio_client.get_object(bucket_name, object_name)
+        file_data = response.read()
+        
+        # 获取文件名
+        file_name = object_name.split('/')[-1]
+        
+        # 确定文件类型
+        file_extension = file_name.split('.')[-1].lower()
+        
+        # 为不同文件类型设置合适的MIME类型
+        mime_types = {
+            'pdf': 'application/pdf',
+            'doc': 'application/msword',
+            'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+            'xls': 'application/vnd.ms-excel',
+            'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+            'txt': 'text/plain',
+            'csv': 'text/csv'
+        }
+        
+        content_type = mime_types.get(file_extension, 'application/octet-stream')
+        
+        # 返回结果
+        return jsonify(success({
+            "filename": file_name,
+            "type": file_extension,
+            "contentType": content_type,
+            "size": len(file_data),
+            "url": f"/api/meta/resource/download?objectName={object_name}"
+        }))
+    except S3Error as e:
+        logger.error(f"MinIO操作失败: {str(e)}")
+        return jsonify(failed(f"文件访问失败: {str(e)}"))
     except Exception as e:
         logger.error(f"文件显示信息获取失败: {str(e)}")
         return jsonify(failed(str(e)))
+    finally:
+        if response:
+            response.close()
+            response.release_conn()
 
 # 文件下载接口
 @bp.route('/resource/download', methods=['GET'])
 def download_file():
+    response = None
     try:
         object_name = request.args.get('objectName')
         if not object_name:
             return jsonify(failed("文件路径不能为空"))
             
+        # URL解码,处理特殊字符
+        import urllib.parse
+        object_name = urllib.parse.unquote(object_name)
+        
+        # 记录下载请求信息,便于调试
+        logger.info(f"下载文件请求: {object_name}")
+        
         # 获取文件
-        response = minio_client.get_object(bucket_name, object_name)
-        file_data = response.read()
+        try:
+            response = minio_client.get_object(bucket_name, object_name)
+            file_data = response.read()
+        except S3Error as e:
+            logger.error(f"MinIO获取文件失败: {str(e)}")
+            return jsonify(failed(f"文件获取失败: {str(e)}"))
         
-        # 获取文件名
+        # 获取文件名,并处理特殊字符
         file_name = object_name.split('/')[-1]
         
-        # 创建临时文件
-        temp_path = os.path.join(UPLOAD_FOLDER, file_name)
-        with open(temp_path, 'wb') as f:
-            f.write(file_data)
-            
+        # 直接从内存返回文件,不创建临时文件
+        import io
+        file_stream = io.BytesIO(file_data)
+        
         # 返回文件
         return send_file(
-            temp_path,
+            file_stream,
             as_attachment=True,
-            download_name=file_name
+            download_name=file_name,
+            mimetype="application/octet-stream"
         )
     except Exception as e:
         logger.error(f"文件下载失败: {str(e)}")
         return jsonify(failed(str(e)))
     finally:
-        if 'response' in locals():
+        if response:
             response.close()
             response.release_conn()