def download_file(filename, download_url=None, contentnode=None, assessment_item=None, preset=None, file_size=None, lang_id=None): checksum, extension = os.path.splitext(filename) extension = extension.lstrip('.') filepath = models.generate_object_storage_name(checksum, filename) # Download file if it hasn't already been downloaded if download_url and not default_storage.exists(filepath): buffer = BytesIO() response = requests.get('{}/content/storage/{}/{}/{}'.format( download_url, filename[0], filename[1], filename)) for chunk in response: buffer.write(chunk) checksum, _, filepath = write_raw_content_to_storage(buffer.getvalue(), ext=extension) buffer.close() # Save values to new file object file_obj = models.File( file_format_id=extension, file_size=file_size or default_storage.size(filepath), contentnode=contentnode, assessment_item=assessment_item, language_id=lang_id, preset_id=preset or "", ) file_obj.file_on_disk.name = filepath file_obj.save()
def test_delete_empty_file_reference(self): checksum, _, storage_path = write_raw_content_to_storage( b'some fake PDF data', ext='.pdf') assert default_storage.exists(storage_path), 'file should be saved' delete_empty_file_reference(checksum, 'pdf') assert not default_storage.exists( storage_path), 'file should be deleted'
def create_file_from_contents(contents, ext=None, node=None, preset_id=None): checksum, filename, path = write_raw_content_to_storage(contents, ext=ext) with open(path, 'rb') as new_file: return File.objects.create(file_on_disk=DjFile(new_file), file_format_id=ext, file_size=os.path.getsize(path), checksum=checksum, preset_id=preset_id, contentnode=node)
def create_file_from_contents(contents, ext=None, node=None, preset_id=None): file_object = None checksum, filename, path = write_raw_content_to_storage(contents, ext=ext) with open(path, 'rb') as new_file: return File.objects.create( file_on_disk = DjFile(new_file), file_format_id = ext, file_size = os.path.getsize(path), checksum = checksum, preset_id = preset_id, contentnode = node )
def create_file_from_contents(contents, ext=None, node=None, preset_id=None, uploaded_by=None): checksum, _, path = write_raw_content_to_storage(contents, ext=ext) with default_storage.open(path, 'rb') as new_file: return File.objects.create(file_on_disk=DjFile(new_file), file_format_id=ext, file_size=default_storage.size(path), checksum=checksum, preset_id=preset_id, contentnode=node, uploaded_by=uploaded_by)
def create_file_from_contents(contents, ext=None, node=None, preset_id=None, uploaded_by=None): checksum, _, path = write_raw_content_to_storage(contents, ext=ext) result = File( file_format_id=ext, file_size=default_storage.size(path), checksum=checksum, preset_id=preset_id, contentnode=node, uploaded_by=uploaded_by ) result.file_on_disk.name = path result.save() return result
def create_file_from_contents(contents, ext=None, node=None, preset_id=None, uploaded_by=None): checksum, _, path = write_raw_content_to_storage(contents, ext=ext) result = File( file_format_id=ext, file_size=default_storage.size(path), checksum=checksum, preset_id=preset_id, contentnode=node, uploaded_by=uploaded_by ) result.file_on_disk.name = path result.save() return result