def _make_swf(file): buffer = StringIO() for chunk in file.chunks(): buffer.write(chunk) buffer.reset() file = File(type='application_swf') file.file.put(buffer, content_type='application/x-shockwave-flash') file.transformation = 'main.swf' file.save() return file
def audio_file(name, description): buffer = StringIO() for chunk in request.FILES['file'].chunks(): buffer.write(chunk) buffer.reset() file = File(type='library_audio') file.file.put(buffer, content_type='audio/mpeg') file.transformation = 'main.mp3' file.name = name file.description = description return file.save()
def test_batch_transformation_with_single_transformation(self): file = File(type='text') buffer = StringIO() buffer.write('1') buffer.reset() file.file.put(buffer, content_type='text/plain') file.save() file.reload() transformation = BatchFileTransformation('batch', TextTransformation('test')) transformation.apply(file, file) file.reload() self.failUnlessEqual('2', file.file.read())
def test_batch_transformation_with_single_transformation(self): file = File(type='text') buffer = StringIO() buffer.write('1') buffer.reset() file.file.put(buffer, content_type='text/plain') file.save() file.reload() transformation = BatchFileTransformation('batch', TextTransformation('test') ) transformation.apply(file, file) file.reload() self.failUnlessEqual('2', file.file.read())
def test_apply_transformation_into_source(self): file = File(type='text') buffer = StringIO() buffer.write('1') buffer.reset() file.file.put(buffer, content_type='text/plain') file.save() file.reload() self.failUnlessEqual('1', file.file.read()) transformation = TextTransformation('test') transformation.apply(file, file) file.reload() self.failUnlessEqual('2', file.file.read()) transformation.apply(file, file) file.reload() self.failUnlessEqual('4', file.file.read())
def _apply(self, source, destination): parser = ImageFileParser() parser.feed(source.file.read()) source_image = parser.close() crop_box = ImageResize.cropbox(source_image.size, (self.width, self.height)) image = source_image.crop(crop_box) image = image.resize((self.width, self.height), Image.BICUBIC) buffer = StringIO() image.save(buffer, self.format) buffer.reset() if destination.file: write = destination.file.replace else: write = destination.file.put write(buffer, content_type="image/%s" % self.format) destination.save() return destination
def _apply(self, source, destination): parser = ImageFileParser() parser.feed(source.file.read()) source_image = parser.close() crop_box = self._get_crop_box(source_image.size, (self.width, self.height)) image = source_image.crop(crop_box) image = image.resize((self.width, self.height), Image.ANTIALIAS) buffer = StringIO() image.save(buffer, self.format) buffer.reset() if destination.file: write = destination.file.replace else: write = destination.file.put write(buffer, content_type = 'image/%s' % self.format) destination.save() return destination
def test_system_command_transformations(self): class TestSystemCommandFileTransformation(SystemCommandFileTransformation): SYSTEM_COMMAND = 'cp %(source)s %(destination)s' def _get_derivative_content_type(self): return 'text/plain' DATA = 'xxxxXXXXxxxx' file = File(type='text') buffer = StringIO() buffer.write(DATA) buffer.reset() file.file.put(buffer, content_type='text/plain') file.save() file.reload() transformation = TestSystemCommandFileTransformation('test_trans') transformation.apply(file) derivative = file.get_derivative('test_trans') self.failUnlessEqual(DATA, derivative.file.read()) self.failUnlessEqual('text/plain', derivative.file.content_type)
def test_system_command_transformations(self): class TestSystemCommandFileTransformation( SystemCommandFileTransformation): SYSTEM_COMMAND = 'cp %(source)s %(destination)s' def _get_derivative_content_type(self): return 'text/plain' DATA = 'xxxxXXXXxxxx' file = File(type='text') buffer = StringIO() buffer.write(DATA) buffer.reset() file.file.put(buffer, content_type='text/plain') file.save() file.reload() transformation = TestSystemCommandFileTransformation('test_trans') transformation.apply(file) derivative = file.get_derivative('test_trans') self.failUnlessEqual(DATA, derivative.file.read()) self.failUnlessEqual('text/plain', derivative.file.content_type)
def to_python(self, data): """ Checks that the file-upload field data contains a valid video file """ f = super(VideoField, self).to_python(data) if f is None: return None buffer = StringIO() for chunk in data.chunks(): buffer.write(chunk) buffer.reset() try: validator = VideoFileValidator() validator.validate(buffer) except FileValidator.Exception: raise ValidationError(self.error_messages['invalid_video']) buffer.reset() self.buffer = buffer self.content_type = f.content_type return data
def to_python(self, data): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """ f = super(ImageField, self).to_python(data) if f is None: return None buffer = StringIO() for chunk in data.chunks(): buffer.write(chunk) buffer.reset() try: parser = ImageFileParser() parser.feed(buffer.read()) parser.close() except Exception: raise ValidationError(self.error_messages['invalid_image']) self.buffer = buffer self.content_type = f.content_type return data
def to_python(self, data): """ Checks that the file-upload field data contains a valid video file """ f = super(VideoField, self).to_python(data) if f is None: return None buffer = StringIO() for chunk in data.chunks(): buffer.write(chunk) buffer.reset() try: validator = VideoFileValidator() validator.validate(buffer) except FileValidator.Exception: raise ValidationError(self.error_messages["invalid_video"]) buffer.reset() self.buffer = buffer self.content_type = f.content_type return data
def to_python(self, data): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """ f = super(ImageField, self).to_python(data) if f is None: return None buffer = StringIO() for chunk in data.chunks(): buffer.write(chunk) buffer.reset() try: parser = ImageFileParser() parser.feed(buffer.read()) parser.close() except Exception: raise ValidationError(self.error_messages["invalid_image"]) self.buffer = buffer self.content_type = f.content_type return data
def send(request): send_form = InviteForm(request.POST or None) import_form = ImportInviteForm(request.POST or None, request.FILES) sent = [] if send_form.is_valid(): sent.append(_send_invite(request.user, send_form.cleaned_data['email'], send_form.cleaned_data['name'])) if import_form.is_valid(): buffer = StringIO() for chunk in request.FILES['file']: buffer.write(chunk) buffer.reset() str = buffer.read() if re.match(r'BEGIN\s*:\s*VCARD', str, re.IGNORECASE | re.MULTILINE): file_type = 'vcard' else: file_type = 'csv' buffer.reset() contacts = [] if file_type == 'csv': try: encoding = chardet.detect(buffer.read())['encoding'] buffer.reset() reader = codecs.getreader(encoding)(buffer) buffer = StringIO() buffer.write('\n'.join(reader.readlines()).encode('utf-8')) buffer.reset() reader = csv.DictReader(buffer) for row in reader: try: email = row['E-mail Address'] first_name = row['First Name'] last_name = row['Last Name'] contacts.append((email, ('%s %s' % (first_name, last_name)).decode('utf-8'))) except: pass except: pass elif file_type == 'vcard': vobjects = vobject.readComponents(buffer) while 1: try: vcf = vobjects.next() if vcf.contents.has_key('email'): contacts.append((vcf.email.value, vcf.fn.value)) except Exception, e: print e break for (email, name) in contacts: sent.append(_send_invite(request.user, email, name))