def PUT(self, map_name, ws_name, st_type, st_name, f_type, format): import zipfile mf, ws = get_mapfile_workspace(map_name, ws_name) # TODO: According to geoserv's examples we might have to handle # directories as well as files, in that case we want to upload # all the files from the directory. # Lets first try to get the file. if f_type == "file": # Check if zip or not... data = web.data() elif f_type == "url": raise NotImplemented() elif f_type == "external": raise NotImplemented() # Now we make sure the store exists. with webapp.mightNotFound(message="Store {exception} does not exist " "and automatic creation is not yet suported."): ws.get_store_info(st_type, st_name) # TODO: Create the store if it does not exist. # Then we store the file. ext = web.ctx.env.get('CONTENT_TYPE', '').split("/")[-1] path = tools.mk_st_data_path(ws_name, st_type, st_name, st_name + (".%s" % ext) if ext else "") with open(path, "w") as f: f.write(data) # We also unzip it if its ziped. ctype = web.ctx.env.get('CONTENT_TYPE', None) if ctype == "application/zip": z = zipfile.ZipFile(path) for f in z.namelist(): fp = tools.mk_st_data_path(ws_name, st_type, st_name, f) # If the file has the correct target we might want it. if format and fp.endswith(format) and not tools.is_hidden(fp): path = fp z.extract(f, path=tools.get_st_data_path(ws_name, st_type, st_name)) # Set new connection parameters: ws.update_store(st_type, st_name, {"connectionParameters":{"url":"file:"+tools.no_res_root(path)}}) ws.save() # Finally we might have to configure it. params = web.input(configure="none") if params.configure == "first": raise NotImplemented() elif params.configure == "none": pass elif params.configure == "all": raise NotImplemented() else: raise webapp.BadRequest(message="configure must be one of first, none or all.")
def PUT(self, ws_name, st_type, st_name, f_type, format): """Uploads a file from a local source. The body of the request is the file itself.""" import zipfile # TODO: According to geoserv's examples we might have to handle # directories as well as files, in that case we want to upload # all the files from the directory. # Lets first try to get the file. if f_type == "file": # Check if zip or not... data = web.data() elif f_type == "url": raise webapp.NotImplemented() elif f_type == "external": raise webapp.NotImplemented() ws = get_workspace(ws_name) # Now we make sure the store exists. try: ws.get_store_info(st_type, st_name) except KeyError: # Create the store if it seems legit and it does not exist. if st_type == "datastores" or st_type == "coveragestores": st_type = st_type[:-1] # Remove trailing 's' with webapp.mightConflict("Workspace", workspace=ws_name): ws.create_store(st_type, st_name, {}) # Finaly check if its OK now. with webapp.mightNotFound(message="Store {exception} does not exist " "and it could not be created."): ws.get_store_info(st_type, st_name) # Then we store the file. ext = web.ctx.env.get('CONTENT_TYPE', '').split("/")[-1] path = mra.create_file(st_name + (".%s" % ext) if ext else "", data=data) dest = os.path.join(os.path.split(path)[0], st_name) # We also unzip it if its ziped. ctype = web.ctx.env.get('CONTENT_TYPE', None) if ctype == "application/zip": z = zipfile.ZipFile(path) for f in z.namelist(): fp = mra.mk_path(mra.get_file_path(st_name, f)) # If the file has the correct target we might want it. if format and fp.endswith(format) and not tools.is_hidden(fp): path = fp z.extract(f, path=dest) # Set new connection parameters: ws.update_store(st_type, st_name, {"connectionParameters":{"url":"file:"+mra.pub_file_path(path)}}) ws.save() # Finally we might have to configure it. params = web.input(configure="none") if params.configure == "first": raise webapp.NotImplemented() elif params.configure == "none": pass elif params.configure == "all": raise webapp.NotImplemented() else: raise webapp.BadRequest(message="configure must be one of 'first', 'none' or 'all'.")