Modify

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#13202 closed defect (fixed)

AttributeError: 'FileContentStream' object has no attribute 'seek'

Reported by: Ryan J Ollos Owned by: Jun Omae
Priority: normal Component: ArchiveViewerPlugin
Severity: normal Keywords:
Cc: Trac Release: 1.0

Description

I get the following on Trac 1.0-stable and 1.2-stable:

12:14:49 Trac[api] WARNING: HTML preview using ZipRenderer with <RenderingContext <Resource u'repository:proj1, source:ArchiveViewer-0.1-py2.7.egg@132'>> failed: 
Traceback (most recent call last):
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/mimeview/api.py", line 814, in render
    rendered_content, filename, url)
  File "/Users/rjollos/Documents/Workspace/trac-dev/trac-hacks/archiveviewerplugin/1.0/archiveviewer/zip.py", line 125, in render
    zipfile = ZipFile(content.input)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 770, in __init__
    self._RealGetContents()
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 809, in _RealGetContents
    endrec = _EndRecData(fp)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 208, in _EndRecData
    fpin.seek(0, 2)
AttributeError: 'FileContentStream' object has no attribute 'seek'
$ svn --version
svn, version 1.9.5 (r1770682)
   compiled May 12 2017, 06:52:50 on x86_64-apple-darwin16.5.0
$ python --version
Python 2.7.13

Issue appears to be associated with addition of FileContentStream class in r11797.

Attachments (0)

Change History (8)

comment:1 Changed 7 years ago by Ryan J Ollos

Proposed fix, tested with Trac 1.0, 1.0-stable and 1.2-stable:

  • archiveviewer/zip.py

     
    3636from trac.web.href import Href
    3737from trac.web.wsgi import _FileWrapper
    3838from trac.wiki.api import IWikiSyntaxProvider
     39try:
     40    from tracopt.versioncontrol.svn.svn_fs import FileContentStream
     41except ImportError:
     42    FileContentStream = False
    3943
    4044
    4145class ZipRenderer(Component):
     
    118122    def render(self, context, mimetype, content, filename=None, url=None):
    119123        if content and content.input:
    120124            max_size = self.env.config.getint('attachment', 'max_zip_size', default=2097152)
    121             if (Stream and isinstance(content.input, Stream)) \
     125            if Stream and isinstance(content.input, Stream) \
     126                    or FileContentStream and isinstance(content.input, FileContentStream) \
    122127                    or ZipExtFile and isinstance(content.input, ZipExtFile):
    123128                zipfile = ZipFile(StringIO(content.input.read(max_size)))
    124129            else:

comment:2 Changed 7 years ago by Jun Omae

I consider it would be simple to use hasattr(content.input, 'seek') (untested).

  • archiveviewerplugin/1.0/archiveviewer/zip.py

    diff --git a/archiveviewerplugin/1.0/archiveviewer/zip.py b/archiveviewerplugin/1.0/archiveviewer/zip.py
    index 9b606f50d..0ad460ce8 100644
    a b from datetime import datetime 
    1212from genshi.builder import tag
    1313from genshi.core import QName
    1414from genshi.filters.transform import Transformer, ENTER
    15 try:
    16     Stream = False
    17     from svn.core import Stream
    18 except ImportError:
    19     pass
     15
    2016from trac.attachment import Attachment, AttachmentModule
    2117from trac.core import Component, implements, TracError
    2218from trac.mimeview.api import IHTMLPreviewRenderer, Mimeview
    from trac.web.chrome import web_context, add_stylesheet, add_link 
    3329from trac.web.href import Href
    3430from trac.web.wsgi import _FileWrapper
    3531from trac.wiki.api import IWikiSyntaxProvider
    36 from zipfile import ZipFile, ZipExtFile
     32from zipfile import ZipFile
    3733import os
    3834import re
    3935
    class ZipRenderer(Component): 
    117113
    118114    def render(self, context, mimetype, content, filename=None, url=None):
    119115        if content and content.input:
    120             max_size = self.env.config.getint('attachment', 'max_zip_size', default=2097152)
    121             if (Stream and isinstance(content.input, Stream)) \
    122                     or ZipExtFile and isinstance(content.input, ZipExtFile):
    123                 zipfile = ZipFile(StringIO(content.input.read(max_size)))
    124             else:
    125                 zipfile = ZipFile(content.input)
     116            f = content.input
     117            if not hasattr(f, 'seek'):
     118                max_size = self.config.getint('attachment', 'max_zip_size',
     119                                              2097152)
     120                f = StringIO(f.read(max_size))
     121            zipfile = ZipFile(f)
    126122            listitems = []
    127123            for info in zipfile.infolist():
    128124                resource = context.resource.child('zip', info.filename)

Also, [attachment] max_zip_size can be -1 to disable download as zip. I think we should check it.

Last edited 7 years ago by Jun Omae (previous) (diff)

comment:3 Changed 7 years ago by Ryan J Ollos

Owner: changed from matobaa to Ryan J Ollos
Status: newaccepted

Thanks, that looks good. I will test and commit.

comment:4 Changed 7 years ago by Ryan J Ollos

In 16649:

ArchiveViewer 0.2dev: Fix AttributeError with Trac 1.0.2+

Patch by Jun Omae.

Refs #13202.

comment:5 Changed 7 years ago by Ryan J Ollos

In 16650:

TracArchiveViewer 0.2dev: Change plugin name

This is being done to follow convention and avoid name clashes
in eventual publication to PyPI.

When upgrading, you should uninstall the old plugin first:
$ pip uninstall ArchiveViewer

Refs #13202.

comment:6 in reply to:  2 Changed 7 years ago by Ryan J Ollos

Replying to Jun Omae:

Also, [attachment] max_zip_size can be -1 to disable download as zip. I think we should check it.

I created #13205 to discuss. Let me know if I'm misunderstanding the issue.

comment:7 Changed 7 years ago by Ryan J Ollos

Resolution: fixed
Status: acceptedclosed

comment:8 Changed 7 years ago by Ryan J Ollos

Owner: changed from Ryan J Ollos to Jun Omae

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.