def fetch_original (repository, response, params):
    """
    Return the original bits of the document.  If the document original is multi-file,
    a zipfile containing all the parts is returned, with MIME type "application/x-folder-zipped".
    With one exception -- if the document is multi-file and a Web page,
    and if the user has specified the "browser" parameter,
    the response is an HTTP redirect to the stored version of the Web page,
    rather than an actual value.

    :param doc_id: the document to retrieve
    :type doc_id: an UpLib doc ID string
    :param browser: optional, indicates whether the result is for display in browser
    :type browser: boolean
    :return: the document original
    :rtype: bits or possibly a redirect
    """

    def check_for_webpage_complete(d):
        files = os.listdir(d)
        files.sort()
        if len(files) != 2:
            return None
        if files[0].endswith(" Files") and (files[1].endswith(".html") or files[1].endswith(".htm")):
            return files[1]
        if files[1].endswith("_files") and (files[0].endswith(".html") or files[0].endswith(".htm")):
            return files[0]
        return None

    if not params.has_key('doc_id'):
        response.error(HTTPCodes.BAD_REQUEST, "No query specified.\n")
        return
    doc_id = params.get('doc_id')
    if not repository.valid_doc_id(doc_id):
        response.error(HTTPCodes.NOT_FOUND, "Invalid doc_id %s specified.\n" % doc_id)
        return

    doc = repository.get_document(doc_id)
    originals_dir = doc.originals_path()
    format = doc.get_metadata("apparent-mime-type") or "application/octet-stream"
    if os.path.isdir(originals_dir):
        files = os.listdir(originals_dir)
        master = check_for_webpage_complete(originals_dir)
        if not files:
            response.error(HTTPCodes.NOT_FOUND, "No originals for that document %s.\n" % doc_id)
        if len(files) == 1:
            #note("returning single file %s (%s)", os.path.join(originals_dir, files[0]), format)
            response.return_file(format, os.path.join(originals_dir, files[0]))
        elif params.has_key("browser") and os.path.exists(os.path.join(originals_dir, "original.html")):
            #note("redirecting to /docs/%s/originals/original.html" % doc_id)
            response.redirect("/docs/%s/originals/original.html" % doc_id)
        elif params.has_key("browser") and master:
            #note("redirecting to /docs/%s/originals/%s" ,doc_id, master)
            response.redirect("/docs/%s/originals/%s" % (doc_id, master))
        else:
            tfilename = zipup(originals_dir)
            #note("returning zipfile %s", tfilename)
            response.return_file("application/x-folder-zipped", tfilename, true)
    else:
        response.error(HTTPCodes.NOT_FOUND, "No originals for that document %s.\n" % doc_id)
def fetch_folder (repository, response, params):
    """
    Return a document's UpLib folder as a zip file.

    :param doc_id: the document to fetch
    :type doc_id: UpLib doc ID string

    :return: a zipped UpLib document folder
    :rtype: zipped directory tree, as MIME type "application/x-uplib-folder-zipped"
    """
    if not params.has_key('doc_id'):
        response.error(HTTPCodes.BAD_REQUEST, "No query specified.\n")
        return
    doc_id = params.get('doc_id')
    if not repository.valid_doc_id(doc_id):
        response.error(HTTPCodes.NOT_FOUND, "Invalid doc_id %s specified.\n" % doc_id)
        return
    location = repository.doc_location(doc_id)
    tfilename = zipup(location)
    response.return_file("application/x-uplib-folder-zipped", tfilename, true)