Ticket #1029: datauri_workaround.patch

File datauri_workaround.patch, 2.3 KB (added by lucian, 12 years ago)

Workaround for downloading data URIs to the Journal.

  • downloadmanager.py

    diff --git a/downloadmanager.py b/downloadmanager.py
    index 8402b81..329d3fc 100644
    a b import time 
    2222import tempfile
    2323import urlparse
    2424import urllib
     25import re
     26import base64
    2527
    2628import gtk
    2729import hulahop
    components.registrar.registerFactory('{23c51569-e9a1-4a92-adeb-3723db82ef7c}', 
    296298
    297299def save_link(url, text, owner_document):
    298300    # Inspired on Firefox' browser/base/content/nsContextMenu.js:saveLink()
     301   
     302    # HACK, workaround for ticket #1029
     303    if url.startswith('data:'):
     304        save_data_uri(url, text, owner_document)
     305        return
    299306
    300307    cls = components.classes["@mozilla.org/network/io-service;1"]
    301308    io_service = cls.getService(interfaces.nsIIOService)
    def save_link(url, text, owner_document): 
    320327            _SaveLinkProgressListener(owner_document),
    321328            interfaces.nsIStreamListener)
    322329    channel.asyncOpen(listener, None)
     330   
     331def save_data_uri(url, text, owner_document):
     332    '''Special case, workaround for ticket #1029'''
     333    pattern = re.compile(r'''
     334                  ^data: 
     335                  (\w+/\w+)?            # mimetype
     336                  (?:;charset="(\w+)")? # charset
     337                  (?:;(base64))?        # encoding
     338                  ,(.+)                 # actual data
     339                  $
     340                  ''', re.VERBOSE)
     341    result = re.search(pattern, url).groups()
     342
     343    mime = result[0] or 'text/plain'
     344    charset = result[1] or 'US-ASCII'
     345    encoding = result[2] or 'base64'
     346    data = base64.decodestring(result[3])
     347
     348    temp_path = os.path.join(activity.get_activity_root(), 'instance')
     349    if not os.path.exists(temp_path):
     350        os.makedirs(temp_path)
     351    fd, file_path = tempfile.mkstemp(dir=temp_path, prefix='datauri',
     352                                     suffix='')
     353    os.close(fd)
     354    os.chmod(file_path, 0644)
     355
     356    # write data to file
     357    open(file_path, 'w').write(data)
     358
     359    jobject = datastore.create()
     360    jobject.metadata['title'] = 'datauri'
     361    jobject.metadata['mime_type'] = mime
     362    jobject.metadata['icon-color'] = profile.get_color().to_string()
     363    jobject.file_path = file_path
     364    datastore.write(jobject)
     365   
     366    activity.show_object_in_journal(jobject.object_id)
    323367
    324368def _implements_interface(obj, interface):
    325369    try: