Esempio n. 1
0
def file_content(file_attrs, request, output, deleted=False):
    d = {'filename': file_attrs['name'],
         'page_name': file_attrs['attached_to_pagename']}
    if deleted:
        version = file_attrs['uploaded_time']
        d['file_version'] = version
        file = wikidb.getFile(request, d, deleted=True,
                              version=version, fresh=True)
    else:
        file = wikidb.getFile(request, d, fresh=True)
    file_str = file[0]

    base64_file_str = b64encode(file_str)
    doc = xml.createDocument(None, dummy_name, None)
    root = doc.documentElement
    text = doc.createTextNode(base64_file_str)
    output.write(text.toxml().encode(config.charset))
Esempio n. 2
0
def file_content(file_attrs, request, output, deleted=False):
    d = {
        'filename': file_attrs['name'],
        'page_name': file_attrs['attached_to_pagename']
    }
    if deleted:
        version = file_attrs['uploaded_time']
        d['file_version'] = version
        file = wikidb.getFile(request,
                              d,
                              deleted=True,
                              version=version,
                              fresh=True)
    else:
        file = wikidb.getFile(request, d, fresh=True)
    file_str = file[0]

    base64_file_str = b64encode(file_str)
    doc = xml.createDocument(None, dummy_name, None)
    root = doc.documentElement
    text = doc.createTextNode(base64_file_str)
    output.write(text.toxml().encode(config.charset))
Esempio n. 3
0
def fileSend(request, pagename=None, filename=None):
    # Front_Page?file=larry_coho.jpg&thumb=yes&size=240
    # httpd_referer is like "http://daviswiki.org/blahblah/page?test=goajf"
    # (the whole string)
    # let's test against it using their possibly configured regular expression. 
    # this is to prevent image hotlinking
    referer_domain = getDomainFromURL(request.http_referer)
    if farm.isDomainInFarm(referer_domain, request):
        allowed = True
    elif request.config.referer_regexp and request.http_referer:
        allowed = re.search(request.config.referer_regexp,
                            request.http_referer, re.IGNORECASE)
    else:
        allowed = True

    if not pagename:
        pagename = request.pagename

    # if they aren't allowed to view this page or this image (bad referer)
    # then don't send them the image
    if allowed:
        if not request.user.may.read(Page(pagename, request)):
            return
    else:
        return

    deleted = False
    version = 0
    thumbnail = False
    thumbnail_size = 0
    do_download = False
    ticket = None
    size = None

    if not filename:
        filename_encoded = request.form['file'][0]
        filename = urllib.unquote(filename_encoded)
    
    if request.form.has_key('deleted'):
        if request.form['deleted'][0] == 'true':
            deleted = True
    if request.form.has_key('thumb'):
        if request.form['thumb'][0] == 'yes':
            thumbnail = True
    if request.form.has_key('size'):
        thumbnail_size = int(request.form['size'][0])
    if request.form.has_key('version'):
        version = float(request.form['version'][0])
    if request.form.has_key('download') and request.form['download'][0]:
        do_download = True
    if request.form.has_key('ticket'):
        ticket = request.form['ticket'][0]
        if not checkTicket(ticket) or not request.form.has_key('size'):
            request.http_headers()
            request.write("No file..?")
            return
        try:
            size = int(request.form['size'][0])
        except:
            request.http_headers()
            request.write("No file..?")

    d = {'filename':filename, 'page_name':pagename, 'file_version':version,
         'maxsize': size} 

    try:
        file, modified_time_unix = wikidb.getFile(request, d, deleted=deleted,
            thumbnail=thumbnail, version=version, ticket=ticket)
    except:
        request.http_headers()
        request.write("No file..?")
        return

    mimetype = mimetypes.guess_type(filename)[0]
    
    if not mimetype:
        mimetype = "application/octet-stream"

    # we're good to go to output the image
    if modified_time_unix is None: modified_time_unix = 0
    # if we're sent an If-Modified-Since header and the file hasn't
    # been modified, send 304 Not Modified
    if not _modified_since(request, modified_time_unix): 
        request.do_gzip = False
        request.status = "304 Not Modified"
        request.http_headers()
        return
    datestring = time.strftime('%a, %d %b %Y %H:%M:%S',
                               time.gmtime(modified_time_unix)) + ' GMT' 
    length = len(file)
    contentstring = 'filename="%s"' % filename.encode(config.charset)
    # images are usually compressed anyway, so let's not bother gziping
    request.do_gzip = False
    if do_download:  
        contentstring = 'attachment; %s' % contentstring
        # bogus mimetype to force browsers to behave
        mimetype = 'application/x-download' 
    request.http_headers([("Content-Type", mimetype),
                          ("Content-Length", length),
                          ("Last-Modified", datestring),
                          ("Content-Disposition", contentstring)])
    #output image
    request.write(file, raw=True)
Esempio n. 4
0
#################################################
# This script will rebuild the imageInfo table.
#################################################
req = request.RequestDummy()

req.cursor.execute("SELECT files.name, files.attached_to_pagename, wikis.name from files, wikis where files.wiki_id=wikis.id")
files_list = req.cursor.fetchall()
for file_info in files_list:
    name, attached_to_pagename, wiki_name = file_info
    print name
    
    if wikiutil.isImage(name):
        d = {'filename': name, 'page_name': attached_to_pagename}
        req.switch_wiki(wiki_name)
        file_get = wikidb.getFile(req, d)
	if not file_get:  continue
        filecontent, last_modified = file_get

    
        file = cStringIO.StringIO(filecontent)
        img = Image.open(file)
        img_size = img.size
        file.close()
    
        d['x'] = img_size[0]
        d['y'] = img_size[1]
        d['wiki_name'] = wiki_name
    
        req.cursor.execute("UPDATE imageInfo set xsize=%(x)s, ysize=%(y)s where imageInfo.name=%(filename)s and imageInfo.attached_to_pagename=%(page_name)s and imageInfo.wiki_id=(SELECT id from wikis where name=%(wiki_name)s)", d, isWrite=True)
    
Esempio n. 5
0
def generateThumbnail(request, pagename, image_name, maxsize, temporary=False,
                      ticket=None, return_image=False, fresh=False):
    cursor = request.cursor 
    from PIL import Image
    import cStringIO
    dict = {'filename':image_name, 'page_name':pagename}

    open_imagefile = cStringIO.StringIO(wikidb.getFile(request, dict,
                                                       fresh=fresh)[0])
    im = Image.open(open_imagefile)
    converted = 0
    if not im.palette is None:
        if im.info.has_key('transparency'):
            trans = im.info['transparency']
            pal = []
            ind = 0
            numcols = len(im.palette.palette) / 3;
            while ind < numcols:
                if ind == trans:
                    pal.append( ord('\xff') )
                    pal.append(ord('\xff'))
                    pal.append( ord('\xff'))
                else:
                    pal.append(ord(im.palette.palette[ind * 3]))
                    pal.append(ord(im.palette.palette[ind * 3 + 1]))
                    pal.append(ord(im.palette.palette[ind * 3 + 2]))
                ind = ind + 1
            im.putpalette(pal)
        im = im.convert("RGB")
        converted = 1
    if im.size[0] >= im.size[1]:
        max = im.size[0]
        min = im.size[1]
        if maxsize >= max:
            shrunk_im = im
            x, y = im.size
        else:
            x = maxsize
            y = int((min * maxsize)/max)
            shrunk_im = im.resize((x, y), Image.ANTIALIAS)
            shrunk_im = sharpen_thumbnail(shrunk_im)
    else:
        max = im.size[1]
        min = im.size[0]
        if maxsize >= max:
            shrunk_im = im
            x, y = im.size
        else:
            x = int((min * maxsize)/max)
            y = maxsize
            shrunk_im = im.resize((x,y), Image.ANTIALIAS)
            shrunk_im = sharpen_thumbnail(shrunk_im)

    if converted == 1:
        shrunk_im = shrunk_im.convert("P", dither=Image.NONE,
                                      palette=Image.ADAPTIVE)

    import mimetypes
    type = mimetypes.guess_type(image_name)[0][6:]
    save_imagefile = cStringIO.StringIO()
    try:
        shrunk_im.save(save_imagefile, type, quality=90)
    except IOError:
        request.write('<em style="background-color: #ffffaa; padding: 2px;">'
                      'There was a problem with image %s.  '
                      'It probably has the wrong file extension.</em>' %
                      image_name)
    image_value = save_imagefile.getvalue()
    if return_image:
        # one-time generation for certain things like preview.
        # just return the image string
        return image_value
    dict = {'x':x, 'y':y, 'filecontent':image_value,
            'uploaded_time':time.time(), 'filename':image_name,
            'pagename':pagename}
    wikidb.putFile(request, dict, thumbnail=True, temporary=temporary,
                   ticket=ticket)

    save_imagefile.close()
    open_imagefile.close()
    
    return x, y