Ejemplo n.º 1
0
def appImageHandle(environ, start_response):
	"""main image url process"""
	engine_code = environ.get('IMSTO_ENGINE', 'default')
	print 'engine_code: {0}'.format(engine_code)
	path = environ.get('PATH_INFO', '')
	image_url_regex = r'/([a-z0-9]{2})/([a-z0-9]{2})/([a-z0-9]{19,36})(-[scwh]\d{2,4})?\.(gif|jpg|jpeg|png)$'
	match = re.search(image_url_regex, path)
	#print(image_url_regex, path, match)
	if match is None:
		return not_found(environ, start_response, 'invalid path')

	ids = match.groups()
	#print(ids)
	id = '{0}{1}{2}'.format(*ids)
	#print(id)
	org_path = '{0}/{1}/{2}.{4}'.format(*ids)
	org_file = '{0}/{1}'.format(THUMB_ROOT, org_path)

	if not os.path.exists(org_file):
		if engine_code == 's3':
			ret = processFileS3(org_path, org_file)
		else:
			ret = processFile(id, org_file)

		if ret is False:
			return not_found(environ, start_response, message = 'id not found')

	if not os.path.exists(org_file):
		return not_found(environ, start_response, message = 'file not found')

	if ids[3] is None:
		dst_path = org_path
		dst_file = org_file
	else:
		dst_path = '{0}/{1}/{2}{3}.{4}'.format(*ids)
		dst_file = '{0}/{1}'.format(THUMB_ROOT, dst_path)
		#print(ids[3][1:])
		mode = ids[3][1]
		size = int(ids[3][2:])
		if size not in SUPPORTED_SIZE:
			print('unsupported size: {0}'.format(size))
			return not_found(environ, start_response, message = 'unsupported size')
		thumb_image(org_file, size, dst_file, mode)
	#print(dst_file)
	server_soft = environ.get('SERVER_SOFTWARE','')
	if server_soft[:5] == 'nginx' and os.name != 'nt':
		print('{0}/{1}'.format(THUMB_PATH, dst_path))
		start_response('200 OK', [('X-Accel-Redirect', '{0}/{1}'.format(THUMB_PATH, dst_path))])
		return []
	#print(file.type) 
	mimetype = guess_mimetype(dst_path)
	filesize = os.path.getsize(dst_file)
	headers = [('Content-Type', str(mimetype)), ('Content-Length', '{0}'.format(filesize)), ('Via','imsto')]
	#print(headers)
	start_response('200 OK', headers)
	fd = open(dst_file,'r')
	return environ['wsgi.file_wrapper'](fd, 4096)
Ejemplo n.º 2
0
def image_handle(environ, start_response):
    """main url process"""
    path = environ.get('PATH_INFO', '')
    image_url_regex = r'/([a-z0-9]{2})/([a-z0-9]{2})/([a-z0-9]{19,36})(-[sc]\d{2,4})?\.(gif|jpg|jpeg|png)$'
    match = re.search(image_url_regex, path)
    #print(image_url_regex, path, match)
    if match is not None:
        ids = match.groups()
        #print(ids)
        id = '{0}{1}{2}'.format(*ids)
        from store import ImSto
        imsto = ImSto()
        file = imsto.get(id)
        if file is None:
            imsto.close()
            return not_found(environ, start_response)

        org_path = '{0}/{1}/{2}.{4}'.format(*ids)
        org_file = '{0}/{1}'.format(THUMB_ROOT, org_path)
        if not os.path.exists(org_file):
            save_file(file, org_file)
        if ids[3] is None:
            dst_path = org_path
            dst_file = org_file
        else:
            dst_path = '{0}/{1}/{2}{3}.{4}'.format(*ids)
            dst_file = '{0}/{1}'.format(THUMB_ROOT, dst_path)
            #print(ids[3][1:])
            size = int(ids[3][2:])
            if size not in SUPPORTED_SIZE:
                print('unsupported size: {0}'.format(size))
                imsto.close()
                return not_found(environ, start_response)
            thumb_image(org_file, size, dst_file)
        #print(dst_file)
        server_soft = environ.get('SERVER_SOFTWARE', '')
        if server_soft[:5] == 'nginx' and os.name != 'nt':
            imsto.close()
            start_response(
                '200 OK',
                [('X-Accel-Redirect', '{0}/{1}'.format(THUMB_PATH, dst_path))])
            return []
        #print(file.type)
        headers = [('Content-Type', str(file.type)),
                   ('Content-Length', '{0.length}'.format(file)),
                   ('Via', 'imsto')]
        #print(headers)
        start_response('200 OK', headers)
        # TODO: response file content
        #data = file.read()
        imsto.close()
        #return [data]
        fd = open(dst_file, 'r')
        return environ['wsgi.file_wrapper'](fd, 4096)

    return not_found(environ, start_response)
Ejemplo n.º 3
0
def image_handle(environ, start_response):
	"""main url process"""
	path = environ.get('PATH_INFO', '')
	image_url_regex = r'/([a-z0-9]{2})/([a-z0-9]{2})/([a-z0-9]{19,36})(-[sc]\d{2,4})?\.(gif|jpg|jpeg|png)$'
	match = re.search(image_url_regex, path)
	#print(image_url_regex, path, match)
	if match is not None:
		ids = match.groups()
		#print(ids)
		id = '{0}{1}{2}'.format(*ids)
		from store import ImSto
		imsto = ImSto()
		file = imsto.get(id)
		if file is None:
			imsto.close()
			return not_found(environ, start_response)

		org_path = '{0}/{1}/{2}.{4}'.format(*ids)
		org_file = '{0}/{1}'.format(THUMB_ROOT, org_path)
		if not os.path.exists(org_file):
			save_file(file, org_file)
		if ids[3] is None:
			dst_path = org_path
			dst_file = org_file
		else:
			dst_path = '{0}/{1}/{2}{3}.{4}'.format(*ids)
			dst_file = '{0}/{1}'.format(THUMB_ROOT, dst_path)
			#print(ids[3][1:])
			size = int(ids[3][2:])
			if size not in SUPPORTED_SIZE:
				print('unsupported size: {0}'.format(size))
				imsto.close()
				return not_found(environ, start_response)
			thumb_image(org_file, size, dst_file)
		#print(dst_file)
		server_soft = environ.get('SERVER_SOFTWARE','')
		if server_soft[:5] == 'nginx' and os.name != 'nt':
			imsto.close()
			start_response('200 OK', [('X-Accel-Redirect', '{0}/{1}'.format(THUMB_PATH, dst_path))])
			return []
		#print(file.type) 
		headers = [('Content-Type', str(file.type)), ('Content-Length', '{0.length}'.format(file)), ('Via','imsto')]
		#print(headers)
		start_response('200 OK', headers)
		# TODO: response file content
		#data = file.read()
		imsto.close()
		#return [data]
		fd = open(dst_file,'r')
		return environ['wsgi.file_wrapper'](fd, 4096)
		
	return not_found(environ, start_response)
Ejemplo n.º 4
0
def manage(environ, start_response):
    path_info = environ.get('PATH_INFO', '')

    man_regex = r'^/([A-Za-z]+)/(env|Gallery|Stored)'
    match = re.search(man_regex, path_info)
    #print('match: {0}'.format(match))
    if match is None:
        return not_found(environ, start_response)

    action = match.groups()[1]
    if (action == 'Gallery'):
        from cgi import FieldStorage
        form = FieldStorage(environ=environ)
        limit = 20
        start = 0
        if form.has_key("page") and form["page"].value != "":
            page = int(form["page"].value)
            if page < 1:
                page = 1
            start = limit * (page - 1)

        start_response('200 OK', [('Content-type', 'text/plain')])

        imsto = ImSto()
        gallery = imsto.browse(limit, start)
        import datetime
        dthandler = lambda obj: obj.isoformat() if isinstance(
            obj, datetime.datetime) else None
        if hasattr(imsto, 'close'):
            imsto.close()
        return [json.dumps(gallery, default=dthandler)]
    elif (action == 'Stored'):
        return stored_process(environ, start_response)
        #start_response('200 OK', [('Content-type', 'text/plain')])
        #return ['Stored']
    elif (action == 'env'):
        from _respond import print_env
        return print_env(environ, start_response)

    start_response('200 OK', [('Content-type', 'text/plain')])
    return [path_info]
Ejemplo n.º 5
0
def manage(environ, start_response):
	path_info = environ.get('PATH_INFO', '')
	
	man_regex = r'^/([A-Za-z]+)/(env|Gallery|Stored)'
	match = re.search(man_regex, path_info)
	#print('match: {0}'.format(match))
	if match is None:
		return not_found(environ, start_response)
	
	action = match.groups()[1]
	if (action == 'Gallery'):
		from cgi import FieldStorage
		form = FieldStorage(environ=environ)
		limit = 20
		start = 0
		if form.has_key("page") and form["page"].value != "":
			page = int(form["page"].value)
			if page < 1:
				page = 1
			start = limit * (page - 1)
		
		start_response('200 OK', [('Content-type', 'text/plain')])
		
		imsto = ImSto()
		gallery = imsto.browse(limit, start)
		import datetime
		dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
		if hasattr(imsto, 'close'):
			imsto.close()
		return [json.dumps(gallery, default=dthandler)]
	elif (action == 'Stored'):
		return stored_process(environ, start_response)
		#start_response('200 OK', [('Content-type', 'text/plain')])
		#return ['Stored']
	elif  (action == 'env'):
		from _respond import print_env
		return print_env(environ, start_response)
	
	start_response('200 OK', [('Content-type', 'text/plain')])
	return [path_info]