def load(request, pagename, filename, start, end): try: fpath, exists = check_attachfile(request, pagename, filename) if not exists: return None stream = open(fpath, "rb") stream.seek(start) data = stream.read(max(end-start, 0)) stream.close() except: return None return xmlrpclib.Binary(data)
def info(request, pagename, filename): try: fpath, exists = check_attachfile(request, pagename, filename) if not exists: return None stream = open(fpath, "rb") digest = md5() data = stream.read(CHUNK_SIZE) while data: digest.update(data) data = stream.read(CHUNK_SIZE) digest = digest.hexdigest() size = stream.tell() stream.close() except: return None return [digest, size]
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"))