def captcha_audio(request, key): if settings.CAPTCHA_FLITE_PATH: try: store = CaptchaStore.objects.get(hashkey=key) except CaptchaStore.DoesNotExist: # HTTP 410 Gone status so that crawlers don't index these expired urls. return HttpResponse(status=410) text = store.challenge if "captcha.helpers.math_challenge" == settings.CAPTCHA_CHALLENGE_FUNCT: text = text.replace("*", "times").replace("-", "minus").replace("+", "plus") else: text = ", ".join(list(text)) path = str(os.path.join(tempfile.gettempdir(), "%s.wav" % key)) subprocess.call([settings.CAPTCHA_FLITE_PATH, "-t", text, "-o", path]) # Add arbitrary noise if sox is installed if settings.CAPTCHA_SOX_PATH: arbnoisepath = str( os.path.join(tempfile.gettempdir(), "%s_arbitrary.wav") % key) mergedpath = str( os.path.join(tempfile.gettempdir(), "%s_merged.wav") % key) subprocess.call([ settings.CAPTCHA_SOX_PATH, "-r", "8000", "-n", arbnoisepath, "synth", "2", "brownnoise", "gain", "-15", ]) subprocess.call([ settings.CAPTCHA_SOX_PATH, "-m", arbnoisepath, path, "-t", "wavpcm", "-b", "16", mergedpath, ]) os.remove(arbnoisepath) os.remove(path) os.rename(mergedpath, path) if os.path.isfile(path): response = RangedFileResponse(request, open(path, "rb"), content_type="audio/wav") response[ "Content-Disposition"] = 'attachment; filename="{}.wav"'.format( key) return response raise Http404
def 傳音檔(request, 音檔編號, 開始時間, 結束時間): 全部音檔 = 句表.objects.get(id=音檔編號).聲音檔() 語句音檔 = 全部音檔.照秒數切出音檔(float(開始時間), float(結束時間)) try: with TemporaryDirectory() as 資料夾: 檔名 = join(資料夾, 'audio.wav') with open(檔名, 'wb') as 檔案: 指令 = Popen( [ 'sox', '-', '-b', '24', 檔名, 'remix', '1', # 'silence', '1', '0.01', '-40db', 'reverse', # 'silence', '1', '0.01', '-40db', 'reverse', ], stdin=PIPE) 指令.communicate(input=語句音檔.wav格式資料()) 程式腳本._走指令(['normalize-audio', 檔名]) with open(檔名, 'rb') as 檔案: 資料 = 檔案.read() except RuntimeError: 資料 = 語句音檔.wav格式資料() return RangedFileResponse(request, io.BytesIO(資料), content_type="audio/wav")
def captcha_audio(request, key): if settings.CAPTCHA_FLITE_PATH: try: store = CaptchaStore.objects.get(hashkey=key) except CaptchaStore.DoesNotExist: # HTTP 410 Gone status so that crawlers don't index these expired urls. return HttpResponse(status=410) text = store.challenge if 'captcha.helpers.math_challenge' == settings.CAPTCHA_CHALLENGE_FUNCT: text = text.replace('*', 'times').replace('-', 'minus').replace('+', 'plus') else: text = ', '.join(list(text)) path = str(os.path.join(tempfile.gettempdir(), '%s.wav' % key)) subprocess.call([settings.CAPTCHA_FLITE_PATH, "-t", text, "-o", path]) # Add arbitrary noise if sox is installed if settings.CAPTCHA_SOX_PATH: arbnoisepath = str(os.path.join(tempfile.gettempdir(), '%s_arbitrary.wav') % key) mergedpath = str(os.path.join(tempfile.gettempdir(), '%s_merged.wav') % key) subprocess.call([settings.CAPTCHA_SOX_PATH, '-r', '8000', '-n', arbnoisepath, 'synth', '2', 'brownnoise', 'gain', '-15']) subprocess.call([settings.CAPTCHA_SOX_PATH, '-m', arbnoisepath, path, '-t', 'wavpcm', '-b', '16', mergedpath]) os.remove(arbnoisepath) os.remove(path) os.rename(mergedpath, path) if os.path.isfile(path): response = RangedFileResponse(request, open(path, 'rb'), content_type='audio/wav') response['Content-Disposition'] = 'attachment; filename="{}.wav"'.format(key) return response raise Http404
def question_download(request, ID): path = os.path.join(MEDIA_ROOT, 'question') path = os.path.join(path, str(ID)) output_path = os.path.join(MEDIA_ROOT, 'download') file_name = str(ID) + '.zip' if os.path.exists(path): zip_file_path(path, output_path, file_name) file_path = os.path.join(output_path, file_name) """ def file_itertor(file_path, chunk_size=512): with open(file_path, 'rb') as f: while True: c = f.read(chunk_size) if c: yield c else: break response = StreamingHttpResponse(file_itertor(file_path)) response['Content-Type'] = 'application/octet-stream' response['Content-Length'] = os.path.getsize(file_path) response['Content-Disposition'] = 'attachment;filename="{0}"'.format(file_name) return response """ response = RangedFileResponse(request, open(file_path, 'rb'), content_type='audio/wav') response['Content-Disposition'] = 'attachment;filename="{0}"'.format( file_name) return response else: return HttpResponse("bad request")
def test_more_one_char(self): request = self.factory.get( '/path', HTTP_RANGE='bytes=10-17' ) 回應 = RangedFileResponse( request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav' ) self.assertContent(回應, b'tsiang5')
def test_failing(self): request = self.factory.get( '/path', HTTP_RANGE='bytes=17-20' ) 回應 = RangedFileResponse( request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav' ) self.assertEqual(回應.status_code, 416)
def video_stream(request, video_file): _file = "music/" + video_file if not os.path.isfile(_file): return HttpResponseNotFound() response = RangedFileResponse( request, open(_file, 'rb'), content_type=mimetypes.guess_type(_file)[0] ) response['Content-Length'] = os.path.getsize(_file) return response
def serve_media(request, path): _file = settings.MEDIA_ROOT + path if not os.path.isfile(_file): return HttpResponseNotFound() response = RangedFileResponse(request, open(_file, 'rb'), content_type=mimetypes.guess_type(_file)[0]) response['Content-Length'] = os.path.getsize(_file) return response
def video_download(request, video_file): _file = settings.DOWNLOAD_PATH + video_file if not os.path.isfile(_file): return HttpResponseNotFound() response = RangedFileResponse(request, open(_file, 'rb'), content_type=mimetypes.guess_type(_file)[0]) response['Content-Length'] = os.path.getsize(_file) response['Content-Disposition'] = 'attachment; filename="%s"' % video_file return response
def serve_private_file(request, path): """ Simple example of a view to serve private files with xsendfile" :param request: :param path: :return: """ if has_read_permission(request, path): fileRes = serve(request, path, settings.MEDIA_ROOT, False) response = RangedFileResponse(request, fileRes.file_to_stream, fileRes._headers['content-type'][1]) return response else: return HttpResponseForbidden()
def get_response(self, request=None, name=None, add_extension=False): if not request: response = HttpResponseRedirect(redirect_to=self.file.url) else: response = RangedFileResponse( request, self.file.file, content_type='application/octet-stream') if not name: name = posixpath.basename(self.file.name) if add_extension and "." not in name: # use ending of saved file ext = self.file.name.rsplit(".", 1) if len(ext) > 1: name = "%s.%s" % (name, ext[1]) # name is sanitized to not contain \n, and other ugly control chars response['Content-Disposition'] = \ 'attachment; filename="%s"' % posixpath.basename(name.replace( r'"', r'\"' )) return response
def 音檔包做回應(self, request, 音檔): 回應 = RangedFileResponse( request, io.BytesIO(音檔), content_type='audio/wav' ) 回應['Content-Disposition'] = 'filename="taiwanese.wav"' return 回應