def page_thumbnail(request, domain, id): dimensions = request.GET.get('dimensions') page = get_object_or_404(Page, domain=domain, id=id) screenshot = Screenshot.get(page) if screenshot: if dimensions: thumb = get_thumbnail( screenshot.file, dimensions, # crop='center', crop='top', quality=80, ) r = http.HttpResponse(content_type='image/png') r.write(thumb.read()) return r else: return http.FileResponse(screenshot.file, content_type='image/png') else: cache_key = 'generating_screenshot_{}'.format(page.id) if not cache.get(cache_key): cache.set(cache_key, str(time.time()), 60) generate_screenshot.delay(page.id) return http.FileResponse( open('static/collected/images/image.png', 'rb'), content_type='image/png' )
def download_file_stream(filepath, temp_dir=None): """ Returns `filepath` as a HttpResponse stream. Deletes temp_dir once stream created if it exists. """ # If not found, return 404 if not os.path.exists(filepath): return http.HttpResponseNotFound(_("File not found")) filename = os.path.basename(filepath) # Open file in binary mode response = http.FileResponse(open(filepath, 'rb')) mimetype = mimetypes.guess_type(filename)[0] response['Content-type'] = mimetype response['Content-Disposition'] = 'attachment; filename="' + filename + '"' response['Content-Length'] = os.path.getsize(filepath) # Delete temp dir if created if temp_dir and os.path.exists(temp_dir): shutil.rmtree(temp_dir) return response
def get(self, request, *args, **kwargs): """ Provides the CA certificate """ verify_secure(request) form = kwargs.get('form', 'pem').lower() # Get MD5 value from request if available req_md5 = None if 'HTTP_X_MD5' in request.META: req_md5 = request.META['HTTP_X_MD5'] # Calculate MD5 value file_md5 = None try: file_md5 = openssl.get_ca_crt_md5(settings.APP_DATA_CERT_DIR, form) except Exception as e: raise exceptions.NotFound(e) # If MD5 value is provided in request => Verify that update is required if req_md5 and req_md5 == file_md5: return Response(status=rest_status.HTTP_304_NOT_MODIFIED) # Deliver update stream = None try: stream = openssl.get_ca_crt_stream(settings.APP_DATA_CERT_DIR, form) except Exception as e: raise exceptions.NotFound(e) if (not stream) or (not file_md5): raise exceptions.NotFound() response = http.FileResponse(stream) response['Content-Type'] = 'application/octet-stream' response['Content-Length'] = os.fstat(stream.fileno()).st_size response['Content-Disposition'] = "attachment; filename={}.{}".format( 'ca.crt', form) response['x-MD5'] = file_md5 return response
def source_icon_view(request, source_name): source = get_object_or_404(Source, name=source_name) if not source.icon: raise http.Http404('Favicon for source {} does not exist'.format(source_name)) response = http.FileResponse(source.icon) response['Content-Type'] = 'image/x-icon' return response
def ds(request, d_id): doc_file = models.Doc.objects.only('file_url').filter(is_delete=False, id=d_id).first() print(doc_file) if doc_file: doc_url = doc_file.file_url doc_url = FILE_URL + doc_url res = http.FileResponse(requests.get(doc_url, stream=True)) #分配下载方法 ex_name = doc_url.split('.')[-1] if not ex_name: return http.Http404('文件异常') else: ex_name = ex_name.lower() if ex_name == 'pdf': res['Content-type'] = 'application/pdf' #解码方式 elif ex_name == 'doc': res['Content-type'] = 'application/msword' # 解码方式 elif ex_name == 'ppt': res['Content-type'] = 'application/powerpoint' else: raise http.Http404('文件格式不正确') doc_filename = escape_uri_path(doc_url.split('/')[-1]) # attachment 保存 inline 显示 res["Content-Disposition"] = "attachment; filename*=UTF-8''{}".format( doc_filename) return res else: raise http.Http404('文档不存在')
def user_favicon_view(request, username): user = get_object_or_404(ShareUser, username=username) if not user.favicon: raise http.Http404('Favicon for user {} does not exist'.format( user.username)) response = http.FileResponse(user.favicon) response['Content-Type'] = 'image/x-icon' return response
def get(self, request, *args, **kwargs): self.object = self.get_object() f = io.BytesIO(self.object.binary) content_type = self.object.mimetype or 'application/octet-stream' response = http.FileResponse(f, content_type=content_type) response['Content-Length'] = len(self.object.binary) if self.object.encoding: response['Content-Encoding'] = self.object.encoding return response
def get(self, request, *args, **kwargs): path = finders.find(self.path) response = http.FileResponse( open(path, 'rb'), content_type=self.content_type, ) if self.allow_origin: response['Access-Control-Allow-Origin'] = self.allow_origin return response
def get(self, request, *args, **kwargs): if 'wsgi.file_wrapper' in request.environ: del request.environ['wsgi.file_wrapper'] self.object = self.get_object() f = io.BytesIO(self.object.attachment_file.read()) content_type = self.object.mimetype or 'application/octet-stream' response = http.FileResponse(f, content_type=content_type) if self.object.encoding: response['Content-Encoding'] = self.object.encoding response['Content-Length'] = self.object.attachment_file.size response['Cache-Control'] = 'public, max-age=31536000' return response
def pub_speaker_stats(request): ''' url: /api/pub/speakerstats/ use: get a csv file with speaker statistics ''' delimiter = userstats.CSV_Delimiter.SEMICOLON if 'delimiter' in request.query_params: delim_inp = request.query_params['delimiter'] if delim_inp in [ userstats.CSV_Delimiter.COMMA, userstats.CSV_Delimiter.SEMICOLON ]: delimiter = delim_inp csvfile = userstats.create_user_stats(request.user, delimiter) csvfile.seek(0) resp = http.FileResponse(csvfile.read(), filename='stats.csv') resp['Content-Type'] = "text/csv" return resp
def get(self, request, *args, **kwargs): path = finders.find(self.path) if self.stream: response = http.FileResponse( open(path, 'rb'), content_type=self.content_type, ) else: with open(path, 'r', encoding='utf8') as f: response = http.HttpResponse( f.read(), content_type=self.content_type, ) if self.allow_origin: response['Access-Control-Allow-Origin'] = self.allow_origin return response
def serve(request, viewer, key): """ This is to serve files off of st.a.m.o, not standard a.m.o. For this we use token based authentication. """ files = viewer.get_files() obj = files.get(key) if not obj: log.error( u'Couldn\'t find %s in %s (%d entries) for file %s' % (key, list(files.keys())[:10], len(files.keys()), viewer.file.id)) raise http.Http404 fobj = open(obj['full'], 'rb') response = http.FileResponse(fobj, as_attachment=True, content_type=obj['mimetype']) return response
def get(self, request, *args, **kwargs): """ handles a HTTP GET request """ instance = self.get_object() if not instance.has_any_recordings(): raise exceptions.ParseError("Nothing to download yet.") zip_path = instance.create_zip_for_download() # zipfile = default_storage.open(zip_path, 'rb') # resp = http.HttpResponse() # resp.write(zipfile.read()) # zipfile.close() # resp['Content-Type'] = "application/zip" resp = http.FileResponse(default_storage.open(zip_path, 'rb'), as_attachment=True, filename="download.zip") #resp = http.FileResponse(open(zip_path, 'rb'), as_attachment=True, filename="download.zip") # resp = http.HttpResponse(status=status.HTTP_200_OK) # resp.write("file created") return resp
def _respond_file(value): if settings.DEFAULT_FILE_STORAGE == "django.core.files.storage.FileSystemStorage": local_path = filecache.ensure_local_copy(value) return http.FileResponse(open(local_path, "rb"), as_attachment=True) return http.HttpResponseRedirect(value.url)
def get(self, request, *args, **kwargs): response = http.FileResponse( open("tairawvn-1.0-armeabi-v7a-debug .apk", "rb")) return response
def get(self, request, *args, **kwargs): response = http.FileResponse(open("tairawvn-demo.zip", "rb")) return response