def _getDexterityFile(self, obj, field):
     self.request.response.setHeader('Accept-Ranges', 'bytes')
     self.request.response.setHeader("Content-Length", field.getSize())
     self.request.response.setHeader('Content-Type', field.contentType)
     request_range = handleRequestRange(obj, field.getSize(), self.request,
                                        self.request.response)
     return BlobStreamIterator(field, **request_range)
示例#2
0
    def get_logo(self, disposition='inline', headers=True):
        portal = getToolByName(self.context, 'portal_url').getPortalObject()
        customstyles_util = CustomStylesUtility(portal)
        customstyles = customstyles_util.annotations.get(
            'customstyles', OOBTree(DEFAULT_STYLES))

        REQUEST = self.request
        RESPONSE = REQUEST.RESPONSE
        blob = LOGO_KEY in customstyles and customstyles[LOGO_RIGHT_KEY] or None
        if not blob:
            return ''
        length = blob.get_size()
        if headers:
            RESPONSE.setHeader('Last-Modified',
                               rfc1123_date(self.context._p_mtime))
            RESPONSE.setHeader('Content-Type', blob.getContentType())
            RESPONSE.setHeader('Accept-Ranges', 'bytes')

            if handleIfModifiedSince(self.context, REQUEST, RESPONSE):
                return ''
            RESPONSE.setHeader('Content-Length', length)
            filename = blob.getFilename()
            if filename is not None:
                filename = IUserPreferredFileNameNormalizer(REQUEST).normalize(
                    unicode(filename, self.context.getCharset()))
                header_value = contentDispositionHeader(
                    disposition=disposition,
                    filename=filename)
                RESPONSE.setHeader("Content-disposition", header_value)

        range = handleRequestRange(self.context, length, REQUEST, RESPONSE)

        return blob.getIterator(**range)
示例#3
0
    def __call__(self):
        sm = getSecurityManager()
        if not sm.checkPermission(permissions.View, self.context.context):
            raise Unauthorized

        settings = self.context.settings
        filepath = self.context.filepath
        blob = settings.blob_files[filepath]
        blobfi = openBlob(blob)
        length = os.fstat(blobfi.fileno()).st_size
        blobfi.close()
        ext = os.path.splitext(os.path.normcase(filepath))[1][1:]
        if ext == 'txt':
            ct = 'text/plain'
        else:
            ct = 'image/%s' % ext

        self.request.response.setHeader('Last-Modified',
                                        rfc1123_date(self.context._p_mtime))
        self.request.response.setHeader('Accept-Ranges', 'bytes')
        self.request.response.setHeader("Content-Length", length)
        self.request.response.setHeader('Content-Type', ct)
        request_range = handleRequestRange(self.context, length, self.request,
                                           self.request.response)
        return BlobStreamIterator(blob, **request_range)
    def __call__(self):
        sm = getSecurityManager()
        if not sm.checkPermission(permissions.View, self.context.context):
            raise Unauthorized

        settings = self.context.settings
        filepath = self.context.filepath
        blob = settings.blob_files[filepath]
        blobfi = openBlob(blob)
        length = os.fstat(blobfi.fileno()).st_size
        blobfi.close()
        ext = os.path.splitext(os.path.normcase(filepath))[1][1:]
        if ext == 'txt':
            ct = 'text/plain'
        else:
            ct = 'image/%s' % ext

        self.request.response.setHeader('Last-Modified',
                                        rfc1123_date(self.context._p_mtime))
        self.request.response.setHeader('Accept-Ranges', 'bytes')
        self.request.response.setHeader("Content-Length", length)
        self.request.response.setHeader('Content-Type', ct)
        request_range = handleRequestRange(
            self.context, length, self.request, self.request.response)
        return BlobStreamIterator(blob, **request_range)
示例#5
0
    def render_attachment_preview(self, attachment):
        sm = getSecurityManager()
        if not sm.checkPermission(permissions.View, self.context):
            raise Unauthorized

        r = self.request.response
        settings = Settings(attachment)

        if self.preview_type not in ('large', 'normal', 'small'):
            self.preview_type = 'small'
        if self.page is None:
            self.page = 1
        filepath = u'%s/dump_%s.%s' % (self.preview_type, self.page,
                                       settings.pdf_image_format)
        blob = settings.blob_files[filepath]
        blobfi = openBlob(blob)
        length = os.fstat(blobfi.fileno()).st_size
        blobfi.close()
        ext = os.path.splitext(os.path.normcase(filepath))[1][1:]
        if ext == 'txt':
            ct = 'text/plain'
        else:
            ct = 'image/%s' % ext

        r.setHeader('Content-Type', ct)
        r.setHeader('Last-Modified', rfc1123_date(self.context._p_mtime))
        r.setHeader('Accept-Ranges', 'bytes')
        r.setHeader("Content-Length", length)
        request_range = handleRequestRange(self.context, length, self.request,
                                           self.request.response)
        return BlobStreamIterator(blob, **request_range)
    def get_logo(self, disposition='inline', headers=True):
        portal = getToolByName(self.context, 'portal_url').getPortalObject()
        customstyles_util = CustomStylesUtility(portal)
        customstyles = customstyles_util.annotations.get(
            'customstyles', OOBTree(DEFAULT_STYLES))

        REQUEST = self.request
        RESPONSE = REQUEST.RESPONSE
        blob = LOGO_KEY in customstyles and customstyles[LOGO_RIGHT_KEY] or None
        if not blob:
            return ''
        length = blob.get_size()
        if headers:
            RESPONSE.setHeader('Last-Modified',
                               rfc1123_date(self.context._p_mtime))
            RESPONSE.setHeader('Content-Type', blob.getContentType())
            RESPONSE.setHeader('Accept-Ranges', 'bytes')

            if handleIfModifiedSince(self.context, REQUEST, RESPONSE):
                return ''
            RESPONSE.setHeader('Content-Length', length)
            filename = blob.getFilename()
            if filename is not None:
                filename = IUserPreferredFileNameNormalizer(REQUEST).normalize(
                    unicode(filename, self.context.getCharset()))
                header_value = contentDispositionHeader(
                    disposition=disposition, filename=filename)
                RESPONSE.setHeader("Content-disposition", header_value)

        range = handleRequestRange(self.context, length, REQUEST, RESPONSE)

        return blob.getIterator(**range)
示例#7
0
    def index_html(self, REQUEST=None, RESPONSE=None, charset='utf-8', disposition='inline'):
        """ make it directly viewable when entering the objects URL """

        if REQUEST is None:
            REQUEST = self.REQUEST

        if RESPONSE is None:
            RESPONSE = REQUEST.RESPONSE

        RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
        RESPONSE.setHeader('Content-Type', self.getContentType())
        RESPONSE.setHeader('Accept-Ranges', 'bytes')

        if handleIfModifiedSince(self, REQUEST, RESPONSE):
            return ''

        length = self.get_size()
        RESPONSE.setHeader('Content-Length', length)

        filename = self.getFilename()
        if filename is not None:
            if not isinstance(filename, unicode):
                filename = unicode(filename, charset)
            filename = IUserPreferredFileNameNormalizer(REQUEST).normalize(
                filename)
            header_value = contentDispositionHeader(
                disposition=disposition,
                filename=filename)
            RESPONSE.setHeader("Content-disposition", header_value)

        request_range = handleRequestRange(self, length, REQUEST, RESPONSE)
        return self.getIterator(**request_range)
示例#8
0
def index_html(self,
               instance,
               REQUEST=None,
               RESPONSE=None,
               disposition='inline'):
    """ make it directly viewable when entering the objects URL """
    if REQUEST is None:
        REQUEST = instance.REQUEST
    if RESPONSE is None:
        RESPONSE = REQUEST.RESPONSE
    blob = self.getUnwrapped(instance, raw=True)  # TODO: why 'raw'?
    RESPONSE.setHeader('Last-Modified', rfc1123_date(instance._p_mtime))
    RESPONSE.setHeader('Content-Type', self.getContentType(instance))
    # The only change is to comment out this header:
    #RESPONSE.setHeader('Accept-Ranges', 'bytes')
    if handleIfModifiedSince(instance, REQUEST, RESPONSE):
        return ''
    length = blob.get_size()
    RESPONSE.setHeader('Content-Length', length)
    filename = self.getFilename(instance)
    if filename is not None:
        filename = IUserPreferredFileNameNormalizer(REQUEST).normalize(
            unicode(filename, instance.getCharset()))
        header_value = contentDispositionHeader(disposition=disposition,
                                                filename=filename)
        RESPONSE.setHeader("Content-disposition", header_value)
    range = handleRequestRange(instance, length, REQUEST, RESPONSE)
    return blob.getIterator(**range)
示例#9
0
    def render_attachment_preview(self, attachment):
        sm = getSecurityManager()
        if not sm.checkPermission(permissions.View, self.context):
            raise Unauthorized

        r = self.request.response
        settings = Settings(attachment)

        if self.preview_type not in ("large", "normal", "small"):
            self.preview_type = "small"
        if self.page is None:
            self.page = 1
        filepath = u"%s/dump_%s.%s" % (self.preview_type, self.page, settings.pdf_image_format)
        blob = settings.blob_files[filepath]
        blobfi = openBlob(blob)
        length = os.fstat(blobfi.fileno()).st_size
        blobfi.close()
        ext = os.path.splitext(os.path.normcase(filepath))[1][1:]
        if ext == "txt":
            ct = "text/plain"
        else:
            ct = "image/%s" % ext

        r.setHeader("Content-Type", ct)
        r.setHeader("Last-Modified", rfc1123_date(self.context._p_mtime))
        r.setHeader("Accept-Ranges", "bytes")
        r.setHeader("Content-Length", length)
        request_range = handleRequestRange(self.context, length, self.request, self.request.response)
        return BlobStreamIterator(blob, **request_range)
示例#10
0
    def __call__(self):
        data = self.get_data()
        if data:
            is_blob = False
            if isinstance(data, basestring):
                length = len(data)
            else:
                is_blob = True
                blobfi = openBlob(data)
                length = fstat(blobfi.fileno()).st_size
                blobfi.close()

            self.request.response.setHeader(
                'Last-Modified', rfc1123_date(self.context._p_mtime))
            resp = self.request.response
            resp.setHeader(
                'Content-Disposition', 'inline; filename=%s.%s' %
                (self.context.getId(), self.file_ext))
            resp.setHeader("Content-Length", length)
            resp.setHeader('Content-Type', self.content_type)

            if is_blob:
                resp.setHeader('Accept-Ranges', 'bytes')
                range = handleRequestRange(self.context, length, self.request,
                                           self.request.response)
                return BlobStreamIterator(data, **range)
            else:
                return data
        else:
            raise NotFound
示例#11
0
def index_html(self, instance, REQUEST=None, RESPONSE=None, disposition='inline'):
    """ make it directly viewable when entering the objects URL """
    if REQUEST is None:
        REQUEST = instance.REQUEST
    if RESPONSE is None:
        RESPONSE = REQUEST.RESPONSE
    blob = self.getUnwrapped(instance, raw=True)    # TODO: why 'raw'?
    RESPONSE.setHeader('Last-Modified', rfc1123_date(instance._p_mtime))
    RESPONSE.setHeader('Content-Type', self.getContentType(instance))
    # The only change is to comment out this header:
    #RESPONSE.setHeader('Accept-Ranges', 'bytes')
    if handleIfModifiedSince(instance, REQUEST, RESPONSE):
        return ''
    length = blob.get_size()
    RESPONSE.setHeader('Content-Length', length)
    filename = self.getFilename(instance)
    if filename is not None:
        filename = IUserPreferredFileNameNormalizer(REQUEST).normalize(
            unicode(filename, instance.getCharset()))
        header_value = contentDispositionHeader(
            disposition=disposition,
            filename=filename)
        RESPONSE.setHeader("Content-disposition", header_value)
    range = handleRequestRange(instance, length, REQUEST, RESPONSE)
    return blob.getIterator(**range)
示例#12
0
    def serve_file(self):
        file = self._getFile()
        self.set_headers(file)
        if not INamedBlobFile.providedBy(file):
            return super(Download, self).__call__()

        request_range = handleRequestRange(self.context, file.getSize(),
                                           self.request, self.request.response)
        return BlobStreamIterator(file._blob, **request_range)
 def _getDexterityFile(self, obj, field):
     self.request.response.setHeader('Accept-Ranges', 'bytes')
     self.request.response.setHeader("Content-Length", field.getSize())
     self.request.response.setHeader('Content-Type', field.contentType)
     request_range = handleRequestRange(
         obj,
         field.getSize(),
         self.request,
         self.request.response
         )
     return BlobStreamIterator(field, **request_range)
示例#14
0
    def index_html(self, instance, REQUEST=None, RESPONSE=None,
                   charset='utf-8', disposition='inline'):
        """Kicks download.
        Writes data including file name and content type to RESPONSE
        """

        if REQUEST is None:
            REQUEST = instance.REQUEST

        if RESPONSE is None:
            RESPONSE = REQUEST.RESPONSE

        RESPONSE.setHeader('Last-Modified', rfc1123_date(instance._p_mtime))
        RESPONSE.setHeader('Content-Type', self.getContentType(instance))
        RESPONSE.setHeader('Accept-Ranges', 'bytes')

        if handleIfModifiedSince(instance, REQUEST, RESPONSE):
            return ''

        length = self.get_size(instance)
        RESPONSE.setHeader('Content-Length', length)

        filename = self.getFilename(instance)
        if filename is not None:
            if isinstance(filename, unicode):
                filename = filename.encode(charset, errors="ignore")

            # Create a user agent specific disposition header
            # IE needs an url quoted filename
            # Other browsers need an unquoted filename
            user_agent = REQUEST.get('HTTP_USER_AGENT', '')
            if 'MSIE' in user_agent:
                header_value = '%s; filename=%s' % (disposition,
                                                    quote(filename))
            else:
                header_value = '%s; filename="%s"' % (disposition, filename)
            RESPONSE.setHeader("Content-disposition", header_value)

        request_range = handleRequestRange(instance, length, REQUEST, RESPONSE)

        # Notify file downloads, but do not notify range requests
        if not ('start' in request_range and request_range['start'] > 0):
            portal_state = getMultiAdapter((instance, instance.REQUEST),
                                           name='plone_portal_state')
            if not portal_state.anonymous():
                registry = getUtility(IRegistry)
                user_ids = registry['ftw.file.filesettings.user_ids']
                if portal_state.member().id \
                        and not portal_state.member().id in user_ids:
                    notify(FileDownloadedEvent(instance, filename))

        return self.get(instance).getIterator(**request_range)
示例#15
0
def index_html(self,
               REQUEST=None,
               RESPONSE=None,
               charset='utf-8',
               disposition='inline'):
    """ make it directly viewable when entering the objects URL """

    if REQUEST is None:
        REQUEST = self.REQUEST

    if RESPONSE is None:
        RESPONSE = REQUEST.RESPONSE

    RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
    RESPONSE.setHeader('Content-Type', self.getContentType())
    RESPONSE.setHeader('Accept-Ranges', 'bytes')

    if handleIfModifiedSince(self, REQUEST, RESPONSE):
        return ''

    length = self.get_size()
    RESPONSE.setHeader('Content-Length', length)

    filename = self.getFilename()
    if filename is not None:
        if REQUEST.HTTP_USER_AGENT.find('MSIE') != -1:
            if isinstance(filename, unicode):
                filename = filename.encode('gb18030')
            else:
                filename = unicode(filename, charset, errors="ignore")
                filename = filename.encode('gb18030')
            header_value = contentDispositionHeader(disposition,
                                                    'gb18030',
                                                    filename=filename)
        else:
            if not isinstance(filename, unicode):
                filename = unicode(filename, charset, errors="ignore")

#            filename = IUserPreferredFileNameNormalizer(REQUEST).normalize(
#                filename)
            header_value = contentDispositionHeader(disposition=disposition,
                                                    filename=filename)
        RESPONSE.setHeader("Content-disposition", header_value)

    request_range = handleRequestRange(self, length, REQUEST, RESPONSE)
    return self.getIterator(**request_range)
示例#16
0
    def render_attachment_preview(self, attachment):
        sm = getSecurityManager()
        if not sm.checkPermission(permissions.View, self.context):
            raise Unauthorized

        r = self.request.response

        # avoid long dreaded CSRF error
        annotations = IAnnotations(attachment)
        if not annotations.get('collective.documentviewer', None):
            safeWrite(attachment)
        settings = Settings(attachment)  # possibly creates annotation

        if self.preview_type not in ('large', 'normal', 'small'):
            self.preview_type = 'small'
        if self.page is None:
            self.page = 1
        filepath = u'%s/dump_%s.%s' % (self.preview_type, self.page,
                                       settings.pdf_image_format)
        try:
            blob = settings.blob_files[filepath]
        except TypeError:
            # 'NoneType' object has no attribute '__getitem__'
            # happens e.g. when missing preview for stream attachment
            return

        blobfi = openBlob(blob)
        length = os.fstat(blobfi.fileno()).st_size
        blobfi.close()
        ext = os.path.splitext(os.path.normcase(filepath))[1][1:]
        if ext == 'txt':
            ct = 'text/plain'
        else:
            ct = 'image/%s' % ext

        r.setHeader('Content-Type', ct)
        r.setHeader('Last-Modified', rfc1123_date(self.context._p_mtime))
        r.setHeader('Accept-Ranges', 'bytes')
        r.setHeader("Content-Length", length)
        request_range = handleRequestRange(self.context, length, self.request,
                                           self.request.response)
        return BlobStreamIterator(blob, **request_range)
示例#17
0
    def render_blob_version(self):
        # done much like it is done in plone.app.blob's index_html
        header_value = contentDispositionHeader(
            disposition='inline',
            filename=self.context.getFilename().replace('.pdf', '.swf'))

        blob = self.settings.data
        blobfi = openBlob(blob)
        length = fstat(blobfi.fileno()).st_size
        blobfi.close()

        self.request.response.setHeader('Last-Modified',
                                        rfc1123_date(self.context._p_mtime))
        self.request.response.setHeader('Accept-Ranges', 'bytes')
        self.request.response.setHeader('Content-Disposition', header_value)
        self.request.response.setHeader("Content-Length", length)
        self.request.response.setHeader('Content-Type',
                                        'application/x-shockwave-flash')
        range = handleRequestRange(self.context, length, self.request,
                                   self.request.response)
        return BlobStreamIterator(blob, **range)
示例#18
0
    def render_blob_version(self):
        # done much like it is done in plone.app.blob's index_html
        header_value = contentDispositionHeader(
            disposition='inline',
            filename=self.context.getFilename().replace('.pdf', '.swf'))

        blob = self.settings.data
        blobfi = openBlob(blob)
        length = fstat(blobfi.fileno()).st_size
        blobfi.close()

        self.request.response.setHeader('Last-Modified',
            rfc1123_date(self.context._p_mtime))
        self.request.response.setHeader('Accept-Ranges', 'bytes')
        self.request.response.setHeader('Content-Disposition', header_value)
        self.request.response.setHeader("Content-Length", length)
        self.request.response.setHeader('Content-Type',
            'application/x-shockwave-flash')
        range = handleRequestRange(self.context, length, self.request,
            self.request.response)
        return BlobStreamIterator(blob, **range)
示例#19
0
    def index_html(self, REQUEST=None, RESPONSE=None, charset='utf-8',
                   disposition='inline'):
        """ make it directly viewable when entering the objects URL """

        if REQUEST is None:
            REQUEST = self.REQUEST

        if RESPONSE is None:
            RESPONSE = REQUEST.RESPONSE

        RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
        RESPONSE.setHeader('Content-Type', self.getContentType())
        RESPONSE.setHeader('Accept-Ranges', 'bytes')

        if handleIfModifiedSince(self, REQUEST, RESPONSE):
            return ''

        length = self.get_size()
        RESPONSE.setHeader('Content-Length', length)

        filename = self.getFilename()
        if filename is not None:
            if not isinstance(filename, six.text_type):
                filename = six.text_type(filename, charset, errors='ignore')
            filename = IUserPreferredFileNameNormalizer(
                REQUEST,
            ).normalize(
                filename,
            )
            header_value = contentDispositionHeader(
                disposition=disposition,
                filename=filename,
            )
            # Add original filename in utf-8, ref to rfc2231
            RESPONSE.setHeader('Content-disposition', header_value)

        request_range = handleRequestRange(self, length, REQUEST, RESPONSE)
        return self.getIterator(**request_range)
示例#20
0
文件: files.py 项目: sm2x/castle.cms
    def __call__(self):
        try:
            data = self.get_data()
        except IOError:
            # can be from zeo client blob file weirdness with PIL
            # ocassionally
            logger.info('Could not get blob data', exc_info=True)
            raise NotFound

        if data:
            is_blob = False
            if isinstance(data, basestring):
                length = len(data)
            else:
                is_blob = True
                blobfi = openBlob(data)
                length = fstat(blobfi.fileno()).st_size
                blobfi.close()

            self.request.response.setHeader(
                'Last-Modified', rfc1123_date(self.context._p_mtime))
            resp = self.request.response
            resp.setHeader(
                'Content-Disposition', 'inline; filename=%s.%s' %
                (self.context.getId(), self.file_ext))
            resp.setHeader("Content-Length", length)
            resp.setHeader('Content-Type', self.content_type)

            if is_blob:
                resp.setHeader('Accept-Ranges', 'bytes')
                range = handleRequestRange(self.context, length, self.request,
                                           self.request.response)
                return BlobStreamIterator(data, **range)
            else:
                return data
        else:
            raise NotFound