Beispiel #1
0
def _logRestRequest(resource, path, params):
    auditLogger.info('rest.request', extra={
        'details': {
            'method': cherrypy.request.method.upper(),
            'route': (getattr(resource, 'resourceName', resource.__class__.__name__),) + path,
            'params': params,
            'status': cherrypy.response.status or 200
        }
    })
Beispiel #2
0
def _logRestRequest(resource, path, params):
    auditLogger.info('rest.request', extra={
        'details': {
            'method': cherrypy.request.method.upper(),
            'route': (getattr(resource, 'resourceName', resource.__class__.__name__),) + path,
            'params': params,
            'status': cherrypy.response.status or 200
        }
    })
Beispiel #3
0
    def download(self,
                 file,
                 offset=0,
                 headers=True,
                 endByte=None,
                 contentDisposition=None,
                 extraParameters=None):
        """
        Use the appropriate assetstore adapter for whatever assetstore the
        file is stored in, and call downloadFile on it. If the file is a link
        file rather than a file in an assetstore, we redirect to it.

        :param file: The file to download.
        :param offset: The start byte within the file.
        :type offset: int
        :param headers: Whether to set headers (i.e. is this an HTTP request
            for a single file, or something else).
        :type headers: bool
        :param endByte: Final byte to download. If ``None``, downloads to the
            end of the file.
        :type endByte: int or None
        :param contentDisposition: Content-Disposition response header
            disposition-type value.
        :type contentDisposition: str or None
        :type extraParameters: str or None
        """
        events.trigger('model.file.download.request',
                       info={
                           'file': file,
                           'startByte': offset,
                           'endByte': endByte
                       })

        auditLogger.info('file.download',
                         extra={
                             'details': {
                                 'fileId': file['_id'],
                                 'startByte': offset,
                                 'endByte': endByte,
                                 'extraParameters': extraParameters
                             }
                         })

        if file.get('assetstoreId'):
            try:
                fileDownload = self.getAssetstoreAdapter(file).downloadFile(
                    file,
                    offset=offset,
                    headers=headers,
                    endByte=endByte,
                    contentDisposition=contentDisposition,
                    extraParameters=extraParameters)

                def downloadGenerator():
                    for data in fileDownload():
                        yield data
                    if endByte is None or endByte >= file['size']:
                        events.trigger('model.file.download.complete',
                                       info={
                                           'file': file,
                                           'startByte': offset,
                                           'endByte': endByte,
                                           'redirect': False
                                       })

                return downloadGenerator
            except cherrypy.HTTPRedirect:
                events.trigger('model.file.download.complete',
                               info={
                                   'file': file,
                                   'startByte': offset,
                                   'endByte': endByte,
                                   'redirect': True
                               })
                raise
        elif file.get('linkUrl'):
            if headers:
                events.trigger('model.file.download.complete',
                               info={
                                   'file': file,
                                   'startByte': offset,
                                   'endByte': endByte,
                                   'redirect': True
                               })
                raise cherrypy.HTTPRedirect(file['linkUrl'])
            else:
                endByte = endByte or len(file['linkUrl'])

                def stream():
                    yield file['linkUrl'][offset:endByte]
                    if endByte >= len(file['linkUrl']):
                        events.trigger('model.file.download.complete',
                                       info={
                                           'file': file,
                                           'startByte': offset,
                                           'endByte': endByte,
                                           'redirect': False
                                       })

                return stream
        else:
            raise Exception('File has no known download mechanism.')
Beispiel #4
0
    def download(self, file, offset=0, headers=True, endByte=None,
                 contentDisposition=None, extraParameters=None):
        """
        Use the appropriate assetstore adapter for whatever assetstore the
        file is stored in, and call downloadFile on it. If the file is a link
        file rather than a file in an assetstore, we redirect to it.

        :param file: The file to download.
        :param offset: The start byte within the file.
        :type offset: int
        :param headers: Whether to set headers (i.e. is this an HTTP request
            for a single file, or something else).
        :type headers: bool
        :param endByte: Final byte to download. If ``None``, downloads to the
            end of the file.
        :type endByte: int or None
        :param contentDisposition: Content-Disposition response header
            disposition-type value.
        :type contentDisposition: str or None
        :type extraParameters: str or None
        """
        events.trigger('model.file.download.request', info={
            'file': file,
            'startByte': offset,
            'endByte': endByte})

        auditLogger.info('file.download', extra={
            'details': {
                'fileId': file['_id'],
                'startByte': offset,
                'endByte': endByte,
                'extraParameters': extraParameters
            }
        })

        if file.get('assetstoreId'):
            try:
                fileDownload = self.getAssetstoreAdapter(file).downloadFile(
                    file, offset=offset, headers=headers, endByte=endByte,
                    contentDisposition=contentDisposition,
                    extraParameters=extraParameters)

                def downloadGenerator():
                    for data in fileDownload():
                        yield data
                    if endByte is None or endByte >= file['size']:
                        events.trigger('model.file.download.complete', info={
                            'file': file,
                            'startByte': offset,
                            'endByte': endByte,
                            'redirect': False})
                return downloadGenerator
            except cherrypy.HTTPRedirect:
                events.trigger('model.file.download.complete', info={
                    'file': file,
                    'startByte': offset,
                    'endByte': endByte,
                    'redirect': True})
                raise
        elif file.get('linkUrl'):
            if headers:
                events.trigger('model.file.download.complete', info={
                    'file': file,
                    'startByte': offset,
                    'endByte': endByte,
                    'redirect': True})
                raise cherrypy.HTTPRedirect(file['linkUrl'])
            else:
                endByte = endByte or len(file['linkUrl'])

                def stream():
                    yield file['linkUrl'][offset:endByte]
                    if endByte >= len(file['linkUrl']):
                        events.trigger('model.file.download.complete', info={
                            'file': file,
                            'startByte': offset,
                            'endByte': endByte,
                            'redirect': False})
                return stream
        else:
            raise Exception('File has no known download mechanism.')