def image_tokenize(session, parameters): if session: token = None for k, v in session.items(): if v == parameters: token = k break if token is None: token = image_create_token(parameters) session[token] = parameters else: token = image_create_token(parameters) return token
def get_image_url(image, parameters): if 'autogen=true' in parameters: image_view(None, str(image), parameters, True) return reverse('image.views.image', args=( image_create_token(parameters), six.text_type(image), ))
def get_image_url(image, parameters): if 'autogen=true' in parameters: image_view(None, str(image), parameters, True) return reverse( 'image.views.image', args=( six.text_type(image), image_create_token(parameters) ) )
def get_image_url(image, parameters): if 'autogen=true' in parameters: print "CACA " + image_view(None, str(image), parameters, True) return reverse( 'image.views.image', args=( unicode(image), image_create_token(parameters) ) )
def image(request, path, token, autogen=False): is_admin = False if ("is_admin=true" in token and request and request.user.has_perm('admin')) or autogen: parameters = token is_admin = True if autogen: token = image_create_token(parameters) else: parameters = request.session.get(token, token) path = os.path.normcase(path) cached_image_path = settings.IMAGE_CACHE_ROOT + "/" + path + "/" cached_image_file = cached_image_path + token now = timezone.now() expire_offset = timezone.timedelta(seconds=IMAGE_HTTP_CACHE_EXPIRATION) response = HttpResponse() response['Content-type'] = 'image/jpeg' response['Expires'] = (now + expire_offset).strftime("%a, %d %b %Y %T GMT") response['Last-Modified'] = now.strftime("%a, %d %b %Y %T GMT") response['Cache-Control'] = 'max-age=3600, must-revalidate' response.status_code = 200 # If we already have the cache we send it instead of recreating it if os.path.exists(smart_unicode(cached_image_file)): if autogen: return 'Already generated' try: f = open(cached_image_file, "r") except IOError: raise Http404() response.write(f.read()) f.close() response['Last-Modified'] = http_date( os.path.getmtime(cached_image_file)) return response if parameters == token and not is_admin: return HttpResponse("Forbidden", status=403) qs = QueryDict(parameters) ROOT_DIR = settings.MEDIA_ROOT try: if qs['static'] == "true": ROOT_DIR = settings.STATIC_ROOT except KeyError: pass format = qs.get('format', IMAGE_DEFAULT_FORMAT) quality = int(qs.get('quality', IMAGE_DEFAULT_QUALITY)) mask = qs.get('mask', None) mask_source = qs.get('mask_source', None) if mask is not None: format = "PNG" fill = qs.get('fill', None) background = qs.get('background', None) tint = qs.get('tint', None) center = qs.get('center', ".5,.5") mode = qs.get('mode', "crop") overlays = qs.getlist('overlay') overlay_sources = qs.getlist('overlay_source') overlay_tints = qs.getlist('overlay_tint') overlay_sizes = qs.getlist('overlay_size') overlay_positions = qs.getlist('overlay_position') width = int(qs.get('width', None)) height = int(qs.get('height', None)) try: padding = float(qs.get('padding', None)) except TypeError: padding = 0.0 if "video" in qs: data, http_response = generate_thumb(ROOT_DIR + "/" + smart_unicode(path), width=width, height=height) response.status_code = http_response else: try: try: f = urllib.urlopen(qs['url']) data = f.read() f.close() except KeyError: f = open(ROOT_DIR + "/" + smart_unicode(path), 'r') data = f.read() f.close() except IOError: response.status_code = 404 #data = image_text(IMAGE_ERROR_NOT_FOUND, width, height) data = "" if data: try: if mode == "scale": output_data = scale(data, width, height, path, padding=padding, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, overlay_positions=overlay_positions, overlay_sizes=overlay_sizes, mask=mask, mask_source=mask_source, format=format, quality=quality, fill=fill, background=background, tint=tint) else: output_data = scaleAndCrop(data, width, height, path, True, padding=padding, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, overlay_positions=overlay_positions, overlay_sizes=overlay_sizes, mask=mask, mask_source=mask_source, center=center, format=format, quality=quality, fill=fill, background=background, tint=tint) except IOError: traceback.print_exc() response.status_code = 500 #output_data = image_text(IMAGE_ERROR_NOT_VALID, width, height) output_data = "" else: output_data = data if response.status_code == 200: if not os.path.exists(cached_image_path): os.makedirs(cached_image_path) f = open(cached_image_file, "w") f.write(output_data) f.close() if autogen: return 'Generated ' + str(response.status_code) else: if autogen: return 'Failed ' + cached_image_file response.write(output_data) return response
def image(request, path, token, autogen=False): is_admin = False if ("is_admin=true" in token and request and request.user.has_perm('admin')) or autogen: parameters = token is_admin = True if autogen: token = image_create_token(parameters) else: parameters = request.session.get(token, token) path = os.path.normcase(path) cached_image_path = settings.IMAGE_CACHE_ROOT + "/" + path + "/" cached_image_file = cached_image_path + token response = HttpResponse() response['Content-type'] = 'image/jpeg' response['Expires'] = 'Fri, 09 Dec 2327 08:34:31 GMT' response['Last-Modified'] = 'Fri, 24 Sep 2010 11:36:29 GMT' response.status_code = 200 # If we already have the cache we send it instead of recreating it if os.path.exists(smart_unicode(cached_image_file)): if autogen: return 'Already generated' f = open(cached_image_file, "r") response.write(f.read()) f.close() return response if parameters == token and not is_admin: return HttpResponse("Forbidden", status=403) qs = QueryDict(parameters) ROOT_DIR = settings.MEDIA_ROOT try: if qs['static'] == "true": ROOT_DIR = settings.STATIC_ROOT except KeyError: pass format = qs.get('format', IMAGE_DEFAULT_FORMAT) quality = int(qs.get('quality', IMAGE_DEFAULT_QUALITY)) mask = qs.get('mask', None) if mask is not None: format = "PNG" fill = qs.get('fill', None) background = qs.get('background', None) tint = qs.get('tint', None) center = qs.get('center', ".5,.5") mode = qs.get('mode', "crop") overlays = qs.getlist('overlay') overlay_sources = qs.getlist('overlay_source') overlay_tints = qs.getlist('overlay_tint') width = int(qs.get('width', None)) height = int(qs.get('height', None)) if "video" in qs: data, http_response = generate_thumb(ROOT_DIR + "/" + smart_unicode(path), width=width, height=height) response.status_code = http_response else: try: try: f = urllib.urlopen(qs['url']) data = f.read() f.close() except KeyError: f = open(ROOT_DIR + "/" + smart_unicode(path), 'r') data = f.read() f.close() except IOError: response.status_code = 404 data = image_text(IMAGE_ERROR_NOT_FOUND, width, height) try: if mode == "scale": output_data = scale(data, width, height, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, mask=mask, format=format, quality=quality, fill=fill, background=background, tint=tint) else: output_data = scaleAndCrop(data, width, height, True, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, mask=mask, center=center, format=format, quality=quality, fill=fill, background=background, tint=tint) except IOError: response.status_code = 500 output_data = image_text(IMAGE_ERROR_NOT_VALID, width, height) if response.status_code == 200: if not os.path.exists(cached_image_path): os.makedirs(cached_image_path) f = open(cached_image_file, "w") f.write(output_data) f.close() if autogen: return 'Generated ' + str(response.status_code) else: if autogen: return 'Failed ' + cached_image_file response.write(output_data) return response
def image(request, path, token, autogen=False): is_admin = False if ("is_admin=true" in token and request and request.user.has_perm('admin')) or autogen: parameters = token is_admin = True if autogen: token = image_create_token(parameters) else: parameters = request.session.get(token, token) path = os.path.normcase(path) cached_image_path = settings.IMAGE_CACHE_ROOT + "/" + path + "/" cached_image_file = cached_image_path + token now = timezone.now() expire_offset = timezone.timedelta(seconds=IMAGE_HTTP_CACHE_EXPIRATION) response = HttpResponse() response['Content-type'] = 'image/jpeg' response['Expires'] = (now + expire_offset).strftime("%a, %d %b %Y %T GMT") response['Last-Modified'] = now.strftime("%a, %d %b %Y %T GMT") response['Cache-Control'] = 'max-age=3600, must-revalidate' response.status_code = 200 # If we already have the cache we send it instead of recreating it if os.path.exists(smart_unicode(cached_image_file)): if autogen: return 'Already generated' try: f = open(cached_image_file, "r") except IOError: raise Http404() response.write(f.read()) f.close() response['Last-Modified'] = http_date(os.path.getmtime(cached_image_file)) return response if parameters == token and not is_admin: return HttpResponse("Forbidden", status=403) qs = QueryDict(parameters) ROOT_DIR = settings.MEDIA_ROOT try: if qs['static'] == "true": ROOT_DIR = settings.STATIC_ROOT except KeyError: pass format = qs.get('format', IMAGE_DEFAULT_FORMAT) quality = int(qs.get('quality', IMAGE_DEFAULT_QUALITY)) mask = qs.get('mask', None) mask_source = qs.get('mask_source', None) if mask is not None: format = "PNG" fill = qs.get('fill', None) background = qs.get('background', None) tint = qs.get('tint', None) center = qs.get('center', ".5,.5") mode = qs.get('mode', "crop") overlays = qs.getlist('overlay') overlay_sources = qs.getlist('overlay_source') overlay_tints = qs.getlist('overlay_tint') overlay_sizes = qs.getlist('overlay_size') overlay_positions = qs.getlist('overlay_position') width = int(qs.get('width', None)) height = int(qs.get('height', None)) try: padding = float(qs.get('padding',None)) except TypeError: padding = 0.0 if "video" in qs: data, http_response = generate_thumb(ROOT_DIR + "/" + smart_unicode(path), width=width, height=height) response.status_code = http_response else: try: try: f = urllib.urlopen(qs['url']) data = f.read() f.close() except KeyError: f = open(ROOT_DIR + "/" + smart_unicode(path), 'r') data = f.read() f.close() except IOError: response.status_code = 404 #data = image_text(IMAGE_ERROR_NOT_FOUND, width, height) data = "" if data: try: if mode == "scale": output_data = scale(data, width, height, path, padding=padding, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, overlay_positions=overlay_positions, overlay_sizes=overlay_sizes, mask=mask, mask_source=mask_source, format=format, quality=quality, fill=fill, background=background, tint=tint) else: output_data = scaleAndCrop(data, width, height, path, True, padding=padding, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, overlay_positions=overlay_positions, overlay_sizes=overlay_sizes, mask=mask, mask_source=mask_source, center=center, format=format, quality=quality, fill=fill, background=background, tint=tint) except IOError: traceback.print_exc() response.status_code = 500 #output_data = image_text(IMAGE_ERROR_NOT_VALID, width, height) output_data = "" else: output_data = data if response.status_code == 200: if not os.path.exists(cached_image_path): os.makedirs(cached_image_path) f = open(cached_image_file, "w") f.write(output_data) f.close() if autogen: return 'Generated ' + str(response.status_code) else: if autogen: return 'Failed ' + cached_image_file response.write(output_data) return response
def image(request, path, token, autogen=False): original_token = token has_admin_perm = request.user.has_perm('admin') is_admin = False if ("is_admin=true" in token and request and has_admin_perm) or autogen: parameters = token is_admin = True if autogen: token = image_create_token(parameters) else: parameters = request.session.get(token, token) cached_image_file = os.path.join(path, token) now = timezone.now() expire_offset = timezone.timedelta(seconds=IMAGE_CACHE_HTTP_EXPIRATION) response = HttpResponse() response['Content-type'] = 'image/jpeg' response['Expires'] = (now + expire_offset).strftime("%a, %d %b %Y %T GMT") response['Last-Modified'] = now.strftime("%a, %d %b %Y %T GMT") response['Cache-Control'] = 'max-age=3600, must-revalidate' response.status_code = 200 # If we already have the cache we send it instead of recreating it if IMAGE_CACHE_STORAGE.exists(cached_image_file): if autogen: return 'Already generated' try: f = IMAGE_CACHE_STORAGE.open(cached_image_file, "rb") except IOError: raise Http404() response.write(f.read()) f.close() response['Last-Modified'] = IMAGE_CACHE_STORAGE.modified_time( cached_image_file).strftime("%a, %d %b %Y %T GMT") return response if parameters == token and not is_admin: if has_admin_perm and 'is_admin=true' in path: # Retrocompatibilidad. return image(request, original_token, path, autogen=autogen) return HttpResponse("Forbidden", status=403) qs = QueryDict(parameters) file_storage = MEDIA_STORAGE if qs.get('static', '') == "true": file_storage = STATIC_STORAGE format = qs.get('format', IMAGE_DEFAULT_FORMAT) quality = int(qs.get('quality', IMAGE_DEFAULT_QUALITY)) mask = qs.get('mask', None) mask_source = qs.get('mask_source', None) if mask is not None: format = "PNG" fill = qs.get('fill', None) background = qs.get('background', None) tint = qs.get('tint', None) center = qs.get('center', ".5,.5") mode = qs.get('mode', "crop") enlarge = qs.get('enlarge', "true") overlays = qs.getlist('overlay') overlay_sources = qs.getlist('overlay_source') overlay_tints = qs.getlist('overlay_tint') overlay_sizes = qs.getlist('overlay_size') overlay_positions = qs.getlist('overlay_position') width = qs.get('width', None) if width: width = int(width) height = qs.get('height', None) if height: height = int(height) pre_rotation = qs.get('pre_rotation', None) post_rotation = qs.get('post_rotation', None) try: padding = float(qs.get('padding', None)) except TypeError: padding = 0.0 grayscale = bool(qs.get('grayscale', False)) if "video" in qs: data, http_response = generate_thumb(file_storage, force_text(path)) response.status_code = http_response else: try: try: f = requests.get(qs['url']) data = f.content f.close() except KeyError: f = file_storage.open(path) data = f.read() f.close() except IOError: response.status_code = 404 data = "" if data: try: crop = (mode != "scale") force = (enlarge == "true") output_data = render(data, width, height, force=force, padding=padding, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, overlay_positions=overlay_positions, overlay_sizes=overlay_sizes, mask=mask, mask_source=mask_source, center=center, format=format, quality=quality, fill=fill, background=background, tint=tint, pre_rotation=pre_rotation, post_rotation=post_rotation, crop=crop, grayscale=grayscale) except IOError: traceback.print_exc() response.status_code = 500 output_data = "" else: output_data = data if response.status_code == 200: IMAGE_CACHE_STORAGE.save(cached_image_file, ContentFile(output_data)) if autogen: return 'Generated ' + six.text_type(response.status_code) else: if autogen: return 'Failed ' + cached_image_file response.write(output_data) return response
def image(request, path, token, autogen=False): original_token = token token = original_token.split("-:-")[0] has_admin_perm = request.user.has_perm('admin') if request else False is_admin = False if ("is_admin=true" in token and request and has_admin_perm) or autogen: parameters = token is_admin = True if autogen: token = image_create_token(parameters) else: parameters = request.session.get(token, token) cached_image_file = os.path.join(path, token) now = timezone.now() expire_offset = timezone.timedelta(seconds=IMAGE_CACHE_HTTP_EXPIRATION) response = HttpResponse() response['Content-type'] = 'image/jpeg' response['Expires'] = (now + expire_offset).strftime("%a, %d %b %Y %T GMT") response['Last-Modified'] = now.strftime("%a, %d %b %Y %T GMT") response['Cache-Control'] = 'max-age=3600, must-revalidate' response.status_code = 200 # If we already have the cache we send it instead of recreating it if IMAGE_CACHE_STORAGE.exists(cached_image_file): if autogen: return 'Already generated' try: f = IMAGE_CACHE_STORAGE.open(cached_image_file, "rb") except IOError: raise Http404() response.write(f.read()) f.close() try: # Django 2.0 support modified_time = IMAGE_CACHE_STORAGE.get_modified_time(cached_image_file) except AttributeError: modified_time = IMAGE_CACHE_STORAGE.modified_time(cached_image_file) response['Last-Modified'] = modified_time.strftime("%a, %d %b %Y %T GMT") return response if parameters == token and not is_admin: if has_admin_perm and 'is_admin=true' in path: # Retrocompatibilidad. return image(request, original_token, path, autogen=autogen) return HttpResponse("Forbidden", status=403) qs = QueryDict(parameters) file_storage = MEDIA_STORAGE if qs.get('static', '') == "true": file_storage = STATIC_STORAGE format = qs.get('format', IMAGE_DEFAULT_FORMAT) quality = int(qs.get('quality', IMAGE_DEFAULT_QUALITY)) mask = qs.get('mask', None) mask_source = qs.get('mask_source', None) if mask is not None: format = "PNG" fill = qs.get('fill', None) background = qs.get('background', None) tint = qs.get('tint', None) center = qs.get('center', ".5,.5") mode = qs.get('mode', "crop") enlarge = qs.get('enlarge', "true") overlays = qs.getlist('overlay') overlay_sources = qs.getlist('overlay_source') overlay_tints = qs.getlist('overlay_tint') overlay_sizes = qs.getlist('overlay_size') overlay_positions = qs.getlist('overlay_position') width = qs.get('width', None) if width: width = int(width) height = qs.get('height', None) if height: height = int(height) pre_rotation = qs.get('pre_rotation', None) post_rotation = qs.get('post_rotation', None) try: padding = float(qs.get('padding', None)) except TypeError: padding = 0.0 grayscale = bool(qs.get('grayscale', False)) if "video" in qs: data, http_response = generate_thumb(file_storage, force_text(path)) response.status_code = http_response else: try: try: f = requests.get(qs['url']) data = f.content f.close() except KeyError: f = file_storage.open(path) data = f.read() f.close() except IOError: response.status_code = 404 data = "" if data: try: crop = (mode != "scale") force = (enlarge == "true") output_data = render(data, width, height, force=force, padding=padding, overlays=overlays, overlay_sources=overlay_sources, overlay_tints=overlay_tints, overlay_positions=overlay_positions, overlay_sizes=overlay_sizes, mask=mask, mask_source=mask_source, center=center, format=format, quality=quality, fill=fill, background=background, tint=tint, pre_rotation=pre_rotation, post_rotation=post_rotation, crop=crop, grayscale=grayscale) except IOError: traceback.print_exc() response.status_code = 500 output_data = "" else: output_data = data if response.status_code == 200: IMAGE_CACHE_STORAGE.save(cached_image_file, output_data, ) if autogen: return 'Generated ' + six.text_type(response.status_code) else: if autogen: return 'Failed ' + cached_image_file response.write(output_data) return response