def thumbnail(request): matchdict = request.matchdict photo_album_id = matchdict['photo_album_id'] photo_id = matchdict['photo_id'] photo_album = models.PhotoAlbum.objects.with_id(photo_album_id) photo = photo_album.get_photo(photo_id) extension = photo.image.filename[photo.image.filename.rfind('.')+1:] if photo.image.thumbnail: image = photo.image.thumbnail response = Response() if extension.lower() in ['jpg', 'jpeg']: response.content_type='image/jpeg' elif extension.lower() in ['png']: response.content_type='image/png' img = Image.open(image) img_format = img.format if photo.orientation == 'vertical': img = img.transpose(Image.ROTATE_90) tmp_img = tempfile.TemporaryFile() img.save(tmp_img, format=img_format) tmp_img.seek(0) response.body_file = tmp_img return response
def thumbnail(request): matchdict = request.matchdict photo_album_id = matchdict['photo_album_id'] photo_id = matchdict['photo_id'] photo_album = models.PhotoAlbum.objects.with_id(photo_album_id) photo = photo_album.get_photo(photo_id) extension = photo.image.filename[photo.image.filename.rfind('.') + 1:] if photo.image.thumbnail: image = photo.image.thumbnail response = Response() if extension.lower() in ['jpg', 'jpeg']: response.content_type = 'image/jpeg' elif extension.lower() in ['png']: response.content_type = 'image/png' img = Image.open(image) img_format = img.format if photo.orientation == 'vertical': img = img.transpose(Image.ROTATE_90) tmp_img = tempfile.TemporaryFile() img.save(tmp_img, format=img_format) tmp_img.seek(0) response.body_file = tmp_img return response
def __call__(self): response = Response() response.content_disposition = 'attachment; filename="{}"'.format(self.context.filename) response.charset = 'utf-8' response.content_type = self.context.content_type response.body_file = self.context.content.open() response.content_length = self.context.size return response
def retrieve_list(req): params = _parse_retrieve_params(req) headers = { 'Content-Type': 'application/json', } resp = requests.get(_get_db_uri(req.db, params), stream=True, params=params) r = Response(headers=headers) r.body_file = resp.raw return r
def account_photo_get(self): if self.user is None or self.user.photo.get() is None: response = Response(content_type='image/png') response.app_iter = open('myproject/static/images/unknown.png', 'rb') else: content_type = self.user.photo.content_type.encode('ascii') response = Response(content_type=content_type) response.body_file = self.user.photo return response
def retrieve_list(req): params = _parse_retrieve_params(req) headers = { 'Content-Type': 'application/json', } resp = requests.get(_get_db_uri(req.db, params), stream=True, params=params) r = Response(headers=headers) r.body_file = resp.raw return r
def FileView(request): # pylint: disable=invalid-name """Default file view""" context = request.context # set content type content_type = context.content_type if isinstance(content_type, bytes): content_type = content_type.decode('utf-8') # check for last modification date response = Response(content_type=content_type) zdc = IZopeDublinCore(context, None) if zdc is not None: modified = zdc.modified if modified is not None: if_modified_since = request.if_modified_since # pylint: disable=no-member if if_modified_since and \ (int(modified.timestamp()) <= int(if_modified_since.timestamp())): return Response(content_type=content_type, status=NOT_MODIFIED) response.last_modified = modified body_file = context.get_blob(mode='c') if request.params.get('dl') is not None: filename = context.filename or 'noname.txt' response.content_disposition = 'attachment; filename="{0}"'.format( translate_string(filename, force_lower=False)) # check for range request if request.range is not None: try: body = body_file.read() body_length = len(body) range_start = request.range.start or 0 if 'Firefox' in request.user_agent: # avoid partial range for Firefox videos range_end = body_length else: range_end = request.range.end or min( body_length, range_start + MAX_RANGE_LENGTH) ranged_body = body[range_start:range_end] response.status = PARTIAL_CONTENT response.headers[ 'Content-Range'] = 'bytes {first}-{last}/{len}'.format( first=range_start, last=range_start + len(ranged_body) - 1, len=body_length) response.body = ranged_body finally: body_file.close() else: response.body_file = body_file return response
def zip(context, request): temp = tempfile.mkdtemp() filename = '%s/%s' % (temp,'buildout.zip') with zipfile.ZipFile(filename, 'w') as buildout: for bfile in context.keys(): buildout.writestr(bfile.encode('utf-8'), context[bfile].data.encode('utf-8')) response = Response() zipped_file = open(filename, 'r') response.body_file = zipped_file response.content_type = 'application/zip' response.content_disposition = "attachment; filename=buildout.zip" return response
def __call__(self): check_update = True if self.request.GET.get('check_update', 'true') == 'true' else False package = self.context.__parent__.__parent__ last_remote_version = Package.get_last_remote_version(self.proxy, package.name) if check_update: if not package.repository_is_up_to_date(last_remote_version): return not_found(self.request) response = Response() response.content_disposition = 'attachment; filename="{}"'.format(self.context.filename) response.charset = 'utf-8' response.content_type = self.context.content_type response.body_file = self.context.content.open() return response
def file_storage(request): id = request.matchdict['id'] try: f = fs_files.get(id) content_type = f.content_type.encode('ascii') response = Response(content_type=content_type) response.body_file = f response.headers["Content-disposition"] = "filename=" + f.name.encode('euc-kr') except: response = Response(content_type='image/png') response.app_iter = open('myproject/static/images/not_found.png', 'rb') return response
def image_fullsize(request): id = request.matchdict['id'] try: image = fs_images.get(id) content_type = image.content_type.encode('ascii') response = Response(content_type=content_type) response.body_file = image response.headers["Content-disposition"] = "filename=" + image.name.encode('euc-kr') except: response = Response(content_type='image/png') response.app_iter = open('myproject/static/images/no_image.png', 'rb') return response
def export(request): from pyramid.response import Response from pyramid.httpexceptions import HTTPServerError from subprocess import Popen, PIPE import os.path import datetime response = Response( content_type='application/zip', ) if not os.path.isdir('.git'): raise HTTPServerError('not a git directory') try: proc = Popen( [ 'git', 'describe', '--always', ], stdout=PIPE) rev = proc.stdout.read().strip() proc = Popen( [ 'git', 'archive', '--format=zip', '--prefix=oscad/', '-9', 'HEAD', ], stdout=PIPE) except OSError as e: raise HTTPServerError(e) time = datetime.datetime.now().replace(microsecond=0) filename = 'oscad-{}-git-{}.zip'.format( time.isoformat(), rev ) response.content_disposition = 'attachment; filename="{}"'.format(filename) response.body_file = proc.stdout return response
def __call__(self): check_update = True if self.request.GET.get( 'check_update', 'true') == 'true' else False package = self.context.__parent__.__parent__ last_remote_version = Package.get_last_remote_version( self.proxy, package.name) if check_update: if not package.repository_is_up_to_date(last_remote_version): return not_found(self.request) response = Response() response.content_disposition = 'attachment; filename="{}"'.format( self.context.filename) response.charset = 'utf-8' response.content_type = self.context.content_type response.body_file = self.context.content.open() response.content_length = self.context.size return response
def view(request): matchdict = request.matchdict photo_album_id = matchdict['photo_album_id'] photo_id = matchdict['photo_id'] photo_album = models.PhotoAlbum.objects.with_id(photo_album_id) image = photo_album.get_photo(photo_id).image response = Response() extension = image.filename[image.filename.rfind('.') + 1:] if extension.lower() in ['jpg', 'jpeg']: response.content_type = 'image/jpeg' elif extension.lower() in ['png']: response.content_type = 'image/png' response.body_file = image return response
def view(request): matchdict = request.matchdict photo_album_id = matchdict['photo_album_id'] photo_id = matchdict['photo_id'] photo_album = models.PhotoAlbum.objects.with_id(photo_album_id) image = photo_album.get_photo(photo_id).image response = Response() extension = image.filename[image.filename.rfind('.')+1:] if extension.lower() in ['jpg', 'jpeg']: response.content_type='image/jpeg' elif extension.lower() in ['png']: response.content_type='image/png' response.body_file = image return response
def image_thumbnail(request): id = request.matchdict['id'] image = fs_images.get(id) #generate thumbnail in memory img = Image.open(image) if img.size[0] > THUMBNAIL_WIDTH: w, h = THUMBNAIL_WIDTH, (THUMBNAIL_WIDTH * img.size[1]) / img.size[0] else: w, h = img.size thumbnail = img.copy() thumbnail.thumbnail((w, h), Image.ANTIALIAS) io = StringIO() thumbnail.save(io, img.format) io.seek(0) content_type = image.content_type.encode('ascii') response = Response(content_type=content_type) response.body_file = io response.headers["Content-disposition"] = "filename=" + image.name.encode('euc-kr') return response