Ejemplo n.º 1
0
def main(argv=None):
	if argv is None:
		argv = sys.argv
	
	try:
		try:
			opts, args = getopt.getopt(argv[1:], "hi:q:lt:v", ["help", "import=", "id=", "list", "test", "verbose", "limit=", "start="])
		except getopt.error, msg:
			raise Usage(msg)
		
		#print(opts)
		#print(args)
		action = None
		store_file = None
		# option processing
		for option, value in opts:
			if option == "-v":
				verbose = True
			if option in ("-h", "--help"):
				raise Usage(help_message)
			if option in ("-i", "--import"):
				store_file = value
				print('store file: {0}'.format(store_file))
				action = 'import'
			elif option in ("-l", "--list"):
				action = 'list'
			elif option in ("-t", "--test"):
				action = 'test'
				filename = value
			elif option in ("-q", "--id"):
				action = 'get'
				id = value
			else:
				pass
		
		print('action: {}'.format(action))
		if (action == 'list'):
			imsto = ImSto()
			gallery = imsto.browse()
			for img in gallery['items']:
				#print(img)
				print("{0[filename]}\t{0[length]:8,d}".format(img))
			return 0
		elif (action == 'get') and id is not None:
			imsto = ImSto()
			if not imsto.getFs().exists(id):
				print ('not found')
				return 1
			gf = imsto.get(id)
			#print(gf)
			print ("found: {0.name}\t{0.length}".format(gf))
			return 0
		elif (action == 'test'):
			print('filename: %r' % filename)
			fp = open(filename, 'rb')
			h = fp.read(32)
			print(getImageType(h))
			return 0
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 processFile(id, filename):
	from store import ImSto
	imsto = ImSto()
	file = imsto.get(id)
	if file is None:
		imsto.close()
		return False
	save_file(file, filename)
	imsto.close()
	return True
Ejemplo n.º 4
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.º 5
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    try:
        try:
            opts, args = getopt.getopt(argv[1:], "hi:q:lt:v", [
                "help", "import=", "id=", "list", "test", "verbose", "limit=",
                "start="
            ])
        except getopt.error, msg:
            raise Usage(msg)

        #print(opts)
        #print(args)
        action = None
        store_file = None
        # option processing
        for option, value in opts:
            if option == "-v":
                verbose = True
            if option in ("-h", "--help"):
                raise Usage(help_message)
            if option in ("-i", "--import"):
                store_file = value
                print('store file: {0}'.format(store_file))
                action = 'import'
            elif option in ("-l", "--list"):
                action = 'list'
            elif option in ("-t", "--test"):
                action = 'test'
                filename = value
            elif option in ("-q", "--id"):
                action = 'get'
                id = value
            else:
                pass

        print('action: {}'.format(action))
        if (action == 'list'):
            imsto = ImSto()
            gallery = imsto.browse()
            for img in gallery['items']:
                #print(img)
                print("{0[filename]}\t{0[length]:8,d}".format(img))
            return 0
        elif (action == 'get') and id is not None:
            imsto = ImSto()
            if not imsto.getFs().exists(id):
                print('not found')
                return 1
            gf = imsto.get(id)
            #print(gf)
            print("found: {0.name}\t{0.length}".format(gf))
            return 0
        elif (action == 'test'):
            print('filename: %r' % filename)
            fp = open(filename, 'rb')
            h = fp.read(32)
            print(getImageType(h))
            return 0