예제 #1
0
 def process_file(self, file, owner):
     
     #save to disk:
     model_name_plural = models.Audio.model_name_plural
     file_name_new = upload_helpers.save_file_to_disk(owner, model_name_plural, file)
     file_name, ext = os.path.splitext(file_name_new)
     
     # convert to MP3:
     if ext != '.mp3':
         # use ffmpeg to convert to mp3:
         media_path = upload_helpers.generate_absolute_path(owner, model_name_plural)
         path_to_be_converted = media_path + '/' + file_name_new
         file_name_new = file_name + '.mp3'
         path_to_mp3 = media_path + '/' + file_name_new
         command = 'ffmpeg -loglevel panic -i \'%s\' -ab 32k -ar 22050 -y \'%s\'' % \
             (path_to_be_converted, path_to_mp3)
         result = os.popen(command)
     
     return {
         'file_name_orig': file.name,
         'name': self.initial_data.get('name') or file.name,
         'file_name_new': file_name_new,
         'content_type': ext.replace('.', ''),
         'virtual_path': upload_helpers.generate_relative_path(owner, model_name_plural)
     }
예제 #2
0
    def test_make_directory(self):

        # using 'ebooks' here just so we're not testing
        # the creation of a directory that already exists
        path = upload_helpers.generate_absolute_path(
            self.user, 'ebooks'
        )
        # test that directory doesn't yet exist
        self.assertFalse(os.path.isdir(path))

        # create directory
        self.model.make_directory(path)

        # test that directory now exists
        self.assertTrue(os.path.isdir(path))

        # clean up
        os.rmdir(path)
        self.assertFalse(os.path.isdir(path))
예제 #3
0
    def process_file(self, file, owner):
        from PIL import Image, ImageOps
        #save to disk:
        model_name_plural = models.Photo.model_name_plural
        file_name_new = upload_helpers.save_file_to_disk(owner, model_name_plural, file)
        file_name, ext = os.path.splitext(file_name_new)

        # create thumbnails:
        media_path = upload_helpers.generate_absolute_path(owner, model_name_plural)
        im = Image.open(media_path + '/' + file_name_new)
        exif = models.Photo.read_exif_data(im)
        sizes = [1000, 500, 250, 128, 50, 20]
        photo_paths = [file_name_new]
        for s in sizes:
            if s in [50, 25]:
                # ensure that perfect squares:
                im.thumbnail((s * 2, s * 2), Image.ANTIALIAS)
                im = im.crop([0, 0, s - 2, s - 2])
                # for some reason, ImageOps.expand throws an error for some files:
                im = ImageOps.expand(im, border=2, fill=(255, 255, 255, 255))
            else:
                im.thumbnail((s, s), Image.ANTIALIAS)
            abs_path = '%s/%s_%s%s' % (media_path, file_name, s, ext)
            im.save(abs_path)
            photo_paths.append('%s_%s%s' % (file_name, s, ext))
        
        return {
            'device': exif.get('model', None),
            'point': exif.get('point', None),
            'file_name_orig': file.name,
            'name': self.initial_data.get('name') or file.name,
            'file_name_new': file_name_new,
            'file_name_large': photo_paths[1],
            'file_name_medium': photo_paths[2],
            'file_name_medium_sm': photo_paths[3],
            'file_name_small': photo_paths[4],
            'file_name_marker_lg': photo_paths[5],
            'file_name_marker_sm': photo_paths[6],
            'content_type': ext.replace('.', ''),
            'virtual_path': upload_helpers.generate_relative_path(owner, model_name_plural)
        }
예제 #4
0
 def process_file(self, file, owner):
     #save to disk:
     model_name_plural = models.Scan.model_name_plural
     uuid = generic.generateID()
     file_name_new = upload_helpers.save_file_to_disk(owner, model_name_plural, file, uuid=uuid)
     file_name, ext = os.path.splitext(file_name_new)
     
     # create thumbnail:
     from PIL import Image
     thumbnail_name = '%s_thumb.png' % file_name
     media_path = upload_helpers.generate_absolute_path(owner, model_name_plural, uuid=uuid)
     im = Image.open(media_path + '/' + file_name_new)
     im.thumbnail([500, 500], Image.ANTIALIAS)
     im.save('%s/%s' % (media_path, thumbnail_name))
     
     return {
         'uuid': uuid,
         'file_name_orig': file.name,
         'name': self.initial_data.get('name') or file.name,
         'file_name_new': file_name_new,
         'file_name_thumb': thumbnail_name,
         'content_type': ext.replace('.', ''),
         'virtual_path': upload_helpers.generate_relative_path(owner, model_name_plural, uuid=uuid)
     }
예제 #5
0
 def generate_absolute_path(self):
     return upload_helpers.generate_absolute_path(self.owner, self.model_name_plural)
예제 #6
0
 def generate_absolute_path(self):
     return upload_helpers.generate_absolute_path(
         self.owner, self.model_name_plural
     )
예제 #7
0
 def test_generate_absolute_path(self):
     abs_path = upload_helpers.generate_absolute_path(
         self.model.owner, self.model.model_name_plural
     )
     self.assertEqual(abs_path, self.model.generate_absolute_path())