Ejemplo n.º 1
0
    def obj_create(self, bundle, **kwargs):
        username = bundle.request.GET['username']
        media_file = bundle.data['media_file']
        bundle.data['media_type'] = 'Document'

        #todo: perhaps don't fail the upload if thumbnailing fails
        if 'video' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Video'
            if settings.VIDEO_THUMBNAILING:
                ffmpeg_wrapper = MiniFFMPEGWrapper()

                # Files < 2.5MB get stored in memory rather than on disk
                if hasattr(media_file,
                           'temporary_file_path'):  # file_object is file path
                    file_location = 'disk'
                    file_object = media_file
                    file_name = file_object.temporary_file_path()
                    #filetype = mime.from_file(file_object)
                    #file_info = m.from_file(file_object)
                else:  # file_object is an actual file object
                    file_location = 'memory'
                    file_object = media_file
                    file_buffer = media_file.read()
                    #filetype = mime.from_buffer(file_buffer)
                    #file_info = m.from_buffer(file_buffer)
                    file_object.seek(0)
                    # write it to a temporary file on disk so we can get a path to pass to the ffmpeg command
                    file_object = tempfile.NamedTemporaryFile(
                        dir=settings.FILE_UPLOAD_TEMP_DIR)
                    file_object.write(file_buffer)
                    file_object.flush()
                    os.fsync(file_object.fileno())
                    file_name = file_object.name

                ffmpeg_wrapper.video_file = file_name
                ffmpeg_wrapper.create_jpeg_from_video()
                thumb_source_file = UploadedFile(
                    open(ffmpeg_wrapper.out_filename))
                thumbnailer = Thumbnailer()
                bundle.data['media_thumb_file'] =\
                    thumbnailer.construct_thumb_from_image(
                        thumb_source_file
                    )

        if 'image' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Picture'
            media_thumb_file = Thumbnailer()\
                .construct_thumb_from_image(media_file)
            bundle.data['media_thumb_file'] = media_thumb_file

        if 'pdf' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Pdf'

        parts = media_file.name.split('.')
        media_file_type = parts[len(parts) - 1]
        bundle.data['media_file_type'] = media_file_type
        bundle = super(MediaResource, self).obj_create(bundle, **kwargs)
        update_object.delay(username)
        return bundle
class ThumbnailerTestCase(TestCase):
    '''
    test thumbnail creation for both images and videos
    '''

    def setUp(self):
        '''
        initialisation for tests
        '''
        self.thumbnailer = Thumbnailer()

    def tearDown(self):
        '''
        cleanup for tests
        '''
        pass

    def test_image_thumbnail_creation(self):
        '''
        end to end test for thumbnail creation for image files
        '''
        test_file_path = 'corroborator_app/fixtures/images.jpeg'
        test_file = open(test_file_path)
        uploaded_file = UploadedFile(test_file)
        memory_file =\
            self.thumbnailer.construct_thumb_from_image(uploaded_file)
        self.assertEqual(memory_file.content_type, 'image/jpeg')

    def test_offset_calc(self):
        '''
        test the offset is calculated correctly
        '''
        class MockImage():
            size = [100, 100]

        image = MockImage()
        offset_tuple = self.thumbnailer.calculate_offset(image)
        self.assertEqual(offset_tuple, (0, 0))

    def test_file_read(self):
        '''
        test that a file is read correctly
        '''
        test_file_path = 'corroborator_app/fixtures/images.jpeg'
        test_file = open(test_file_path)
        uploaded_file = UploadedFile(test_file)
        image = self.thumbnailer.read_image_from_file(uploaded_file)
        self.assertEqual(image.size, (259, 194))

    def test_name_file(self):
        '''
        test that the thumb gets named correctly
        '''
        test_file_path = 'corroborator_app/fixtures/images.jpeg'
        test_file = open(test_file_path)
        uploaded_file = UploadedFile(test_file)
        filename = self.thumbnailer.name_thumbnail(uploaded_file)
        self.assertEqual('images_thumb.jpg', filename)
Ejemplo n.º 3
0
class ThumbnailerTestCase(TestCase):
    '''
    test thumbnail creation for both images and videos
    '''
    def setUp(self):
        '''
        initialisation for tests
        '''
        self.thumbnailer = Thumbnailer()

    def tearDown(self):
        '''
        cleanup for tests
        '''
        pass

    def test_image_thumbnail_creation(self):
        '''
        end to end test for thumbnail creation for image files
        '''
        test_file_path = 'corroborator_app/fixtures/images.jpeg'
        test_file = open(test_file_path)
        uploaded_file = UploadedFile(test_file)
        memory_file =\
            self.thumbnailer.construct_thumb_from_image(uploaded_file)
        self.assertEqual(memory_file.content_type, 'image/jpeg')

    def test_offset_calc(self):
        '''
        test the offset is calculated correctly
        '''
        class MockImage():
            size = [100, 100]

        image = MockImage()
        offset_tuple = self.thumbnailer.calculate_offset(image)
        self.assertEqual(offset_tuple, (0, 0))

    def test_file_read(self):
        '''
        test that a file is read correctly
        '''
        test_file_path = 'corroborator_app/fixtures/images.jpeg'
        test_file = open(test_file_path)
        uploaded_file = UploadedFile(test_file)
        image = self.thumbnailer.read_image_from_file(uploaded_file)
        self.assertEqual(image.size, (259, 194))

    def test_name_file(self):
        '''
        test that the thumb gets named correctly
        '''
        test_file_path = 'corroborator_app/fixtures/images.jpeg'
        test_file = open(test_file_path)
        uploaded_file = UploadedFile(test_file)
        filename = self.thumbnailer.name_thumbnail(uploaded_file)
        self.assertEqual('images_thumb.jpg', filename)
Ejemplo n.º 4
0
    def obj_create(self, bundle, **kwargs):
        username = bundle.request.GET['username']
        media_file = bundle.data['media_file']
        bundle.data['media_type'] = 'Document'

        #todo: perhaps don't fail the upload if thumbnailing fails
        if 'video' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Video'
            if settings.VIDEO_THUMBNAILING:
                ffmpeg_wrapper = MiniFFMPEGWrapper()

                # Files < 2.5MB get stored in memory rather than on disk
                if hasattr(media_file, 'temporary_file_path'): # file_object is file path
                    file_location = 'disk'
                    file_object = media_file
                    file_name = file_object.temporary_file_path()
                    #filetype = mime.from_file(file_object)
                    #file_info = m.from_file(file_object)
                else: # file_object is an actual file object
                    file_location = 'memory'
                    file_object = media_file
                    file_buffer = media_file.read()
                    #filetype = mime.from_buffer(file_buffer)
                    #file_info = m.from_buffer(file_buffer)
                    file_object.seek(0)
                    # write it to a temporary file on disk so we can get a path to pass to the ffmpeg command
                    file_object = tempfile.NamedTemporaryFile(dir=settings.FILE_UPLOAD_TEMP_DIR)
                    file_object.write(file_buffer)
                    file_object.flush()
                    os.fsync(file_object.fileno())
                    file_name = file_object.name
                
                ffmpeg_wrapper.video_file = file_name
                ffmpeg_wrapper.create_jpeg_from_video()
                thumb_source_file = UploadedFile(open(ffmpeg_wrapper.out_filename))
                thumbnailer = Thumbnailer()
                bundle.data['media_thumb_file'] =\
                    thumbnailer.construct_thumb_from_image(
                        thumb_source_file
                    )

        if 'image' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Picture'
            media_thumb_file = Thumbnailer()\
                .construct_thumb_from_image(media_file)
            bundle.data['media_thumb_file'] = media_thumb_file

        if 'pdf' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Pdf'

        parts = media_file.name.split('.')
        media_file_type = parts[len(parts)-1]
        bundle.data['media_file_type'] = media_file_type
        bundle = super(MediaResource, self).obj_create(bundle, **kwargs)
        update_object.delay(username)
        return bundle
Ejemplo n.º 5
0
    def obj_create(self, bundle, **kwargs):
        username = bundle.request.GET['username']
        media_file = bundle.data['media_file']
        bundle.data['media_type'] = 'Document'

        if 'video' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Video'
            #ffmpeg_wrapper = MiniFFMPEGWrapper()
            #ffmpeg_wrapper.video_file = media_file.temporary_file_path()
            #ffmpeg_wrapper.create_jpeg_from_video()
            #thumb_source_file = UploadedFile(ffmpeg_wrapper.out_filename)
            #thumbnailer = Thumbnailer()
            #bundle.data['media_thumb_file'] =\
            #thumbnailer.construct_thumb_from_image(
            #thumb_source_file
            #)

        if 'image' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Picture'
            media_thumb_file = Thumbnailer()\
                .construct_thumb_from_image(media_file)
            bundle.data['media_thumb_file'] = media_thumb_file

        if 'pdf' in bundle.data['media_file'].content_type:
            bundle.data['media_type'] = 'Pdf'

        parts = media_file.name.split('.')
        media_file_type = parts[len(parts) - 1]
        bundle.data['media_file_type'] = media_file_type
        bundle = super(MediaResource, self).obj_create(bundle, **kwargs)
        update_object.delay(username)
        return bundle
Ejemplo n.º 6
0
 def setUp(self):
     '''
     initialisation for tests
     '''
     self.thumbnailer = Thumbnailer()
 def setUp(self):
     '''
     initialisation for tests
     '''
     self.thumbnailer = Thumbnailer()