| 132 | ) |
| 133 | |
| 134 | def handle_audio(self, file_size, file_bytes, content_type, encoded_filename): |
| 135 | |
| 136 | # 解析range请求 (格式如 "bytes=0-1023") |
| 137 | range_match = re.match(r'bytes=(\d+)-(\d*)', self.data.get('http_range', '')) |
| 138 | if range_match: |
| 139 | start = int(range_match.group(1)) |
| 140 | end = int(range_match.group(2)) if range_match.group(2) else file_size - 1 |
| 141 | |
| 142 | # 确保范围合法 |
| 143 | end = min(end, file_size - 1) |
| 144 | length = end - start + 1 |
| 145 | |
| 146 | # 创建部分响应 |
| 147 | response = HttpResponse( |
| 148 | file_bytes[start:start + length], |
| 149 | status=206, |
| 150 | content_type=content_type |
| 151 | ) |
| 152 | |
| 153 | # 设置部分内容响应头 |
| 154 | response['Content-Range'] = f'bytes {start}-{end}/{file_size}' |
| 155 | response['Accept-Ranges'] = 'bytes' |
| 156 | response['Content-Length'] = str(length) |
| 157 | response['Content-Disposition'] = f'inline; filename={encoded_filename}' |
| 158 | return response |
| 159 | |
| 160 | def delete(self): |
| 161 | self.is_valid(raise_exception=True) |