def _handleCmdCRetrieve(srvObj, reqPropsObj, httpRef): """ Carry out the action of a CRETRIEVE command. srvObj: Reference to NG/AMS server class object (ngamsServer). reqPropsObj: Request Property object to keep track of actions done during the request handling (ngamsReqProps). httpRef: Reference to the HTTP request handler object (ngamsHttpRequestHandler). Returns: Void. """ # For data files, retrieval must be enabled otherwise the request is # rejected. if (not srvObj.getCfg().getAllowRetrieveReq()): errMsg = genLog("NGAMS_ER_ILL_REQ", ["Retrieve"]) raise Exception(errMsg) # We don't allow processing yet if 'processing' in reqPropsObj: raise Exception('CRETRIEVE command does not allow processing (yet)') # At least container_id or container_name must be specified containerName = containerId = None if reqPropsObj.hasHttpPar("container_id") and reqPropsObj.getHttpPar( "container_id").strip(): containerId = reqPropsObj.getHttpPar("container_id").strip() if not containerId and reqPropsObj.hasHttpPar( "container_name") and reqPropsObj.getHttpPar( "container_name").strip(): containerName = reqPropsObj.getHttpPar("container_name").strip() if not containerId and not containerName: errMsg = genLog("NGAMS_ER_RETRIEVE_CMD") raise Exception(errMsg) # If container_name is specified, and maps to more than one container, # an error is issued if not containerId: containerId = srvObj.getDb().getContainerIdForUniqueName(containerName) logger.debug("Handling request for file with containerId: %s", containerId) # Build the container hierarchy, get all file references and send back the results container = srvObj.getDb().readHierarchy(containerId, True) cinfo = cinfo_from_database(container, srvObj, reqPropsObj) reader = ngamsMIMEMultipart.ContainerReader(cinfo) # Send all the data back httpRef.send_data(reader, NGAMS_CONT_MT)
def _createMIMEMessage(self, onlyDirs): self._createDirectories() if not onlyDirs: self._createFiles() cinfo = ngamsMIMEMultipart.cinfo_from_filesystem('toplevel', 'application/octet-stream') bs = 65536 output = StringIO.StringIO() reader = ngamsMIMEMultipart.ContainerReader(cinfo) rfunc = functools.partial(reader.read, bs) for buf in iter(rfunc, ''): output.write(buf) message = output.getvalue() self.assertEqual(len(reader), len(message), "Message size calculated by the reader is wrong: %d != %d" % (len(reader), len(message))) return message
def _handleCmdCRetrieve(srvObj, reqPropsObj, httpRef): """ Carry out the action of a CRETRIEVE command. srvObj: Reference to NG/AMS server class object (ngamsServer). reqPropsObj: Request Property object to keep track of actions done during the request handling (ngamsReqProps). httpRef: Reference to the HTTP request handler object (ngamsHttpRequestHandler). Returns: Void. """ # For data files, retrieval must be enabled otherwise the request is # rejected. if (not srvObj.getCfg().getAllowRetrieveReq()): errMsg = genLog("NGAMS_ER_ILL_REQ", ["Retrieve"]) raise Exception(errMsg) # We don't allow processing yet if 'processing' in reqPropsObj: raise Exception('CRETRIEVE command does not allow processing (yet)') # Users can request a tarball instead of the default MIME multipart message return_tar = 'format' in reqPropsObj and reqPropsObj['format'] == 'application/x-tar' container_id = containers.get_container_id(reqPropsObj, srvObj.db) logger.debug("Handling request for file with containerId: %s", container_id) # Build the container hierarchy and get all file references container = srvObj.getDb().readHierarchy(container_id, True) cinfo = cinfo_from_database(container, srvObj, reqPropsObj) # Send all the data back, either as a multipart message or as a tarball if return_tar: httpRef.send_file_headers(cinfo.name, 'application/x-tar', tarsize_cinfo(cinfo)) send_toplevel_cinfo(cinfo, httpRef) else: reader = ngamsMIMEMultipart.ContainerReader(cinfo) httpRef.send_data(reader, NGAMS_CONT_MT)
def carchive(self, dirname, files_mtype): """ Sends a CARCHIVE command to the NG/AMS Server to archive a full hierarchy of files, effectively creating a hierarchy of containers with all the files within """ # If the dataRef is a directory, scan the directory # and build up a list of files contained directly within # Start preparing a mutipart MIME message that will contain # all of them dirname = os.path.abspath(dirname) logger.debug('Archiving directory %s as a container', dirname) # Recursively collect all files cinfo = ngamsMIMEMultipart.cinfo_from_filesystem(dirname, files_mtype) stream = ngamsMIMEMultipart.ContainerReader(cinfo) return self._post('CARCHIVE', NGAMS_CONT_MT, stream)