Ejemplo n.º 1
0
def finfo_from_database(fileInfo, srvObj, reqPropsObj):

    fileId = fileInfo.getFileId()
    fileVer = fileInfo.getFileVersion()

    # Locate the file best suiting the query and send it back if possible.
    location, _, ipAddress, port, mountPoint, filename, fileId,\
              fileVersion, mimeType =\
              ngamsFileUtils.locateArchiveFile(srvObj, fileId,
                                               fileVersion=fileVer,
                                               reqPropsObj=reqPropsObj)

    basename = os.path.basename(filename)

    if location == NGAMS_HOST_LOCAL:
        absname = os.path.normpath(os.path.join(mountPoint, filename))
        size = getFileSize(absname)
        opener = fopener(absname)
    elif location == NGAMS_HOST_CLUSTER or location == NGAMS_HOST_REMOTE:
        size = srvObj.getDb().getFileSize(fileId, fileVersion)
        opener = http_opener(ipAddress, port, fileId, fileVersion, srvObj)
    else:
        raise Exception("Unknown location type: %s" % (location, ))

    return ngamsMIMEMultipart.file_info(mimeType, basename, size, opener)
Ejemplo n.º 2
0
    def test_FileInfoReader(self):

        size = random.randint(10, 100)
        fd, name = tempfile.mkstemp('.bin', dir='tmp')
        with os.fdopen(fd, 'wb') as f:
            f.write(b' ' * size)

        finfo = ngamsMIMEMultipart.file_info('application/octet-stream', name, size, lambda: open(name, 'rb'))
        reader = ngamsMIMEMultipart.FileReader(finfo)
        rlen = len(reader)

        message = StringIO.StringIO()
        rfunc = functools.partial(reader.read, 65536)
        for buf in iter(rfunc, ''):
            message.write(buf)
        message = message.getvalue()
        mlen = len(message)

        self.assertEqual(mlen, rlen)
Ejemplo n.º 3
0
def finfo_from_database(fileInfo, srvObj, reqPropsObj):

    fileId = fileInfo.getFileId()
    fileVer = fileInfo.getFileVersion()

    # Locate the file best suiting the query and send it back if possible.
    location, _, ipAddress, port, mountPoint, filename, fileVersion, mimeType = \
       ngamsFileUtils.quickFileLocate(srvObj, reqPropsObj, fileId, fileVersion=fileVer)

    basename = os.path.basename(filename)

    if location == NGAMS_HOST_LOCAL:
        absname = os.path.normpath(os.path.join(mountPoint, filename))
        size = getFileSize(absname)
        opener = fopener(absname)
    elif location == NGAMS_HOST_CLUSTER or location == NGAMS_HOST_REMOTE:
        # TODO: int in python2 guarantees at least 32 bits, so we may overflow here
        size = int(srvObj.getDb().getFileSize(fileId, fileVersion))
        opener = http_opener(ipAddress, port, fileId, fileVersion, srvObj)
    else:
        raise Exception("Unknown location type: %s" % (location,))

    return ngamsMIMEMultipart.file_info(mimeType, basename, size, opener)