コード例 #1
0
ファイル: PageCache.py プロジェクト: execgit/graphingwiki
def load(request, pagename, filename):
    _ = request.getText
    # check ACLs
    if not request.user.may.read(pagename):
        return xmlrpclib.Fault(1, _("You are not allowed to access this page"))

    # Grab the attachment
    result = load_pagecachefile(request, pagename, filename)

    if not result:
        return xmlrpclib.Fault(2, "%s: %s" % (_("Nonexisting cache file"),
                                              filename))

    return xmlrpclib.Binary(result)
コード例 #2
0
def reassembly(request, pagename, filename, chunkSize, digests, overwrite=True):
    _ = request.getText

    # Also check ACLs
    if not request.user.may.write(pagename):
        return xmlrpclib.Fault(1, _("You are not allowed to attach a "+
                                    "file to this page"))

    # Check whether the file already exists. If it does, check the hashes.
    fpath, exists = check_attachfile(request, pagename, filename)
    if chunkSize is not None and exists:
        try:
            stream = open(fpath, "rb")
        except:
            pass
        else:
            for digest in digests:
                data = stream.read(chunkSize)
                other = md5(data).hexdigest()
                if other != digest:
                    break
            else:
                data = stream.read(chunkSize)
                if not data:
                    stream.close()
                    return list()
            
            stream.close()

        # Fail if the file doesn't match and we don't want to overwrite.
        if not overwrite:
            return xmlrpclib.Fault(2, _("Attachment not saved, file exists"))

    # If there are missing chunks, just return them. Chunks might also
    # be in attachments for the people that use older versions of
    # opencollab.
    result = list_pagecachefiles(request, pagename)
    result.extend(list_attachments(request, pagename))
    missing = [digest for digest in digests if digest not in result]
    if missing:
        return missing

    # Reassembly the file from the chunks into a temp file.
    buffer = StringIO()
    
    for bite in digests:
        # Try both cache files and attachments (for legacy, see above)
        data = load_pagecachefile(request, pagename, bite)
        if not data:
            data = load_attachfile(request, pagename, bite)
        if not data:
            return xmlrpclib.Fault(2, "%s: %s" % (_("Nonexisting "+
                                                    "attachment or cachefile"),
                                                  filename))
        buffer.write(data)

    # Attach the decoded file.
    success = save_attachfile(request, pagename, buffer.getvalue(), filename, 
                              overwrite, True)

    # FIXME: What should we do when the cleanup fails?
    for bite in digests:
        # Try both cache files and attachments (for legacy, see above)
        in_cache = delete_pagecachefile(request, pagename, bite)
        if not in_cache:
            delete_attachfile(request, pagename, bite, True)

    # On success signal that there were no missing chunks.
    if success:
        return list()
    
    if overwrite == False:
        return xmlrpclib.Fault(2, _("Attachment not saved, file exists"))

    return xmlrpclib.Fault(2, _("Attachment not saved"))