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) }
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) }
def create(self, validated_data): validated_data.update(self.get_presave_create_dictionary()) # 1. save file blob to disk: f = self.initial_data.get('media_file') upload_helpers.validate_file(f, self.ext_whitelist) mapimage = validated_data.get('source_mapimage') owner = self.context.get('request').user model_name_plural = models.MapImage.model_name_plural file_name_new = upload_helpers.save_file_to_disk( owner, model_name_plural, f, uuid=mapimage.uuid) # 2. include new path variables in save dictionary: validated_data.update({ 'file_name_orig': file_name_new, 'host': mapimage.host, 'virtual_path': mapimage.virtual_path }) # 3. call default "create" method: return self.Meta.model.objects.create(**validated_data)
def create(self, validated_data): validated_data.update(self.get_presave_create_dictionary()) # 1. save file blob to disk: f = self.initial_data.get('media_file') upload_helpers.validate_file(f, self.ext_whitelist) mapimage = validated_data.get('source_mapimage') owner = self.context.get('request').user model_name_plural = models.MapImage.model_name_plural file_name_new = upload_helpers.save_file_to_disk(owner, model_name_plural, f, uuid=mapimage.uuid) # 2. include new path variables in save dictionary: validated_data.update({ 'file_name_orig': file_name_new, 'host': mapimage.host, 'virtual_path': mapimage.virtual_path }) # 3. call default "create" method: return self.Meta.model.objects.create(**validated_data)
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) }