示例#1
0
    def testNewContentFromTestContentDir(self):
        self.assertTrue(
            os.path.isdir(TEST_CONTENT_DIR),
            "Directory '%s' containing test files does not exist." %
            TEST_CONTENT_DIR)
        files = os.listdir(TEST_CONTENT_DIR)
        print("\n\nFAILEJA", TEST_CONTENT_DIR, files)
        self.assertGreater(
            len(files), 0, "Directory '%s' containing test files is empty." %
            TEST_CONTENT_DIR)
        cnt = 0
        for filename in files:
            cnt += 1
            c = Content(caption="New content #%d" % cnt)
            full_path = os.path.join(TEST_CONTENT_DIR, str(filename))
            c.set_file(str(filename), full_path)
            c.set_fileinfo()
            c.generate_thumbnail()
            c.save()
            maintype = c.mimetype.split("/")[0]
            print("MIMETYYPPI", c.mimetype, c.preview)

            if maintype in ["video", "audio"]:
                ffp = content.filetools.FFProbe(c.file.path)
                info = content.filetools.fileinfo(c.file.path)
                print(info)

                if ffp.is_video():
                    new_video, cmd_str, output = create_videoinstance(
                        c.file.path)
                    vi = Videoinstance(content=c)
                    vi.save()
                    vi.set_file(new_video, "webm")
                    vi.set_metadata(info)
                    vi.save()
                    print("%s %.1f sec %dx%d pix" %
                          (vi.mimetype, vi.duration, vi.width, vi.height))

                if ffp.is_audio():
                    new_audio, cmd_str, output = create_audioinstance(
                        c.file.path)
                    ai = Audioinstance(content=c)
                    ai.save()
                    ai.set_file(new_audio, "ogg")
                    ai.set_metadata(info)
                    ai.save()
                    print("%s %.1f sec" % (ai.mimetype, ai.duration))

            self.all_content.append(c)
            # self.assertEqual(c.file.path, "sd", c.file.path)
        # import time
        # time.sleep(20)
        self.assertEqual(Content.objects.count(), len(self.all_content),
                         "1 or more files failed")
示例#2
0
    def testNewContentSaving(self):
        self.assertTrue(
            os.path.isdir(TEST_CONTENT_DIR),
            f"Directory '{TEST_CONTENT_DIR}' containing test files does not exist."
        )
        files = os.listdir(TEST_CONTENT_DIR)
        self.assertGreater(
            len(files), 0,
            f"Directory '{TEST_CONTENT_DIR}' containing test files is empty.")
        cnt = 0
        for filename in files:
            full_path = os.path.join(TEST_CONTENT_DIR, str(filename))
            if os.path.isfile(full_path) is False:
                print(f"Skip {full_path}, not a file")
                continue
            cnt += 1
            c = Content(caption="New content #%d" % cnt)
            c.set_file(str(filename), full_path)
            c.set_fileinfo()
            c.generate_thumbnail()
            c.save()
            maintype = c.mimetype.split("/")[0]

            if maintype in ["video", "audio"]:
                ffp = content.filetools.FFProbe(c.file.path)
                info = content.filetools.fileinfo(c.file.path)
                print(info)
                if ffp.is_video():
                    new_video, cmd_str, output = create_videoinstance(
                        c.file.path)
                    vi = Videoinstance(content=c)
                    vi.save()
                    vi.set_file(new_video, "webm")
                    vi.set_metadata(info)
                    vi.save()
                    print(
                        f"{c.mimetype} {c.preview} {vi.mimetype} {vi.duration:.1f} sec {vi.width}x{vi.height} px"
                    )
                if ffp.is_audio():
                    new_audio, cmd_str, output = create_audioinstance(
                        c.file.path)
                    ai = Audioinstance(content=c)
                    ai.save()
                    ai.set_file(new_audio, "ogg")
                    ai.set_metadata(info)
                    ai.save()
                    print(
                        f"{c.mimetype} {c.preview} {ai.mimetype} {ai.duration:.1f} sec"
                    )
            self.all_content.append(c)
示例#3
0
 def testNewContentSaving(self):
     self.assertTrue(os.path.isdir(TESTCONTENT_DIR), "Directory '%s' containing test files does not exist." % TESTCONTENT_DIR)
     files = os.listdir(TESTCONTENT_DIR)
     self.assertGreater(len(files), 0,  "Directory '%s' containing test files is empty." % TESTCONTENT_DIR)
     cnt = 0
     for filename in files:
         cnt += 1
         c = Content(caption=u'New content #%d' % cnt)
         full_path = os.path.join(TESTCONTENT_DIR, filename)
         if not os.path.isfile(full_path):
             continue
         c.set_file(filename, full_path)
         c.set_fileinfo()
         c.generate_thumbnail()
         c.save()
         maintype = c.mimetype.split('/')[0]
         print "MIMETYYPPI", c.mimetype, c.preview
         if maintype in ['video', 'audio']:
             ffp = content.filetools.FFProbe(c.file.path)
             info = content.filetools.fileinfo(c.file.path)
             print info
             #finfo = get_ffmpeg_videoinfo(c.file.path)
             #print finfo
             if ffp.is_video():
                 new_video, cmd_str = create_videoinstance(c.file.path)
                 vi = Videoinstance(content=c)
                 vi.save()
                 vi.set_file(new_video, 'webm')
                 #info = get_videoinfo(get_ffmpeg_videoinfo(vi.file.path))
                 vi.set_metadata(info)
                 vi.save()
                 print u'%s %.1f sec %dx%d pix' % (vi.mimetype, vi.duration, vi.width, vi.height)
             if ffp.is_audio():
                 new_audio, cmd_str = create_audioinstance(c.file.path)
                 ai = Audioinstance(content=c)
                 ai.save()
                 ai.set_file(new_audio, 'ogg')
                 #info = get_audioinfo(get_ffmpeg_videoinfo(ai.file.path))
                 #print info
                 ai.set_metadata(info)
                 ai.save()
                 print u'%s %.1f sec' % (ai.mimetype, ai.duration)
         #print c.get_type_instance()
         #print c.caption
         self.all_content.append(c)
         #self.assertEqual(c.file.path, "sd", c.file.path)
     #import time
     #time.sleep(20)
     self.assertEqual(Content.objects.count(), len(self.all_content), "1 or more files failed")
示例#4
0
def savefiles(msg, simulate):
    """
    Extract parts from  msg (which is an email.message_from_string(str) instance)
    and send them to the database.
    NOTES:
    - uses only the first found email address to assume recipient

    TODO stuff
    - reject if From: is empty
    """
    part_counter = 1
    subject = get_subject(msg)
    tos = get_recipient(msg)
    #print tos
    msg_id = msg.get('message-id', '')
    froms = msg.get_all('from', [])
    p = re.compile('([\w\.\-]+)@')
    try:  # May raise in some cases IndexError: list index out of range
        matches = p.findall(froms[0])
        sender_nick = matches[0].split(
            ".")[0].title()  # Use all before first '.'
    except:
        print "ERROR: No From header %s" % (msg_id)
        return False
    if len(tos) == 0:
        print "ERROR: No Tos found %s" % (msg_id)
        return False
    p = re.compile('([\w]+)\.([\w]+)@')  # e.g. [email protected]
    matches = p.findall(tos[0])
    if len(matches) > 0:
        username = matches[0][0].title()
        key = matches[0][1].lower()
    else:
        print "ERROR: No user.authkey found from %s %s" % (tos[0], msg_id)
        return False
    #print "User, key:", username, key
    # TODO: replace this with AuthTicket stuff
    #from django.contrib.auth import authenticate
    #user = authenticate(authtoken='qwerty123')
    try:
        user = User.objects.get(username=username.lower())
    except User.DoesNotExist:
        print "User.DoesNotExist !", username
        log.warning("User.DoesNotExist: '%s'" % username)
        return False
    contentgroup = None
    if user.albumitgroups.count() > 0:
        contentgroup = user.albumitgroups.all()[0]
    sourceorg = None
    if user.sourceorgs.count() > 0:
        sourceorg = user.sourceorgs.all()[0]
    photographer_name = sender_nick
    photographer = None
    if sourceorg and sourceorg.photographers.count() > 0:
        p = sourceorg.photographers.all()[0]
        photographer_name = u'{} {}'.format(p.firstname, p.lastname)
        photographer = p
    #privacy = 'PRIVATE'
    privacy = 'RESTRICTED'
    if key.lower() == 'pub':
        privacy = 'PUBLIC'
    elif key.lower() == 'res':
        privacy = 'RESTRICTED'
    else:
        log.warning("Privacy part not found: '%s'" % key)

    parts_not_to_save = [
        "multipart/mixed",
        "multipart/alternative",
        "multipart/related",
        "text/plain",
    ]
    if simulate:  # Print lots of debug stuff
        print u'=========\nMetadata:\n========='
        print u'''Subject: %s\nUsername: %s\nFrom: %s\nTo: %s\nM-id: %s\n(%s)''' % (
            subject, user, u','.join(froms), u','.join(tos), msg_id, privacy)
        print u'=========\nParts:\n========='
    saved_parts = 0
    log.info("Walking through message parts")
    for part in msg.walk():
        part_content_type = part.get_content_type()
        filename, filedata = handle_part(part)
        if part_content_type in parts_not_to_save or filename is None:
            # print "NOT SAVING", part_content_type
            log_msg = "Not saving '%s', filename '%s'." % (part_content_type,
                                                           filename)
            log.info(log_msg)
            if simulate: print log_msg  # Print lots of debug stuff
            continue
            #print filedata, type(filedata), len(filedata)
        if filedata is None or len(filedata) == 0:
            log_msg = "Not saving '%s', filename '%s', file has no data" % (
                part_content_type, filename)
            log.warning(log_msg)
            if simulate:
                print log_msg  # Print lots of debug stuff
            continue
        log_msg = u'Saving: %s (%s)' % (filename, part_content_type)
        log.info(log_msg)
        if simulate:
            print log_msg  # Print lots of debug stuff
        c = Content(
            user=user,
            privacy=privacy,
            caption=subject,
            author=photographer_name,
            #author=sender_nick,
            #group=contentgroup,
        )
        if simulate is False:
            log.info("Saving file %s" % filename)
            c.set_file(filename, filedata)
            log.info("set_fileinfo()")
            c.set_fileinfo()
            log.info("c.generate_thumbnail()")
            c.generate_thumbnail()
            c.save()
            saved_parts += 1
            log.info("Saving really")
        else:
            log.info("Not saving, simulate %s" % simulate)
        m = Metadata(
            content=c,
            sourceorg=sourceorg,
            photographer=photographer,
            group=contentgroup,
            caption=subject,
            author=photographer_name,
            geometry=c.point,
        )
        m.save()
    return saved_parts