def save(self, *args, **kwargs): super().save(*args, **kwargs) img = Image.open(self.image) if img.height > 300 or img.width > 300: image_types = { 'jpeg': 'JPEG', 'jpg': 'JPEG', 'png': 'PNG', 'gif': 'GIF', 'tif': 'TIFF', } buffer = BytesIO() image_path = Path(self.image.name) image_filename = image_path.name filename_suffix = image_path.suffix[1:].lower() image_format = image_types[filename_suffix] output_size = (300, 300) img = self.crop_max_square(img).resize(output_size, Image.LANCZOS) img.save(buffer, format=image_format) file_object = File(buffer) file_object.content_type = 'image/jpeg' self.image.save(image_filename, file_object) self.save()
def converted(self, content_type="audio/mpeg"): """ Function that converts the uploaded audio to .mp3 and returns the converted file :param self: A handle to the :class:`models.Document` :type self: class: `models.Document` :param content_type: content type of the converted file, defaults to "audio/mpeg" :type request: content_type :return: File containing .mp3 audio :rtype: .mp3 File """ super(Document, self).save() file_path = self.audio.path original_extension = file_path.split(".")[-1] mp3_converted_file = AudioSegment.from_file(file_path, original_extension) new_path = file_path[:-4] + "-conv.mp3" mp3_converted_file.export(new_path, format="mp3", bitrate="44.1k") converted_audiofile = File(file=open(new_path, "rb"), name=Path(new_path)) converted_audiofile.name = Path(new_path).name converted_audiofile.content_type = content_type converted_audiofile.size = os.path.getsize(new_path) os.remove(new_path) return converted_audiofile
def to_python(self, value: str) -> typing.Optional[File]: if not value: return None mime = None if ',' in value: mime, strict = guess_type(value) value = value.split(',')[-1] if self._mime and mime not in self._mime: params = {'allowed': ', '.join(self._mime), 'received': mime} raise ValidationError(self.error_messages['invalid_mime'], code='invalid_mime', params=params) file = File(BytesIO(b64decode(value))) if self._max_length is not None and file.size > self._max_length: params = {'max': self._max_length, 'length': file.size} raise ValidationError(self.error_messages['max_length'], code='max_length', params=params) file.content_type = mime return file
def test_invalid_file(self): """ When file is not a zip file, ValidationError should be raised. """ f = File('invalidfile') f.content_type = 'invalid' with self.assertRaises(ValidationError): validatefiletype(f)
def test_not_a_txt_file(self): """ This method tests if validation error is raised when a non-text file is uploaded """ f = File('invalid') f.content_type = 'invalid' with self.assertRaises(ValueError): extract_text_file(f)
def test_txt_file(self): """ When file is a TXT file, no exception should be raised. """ f = File('txtfile') f.content_type = 'text/plain' try: validatefiletype(f) except ValidationError: self.fail('Should not raise Validation Error on a TXT file.')
def test_accepted_types_field(self): accepted_types = ['image/bmp','image/jpg', 'image/png'] field = RestrictedTypeFileField(accepted_types=accepted_types) f = File(open(os.path.join(os.path.abspath(os.path.dirname(__file__)), '__init__.py'))) f.content_type = 'image/bmp' data = type("",(object,),dict(file=f, accepted_types=accepted_types)) self.assertEquals(field.clean(data, {}).file, f)
def test_no_accepted_types_field(self): field = RestrictedTypeFileField() f = File(open(os.path.join(os.path.abspath(os.path.dirname(__file__)), '__init__.py'))) f.content_type = 'image/bmp' data = type("",(object,),dict(file=f)) with self.assertRaises(forms.ValidationError): field.clean(data, None)
def _open(self, name, mode='rb'): assert mode == 'rb', "You've tried to open binary file without specifying binary mode! You specified: %s" % mode try: media_file = FileStorage.objects.get(file_name=name) except ObjectDoesNotExist: return None content_type = media_file.content_type inMemFile = StringIO.StringIO(media_file.blob) inMemFile.name = name inMemFile.mode = mode retFile = File(inMemFile) retFile.content_type = content_type return retFile
def to_internal_value(self, data): if isinstance(data, unicode) and is_absolute_url(data): response = requests.get(data, verify=False) try: response.raise_for_status() except requests.HTTPError, e: raise ValidationError(e.message) data = File( StringIO(response.content), name=data.split('/')[-1], ) data.content_type = response.headers['content-type']
def test_download(self): filename = "attachment.txt" file = File(StringIO("THIS IS THE ATTACHMENT")) file.name = 'the-text.txt' file.content_type = "text/plain" url = url_for('library', self.anchor) + '/' + filename self.ajax_post(url, {'attachment': file}) response = self.client.get(url) self.assertEqual(response.status_code, 302) assert response['Location'].startswith('http://testserver/media/attachments/the-text')
def create_from_file(file_, name=None, scan=False): ''' Creates a django.core.files.base.File instance from a filepath or filelike object, optionally performing a virus scan on it. The created File object will have the following additional properties: checksum -- the file's computed MD5 checksum content_type -- MIME type guessed from file extension and magic bytes Parameters: file_ -- File path or filelike object to create the File from. This function requires a seekable filelike object, so if one is provided that doesn't support seek(), it will be automatically wrapped in a buffering object. For best result, provide an object that supports seek(). name -- Original filename of the provided file. If a file path is provided, os.path.basename(path) will take precedence. scan -- Boolean, whether or not to scan for viruses. ''' # Identify input file object and normalize to create fileobj if type(file_) in types.StringTypes: fileobj = open(file_, 'rb') else: seek = getattr(file_, 'seek', None) if not type(seek) is types.MethodType: fileobj = filelike.wrappers.Buffer(file_) else: fileobj = file_ # Scan upload for viruses using ClamD if scan and settings.CLAM_AV_ENABLED: scan_file(fileobj) # Calculate file's md5 sum fileobj.seek(0) md5 = hashlib.md5() for chunk in chunk_input_stream(fileobj): md5.update(chunk) checksum = md5.hexdigest() # Guess content type fileobj.seek(0) content_type = guess_mime(fileobj, name) # Construct File object result fileobj.seek(0) ufile = File(fileobj, name=name) ufile.checksum = checksum ufile.content_type = content_type return ufile
def converted(self, content_type='audio/mpeg'): super(Document, self).save() file_path = self.audio.path original_extension = file_path.split('.')[-1] mp3_converted_file = AudioSegment.from_file(file_path, original_extension) new_path = file_path[:-4] + '-conv.mp3' mp3_converted_file.export(new_path, format='mp3', bitrate="44.1k") converted_audiofile = File(file=open(new_path, 'rb'), name=Path(new_path)) converted_audiofile.name = Path(new_path).name converted_audiofile.content_type = content_type converted_audiofile.size = os.path.getsize(new_path) os.remove(new_path) return converted_audiofile
def test_upload_without_filename_in_url(self): filename = "attachment.txt" file = File(StringIO("THIS IS THE ATTACHMENT")) file.name = 'the-text.txt' file.content_type = "text/plain" url = url_for('library', self.anchor) response = self.ajax_post(url, {'attachment': file, 'filename': filename}) self.assertEqual(response.status_code, 200) pk = response.data['id'] content_type = response.data['content_type'] self.assertEqual(content_type, 'text/plain') Attachment.objects.get(anchor_uri=uri(self.anchor), filename=filename)
def generate_db_dump(): db = settings.DATABASES['default'] file_name = 'backup_{}.sql'.format( timezone.now().strftime('%d%m%Y_%H%M%S')) compress_command = ['gzip', file_name] popen = subprocess.Popen([ 'pg_dump', '--dbname=postgresql://{}:{}@{}:{}/{}'.format( db.get('USER'), db.get('PASSWORD'), db.get('HOST'), db.get('PORT'), db.get('NAME')), '-f', file_name ], stdout=subprocess.PIPE, universal_newlines=True) popen.wait() popen2 = subprocess.Popen(compress_command) popen2.wait() compressed_file_name = '{}.gz'.format(file_name) gzipped = open(compressed_file_name, mode='rb') file = File(file=gzipped) file.content_type = 'text/*' subprocess.Popen(['rm', compressed_file_name]) return file
def from_girder(cls, version: Version, girder_file: GirderFile, client: GirderClient) -> Asset: sha256_hasher = hashlib.sha256() blob_size = 0 with NamedTemporaryFile('r+b') as local_stream: logger.info(f'Downloading file {girder_file.girder_id}') with client.iter_file_content( girder_file.girder_id) as file_content_iter: for chunk in file_content_iter: sha256_hasher.update(chunk) blob_size += len(chunk) local_stream.write(chunk) logger.info(f'Downloaded file {girder_file.girder_id}') local_stream.seek(0) # local_path = Path(local_stream.name) sha256 = sha256_hasher.hexdigest() blob = File(file=local_stream, name=girder_file.path.lstrip('/')) # content_type is not part of the base File class (it on some other subclasses), # but regardless S3Boto3Storage will respect and use it, if it's set blob.content_type = 'application/octet-stream' asset = Asset( version=version, path=girder_file.path, size=blob_size, sha256=sha256, metadata=girder_file.metadata, blob=blob, ) # The actual upload of blob occurs when the asset is saved asset.save() return asset