Пример #1
0
def download(url, path):
    with lock:
        if path in files:
            return
        files.add(path)

    if os.path.exists(path):
        r = tls.s.head(url)
        mtime = common.parse_last_modified(r.headers["Last-Modified"])

        if os.path.getmtime(path) == mtime and \
           os.path.getsize(path) == int(r.headers["Content-Length"]):
            return

    common.mkdirs(os.path.dirname(path))

    log(url + " -> " + path)
    r = tls.s.get(url, stream=True)

    temppath = common.mktemppath(path)

    with open(temppath, "wb") as f:
        for data in r.iter_content(4096):
            f.write(data)

        f.flush()
        os.fsync(f.fileno())

    mtime = common.parse_last_modified(r.headers["Last-Modified"])
    os.utime(temppath, (mtime, mtime))
    common.mkro(temppath)
    common.rename(temppath, path)
Пример #2
0
def download(url, path):
    with lock:
        if path in files:
            return
        files.add(path)

    if os.path.exists(path):
        r = tls.s.head(url)
        mtime = common.parse_last_modified(r.headers["Last-Modified"])

        if os.path.getmtime(path) == mtime and \
           os.path.getsize(path) == int(r.headers["Content-Length"]):
            return

    common.mkdirs(os.path.dirname(path))

    log(url + " -> " + path)
    r = tls.s.get(url, stream = True)

    temppath = common.mktemppath(path)

    with open(temppath, "wb") as f:
        for data in r.iter_content(4096):
            f.write(data)

        f.flush()
        os.fsync(f.fileno())

    mtime = common.parse_last_modified(r.headers["Last-Modified"])
    os.utime(temppath, (mtime, mtime))
    common.mkro(temppath)
    common.rename(temppath, path)
Пример #3
0
def extract(path):
    if config["attachments-enabled"] != "1":
        return

    print("Extracting attachments from %s..." % path, file = sys.stderr)

    mbox = mailbox.mbox(config["lists-base"] + "/" + path)

    for msg in mbox.keys():
        index = 0
        for part in mbox[msg].walk():
            fn = part.get_filename()
            typ = part.get_content_type()
            if fn is not None \
                    and not mailindex.decode(part.get("Content-Disposition", "inline")).startswith("inline") \
                    and typ not in \
                    ('application/pgp-signature', 'application/pkcs7-signature',
                     'application/x-pkcs7-signature', 'image/x-icon',
                     'message/external-body', 'message/rfc822', 'text/calendar',
                     'text/x-vcard'):

                p = config["attachments-base"] + "/" + path

                try:
                    fn = cleanfilename(fn)

                    if config["attachments-odponly"] != "1" or \
                            fn.lower().endswith(".odp") or \
                            typ.lower().startswith("application/vnd.oasis.opendocument.presentation"):
                        common.mkdirs(p)
                        p += "/%03u-%03u-%s" % (msg, index, fn)

                        if not os.path.exists(p):
                            temppath = common.mktemppath(p)
                        
                            f = open(temppath, "wb")
                            f.write(part.get_payload(decode = True))

                            f.flush()
                            os.fsync(f.fileno())
                            f.close()
                
                            common.rename(temppath, p)
                            common.mkro(p)

                except UnicodeEncodeError:
                    pass

            index += 1