Example #1
0
    def test_write_back_bad_atoms(self):
        # write a broken atom and try to load it
        data = Atom.render(b"datA", b"\x00\x00\x00\x01\x00\x00\x00\x00wheeee")
        data = Atom.render(b"aART", data)
        tags = self.wrap_ilst(data)
        self.assertFalse(tags)

        # save it into an existing mp4
        original = os.path.join(DATA_DIR, "has-tags.m4a")
        filename = get_temp_copy(original)
        try:
            delete(filename)

            # it should still end up in the file
            tags.save(filename)
            with open(filename, "rb") as h:
                self.assertTrue(b"wheeee" in h.read())

            # if we define our own aART throw away the broken one
            tags["aART"] = ["new"]
            tags.save(filename)
            with open(filename, "rb") as h:
                self.assertFalse(b"wheeee" in h.read())

            # add the broken one back and delete all tags including
            # the broken one
            del tags["aART"]
            tags.save(filename)
            with open(filename, "rb") as h:
                self.assertTrue(b"wheeee" in h.read())
            delete(filename)
            with open(filename, "rb") as h:
                self.assertFalse(b"wheeee" in h.read())
        finally:
            os.unlink(filename)
Example #2
0
    def test_write_back_bad_atoms(self):
        # write a broken atom and try to load it
        data = Atom.render(b"datA", b"\x00\x00\x00\x01\x00\x00\x00\x00wheeee")
        data = Atom.render(b"aART", data)
        tags = self.wrap_ilst(data)
        self.assertFalse(tags)

        # save it into an existing mp4
        original = os.path.join(DATA_DIR, "has-tags.m4a")
        filename = get_temp_copy(original)
        try:
            delete(filename)

            # it should still end up in the file
            tags.save(filename)
            with open(filename, "rb") as h:
                self.assertTrue(b"wheeee" in h.read())

            # if we define our own aART throw away the broken one
            tags["aART"] = ["new"]
            tags.save(filename)
            with open(filename, "rb") as h:
                self.assertFalse(b"wheeee" in h.read())

            # add the broken one back and delete all tags including
            # the broken one
            del tags["aART"]
            tags.save(filename)
            with open(filename, "rb") as h:
                self.assertTrue(b"wheeee" in h.read())
            delete(filename)
            with open(filename, "rb") as h:
                self.assertFalse(b"wheeee" in h.read())
        finally:
            os.unlink(filename)
Example #3
0
    def test_write_back_bad_atoms(self):
        # write a broken atom and try to load it
        data = Atom.render(b"datA", b"\x00\x00\x00\x01\x00\x00\x00\x00wheeee")
        data = Atom.render(b"aART", data)
        tags = self.wrap_ilst(data)
        self.assertFalse(tags)

        # save it into an existing mp4
        original = os.path.join("tests", "data", "has-tags.m4a")
        fd, filename = mkstemp(suffix=".mp4")
        os.close(fd)
        shutil.copy(original, filename)
        delete(filename)

        # it should still end up in the file
        tags.save(filename)
        with open(filename, "rb") as h:
            self.assertTrue(b"wheeee" in h.read())

        # if we define our own aART throw away the broken one
        tags["aART"] = ["new"]
        tags.save(filename)
        with open(filename, "rb") as h:
            self.assertFalse(b"wheeee" in h.read())

        # add the broken one back and delete all tags including the broken one
        del tags["aART"]
        tags.save(filename)
        with open(filename, "rb") as h:
            self.assertTrue(b"wheeee" in h.read())
        delete(filename)
        with open(filename, "rb") as h:
            self.assertFalse(b"wheeee" in h.read())
Example #4
0
    def test_write_back_bad_atoms(self):
        # write a broken atom and try to load it
        data = Atom.render(b"datA", b"\x00\x00\x00\x01\x00\x00\x00\x00wheeee")
        data = Atom.render(b"aART", data)
        tags = self.wrap_ilst(data)
        self.assertFalse(tags)

        # save it into an existing mp4
        original = os.path.join("tests", "data", "has-tags.m4a")
        fd, filename = mkstemp(suffix='.mp4')
        os.close(fd)
        shutil.copy(original, filename)
        delete(filename)

        # it should still end up in the file
        tags.save(filename)
        with open(filename, "rb") as h:
            self.assertTrue(b"wheeee" in h.read())

        # if we define our own aART throw away the broken one
        tags["aART"] = ["new"]
        tags.save(filename)
        with open(filename, "rb") as h:
            self.assertFalse(b"wheeee" in h.read())

        # add the broken one back and delete all tags including the broken one
        del tags["aART"]
        tags.save(filename)
        with open(filename, "rb") as h:
            self.assertTrue(b"wheeee" in h.read())
        delete(filename)
        with open(filename, "rb") as h:
            self.assertFalse(b"wheeee" in h.read())
Example #5
0
 def test_module_delete(self):
     delete(self.filename)
     audio = MP4(self.audio.filename)
     self.failIf(audio.tags)
     self.faad()
Example #6
0
 def test_module_delete(self):
     delete(self.filename)
     audio = MP4(self.audio.filename)
     self.failIf(audio.tags)
     self.faad()
Example #7
0
def upload_media(request):

    """
    HTTP POST with the following fields:

    media_file, type: file
    client_id, type: text
    media_type, type: text
        where valid mediatypes are: 'text', 'audio', 'video', 'image'

    optional fields:

    media_text, type: text
    media_caption, type: text

    """

    result = {'success': False}

    #error_text = ''
    #try:
    if True:

        if True:
        #try:
            client_id = request.POST['client_id']
            media_type = request.POST['media_type']
        #except:
        #    result['error_text'] = 'Missing or invalid field'
        #    raise Exception('missing fields')

        file_name = ''
        file_path = ''
        if media_type == 'image' or media_type == 'video' \
                or media_type == 'audio':


            #if True:
            try:
                #print "FILE TYPE: {0}".format(type(request.POST['media_file']))
                #print "FILE CONTENTS: {0}".format(request.POST['media_file'])
                #print "LIST OF FORM OBJECTS:"
                #print request.POST
                media_file_name = request.POST['media_file'].filename
                input_file = request.POST['media_file'].file
            except:
                result['error_text'] = 'Missing or invalid file field'
                raise Exception('Invalid media_file field.')

            media_extention="processing"

            # generate a unique file name to store the file to
            file_name = '{0}.{1}'.format(uuid.uuid4(),media_extention)
            file_path = os.path.join(system_config['upload_dir'], file_name)

            # write file to temp location, and then to disk
            temp_file_path = file_path + '~'
            output_file = open(temp_file_path, 'wb')

            # Finally write the data to disk
            input_file.seek(0)
            while True:
                data = input_file.read(2<<16)
                if not data:
                    break
                output_file.write(data)

            #decode media type of written file
            #more file types can be added, but these should cover most for now
            #TODO: client side validation so they don't lose content when they upload incorrect files?
            #TODO: better error messages
            #TODO: delete / handle (in some way) files that do not validate?
            mimetype = magic.from_file(temp_file_path, mime=True)
            #process image files
            if media_type == 'image':

                #jpeg
                if mimetype == "image/jpeg":
                    media_extention  = 'jpg'
                    #print "media_Extension is: " + media_extention

                #png
                elif mimetype == "image/png":
                    media_extention  = 'png'
                    #print "media_Extension is: " + media_extention

                #not jpeg or png
                else:
                    error_text = 'invalid image file'
                    raise Exception('')

                #strip metadata from images with ImageMagick's mogrify
                #TODO: dynamically find mogrify (but I think it's usually /usr/bin)
                if True:
                #try:

                    subprocess.call(['mogrify', '-strip', temp_file_path])
                    # subprocess.call(['/usr/bin/mogrify', '-strip', temp_file_path])
                    # subprocess.call(['/usr/local/bin/mogrify', '-strip', temp_file_path])
                #except:
                #    error_text = "Mogrify is missing, or in an unexpected place."
                #    raise Exception('')

            #process video files
            elif media_type == 'video':
                #I can't seem to find any evidence of PII in mpg metadata
                if mimetype == "video/mpeg":
                    media_extention = 'mpg'
                elif mimetype == "video/mp4":
                    media_extension = "mp4"
                    #strip metadata
                    try:
                        mp4 = mutagen.mp4.MP4(temp_file_path)
                        mp4.delete()
                        mp4.save()
                    except:
                        error_text = "Something went wrong while stripping metadata from mp4"
                        raise Exception('')

                else:
                    error_text = 'invalid video file'
                    raise Exception('')

            #process audio files
            elif media_type == 'audio':

                #mp3 file
                if mimetype == "audio/mpeg":
                    media_extention = 'mp3'
                    #strip metadata
                    try:
                        mp3 = mutagen.mp3.MP3(temp_file_path)
                        mp3.delete()
                        mp3.save()
                    except:
                        error_text = "Something went wrong while stripping metadata from mp3"
                        raise Exception('')

                #ogg vorbis file
                elif mimetype == "audio/ogg" or mimetype == "application/ogg":
                    media_extention = 'ogg'
                    try:
                        ogg = mutagen.oggvorbis.Open(temp_file_path)
                        ogg.delete()
                        ogg.save()
                    except:
                        error_text = "Something went wrong while stripping metadata from ogg vorbis"
                        raise Exception('')

                #not mp3 or ogg vorbis
                else:
                    error_text = 'invalid audio file'
                    raise Exception('')

            #I don't think the user has a way to upload files of this type besides typing in the box
            #so it doesn't need as robust detection.
            elif media_type == 'text':
                media_extention = 'txt'

            else:
                error_text = 'invalid media type'
                raise Exception('')

            #the file has been validated and processed, so we adjust the file path
            #to the mimetype-dictated file extension
            file_path = file_path.replace("processing", media_extention)

            # rename once we are valid
            os.rename(temp_file_path, file_path)

            result['file_name'] = os.path.basename(file_path)

        #except:
            #result['error_text'] = 'Missing or invalid media_file contents.'
            #raise Exception('missing/invalid media_file contents')

        media_caption = ''
        #if True:
        try:
            media_caption = request.POST['media_caption']
        except:
            pass


        media_text = ''
        if media_type == 'text':
            try:
                media_text = request.POST['media_text']
            except:
                raise Exception('Invalid/missing field')

        # register file with database, and get file id back
        media_object, created = MediaObjects.create_new_media_object(
            DBSession,
            client_id,
            media_type,
            os.path.basename(file_path),
            media_caption,
            media_text,
        )

        result['media_id'] = media_object.media_id
        result['success'] = True
        result['new_user'] = created
        #result['media_text'] = media_text
        result['error_text'] = ''

        # Debug/Logging
        #datetime = str(strftime("%Y-%m-%d %H:%M:%S"))
        event_type = 'http_request'
        event_details = {
            'url':'upload_media.json',
            'event_datetime': str(datetime.datetime.now()),
            'client_id': client_id,
            'media_type': media_type,
            'file_name': os.path.basename(file_path),
            'media_caption': media_caption,
            'media_text': media_text,
            'success': result['success'],
            'media_id': result['media_id'],
            'new_user': result['new_user'],
            'error_text': result['error_text'],
        }
        client_log = EventLogs.log(DBSession,client_id,event_type,json.dumps(event_details))

        if created:
            #datetime = str(strftime("%Y-%m-%d %H:%M:%S"))
            event_type = 'new_user_created'
            event_details = {
                'client_id': client_id,
                'method': 'upload_media.json',
            }
            client_log = EventLogs.log(DBSession,client_id,event_type,json.dumps(event_details))

    #except:
    #    pass


    #resp = json.dumps(result)
    #return Response(resp,content_type='application/json')

    return make_response(result)