(self, with_valid=True)
| 101 | ) |
| 102 | |
| 103 | def get(self, with_valid=True): |
| 104 | if with_valid: |
| 105 | self.is_valid(raise_exception=True) |
| 106 | file_id = self.data.get('id') |
| 107 | file = QuerySet(File).filter(id=file_id).first() |
| 108 | if file is None: |
| 109 | raise NotFound404(404, _('File not found')) |
| 110 | file_type = file.file_name.split(".")[-1].lower() |
| 111 | content_type = mime_types.get(file_type, 'application/octet-stream') |
| 112 | encoded_filename = urllib.parse.quote(file.file_name) |
| 113 | # 获取文件内容 |
| 114 | file_bytes = file.get_bytes() |
| 115 | file_size = len(file_bytes) |
| 116 | |
| 117 | response = None |
| 118 | if file_type in audio_types and self.data.get('http_range'): |
| 119 | response = self.handle_audio(file_size, file_bytes, content_type, encoded_filename) |
| 120 | if response: |
| 121 | return response |
| 122 | |
| 123 | # 对于非范围请求或其他类型文件,返回完整内容 |
| 124 | headers = { |
| 125 | 'Content-Type': content_type, |
| 126 | 'Content-Disposition': f'attachment; filename={encoded_filename}' |
| 127 | } |
| 128 | return HttpResponse( |
| 129 | file_bytes, |
| 130 | status=200, |
| 131 | headers=headers |
| 132 | ) |
| 133 | |
| 134 | def handle_audio(self, file_size, file_bytes, content_type, encoded_filename): |
| 135 |
no test coverage detected