Beispiel #1
0
    def setUp(self):
        self.file_store_path = getattr(local_settings,
                                       'FILE_STORE_PATH', None)

        local_settings.STORAGES_BACKEND = \
            'storages.backends.sftpstorage.SFTPStorage'

        # Setting up a mock for the storage backend based on examples at
        # https://docs.python.org/3.7/library/unittest.mock-examples.html# \
        # applying-the-same-patch-to-every-test-method
        patcher = patch('storages.backends.sftpstorage.SFTPStorage')
        patcher.start()
        self.addCleanup(patcher.stop)

        # Mock the temporary storage object so that we're not unnecessarily
        # saving files to the local file system.
        patcher2 = patch('django.core.files.storage.FileSystemStorage')
        patcher2.start()
        self.addCleanup(patcher2.stop)

        # Reinitialse stored upload after we changed the STORAGES_BACKEND
        # class name above. This ensures that we're looking at the mocked
        # backend class.
        import django_drf_filepond.api
        django_drf_filepond.api.storage_backend_initialised = False
        django_drf_filepond.api._init_storage_backend()
        self.mock_storage_backend = django_drf_filepond.api.storage_backend

        # Set up an initial file upload
        self.upload_id = _get_file_id()
        self.file_id = _get_file_id()
        self.file_content = (b'This is some test file data for an '
                             b'uploaded file.')
        self.fn = 'my_test_file.txt'
        self.test_filename = 'sdf5dua32defh754dhsrr2'

        # Set up the mock_storage_backend.open to return the file content
        # In relation to the changes for #31, file is now a FileField
        # Since only a string is stored to the DB, the creation of the
        # StoredUpload object below is fine but we need to mock the FileField
        # object returned.
        self.mock_storage_backend.open.return_value = BytesIO(
            self.file_content)

        # Override storage object configured for the FileField in StoredUpload
        # at original init time since this happens before setUp is run.
        StoredUpload.file.field.storage = self.mock_storage_backend

        # Now set up a stored version of this upload
        su = StoredUpload(upload_id=self.upload_id,
                          file=('%s' % (self.fn)),
                          uploaded=timezone.now())
        su.file.storage = self.mock_storage_backend
        su.save()
Beispiel #2
0
def _store_upload_local(destination_file_path, destination_file_name,
                        temp_upload):
    file_path_base = local_settings.FILE_STORE_PATH

    # If called via store_upload, this has already been checked but in
    # case this is called directly, double check that the store path is set
    if not file_path_base or file_path_base == '':
        raise ValueError('The FILE_STORE_PATH is not set to a directory.')

    # Is this necessary? Checking on every file storage in case the directory
    # was removed but not sure that this is really necessary.
    if ((not os.path.exists(file_path_base))
            or (not os.path.isdir(file_path_base))):
        raise FileNotFoundError(
            'The local output directory [%s] defined by FILE_STORE_PATH is '
            'missing.' % file_path_base)

    if destination_file_path.startswith(os.sep):
        destination_file_path = destination_file_path[1:]

    target_dir = os.path.join(file_path_base, destination_file_path)
    target_filename = destination_file_name
    # If not filename provided, assume a directory was provided, get the
    # file name from temp_upload and use this
    if not target_filename:
        target_filename = temp_upload.upload_name
    destination_file_path = os.path.join(destination_file_path,
                                         target_filename)

    # Check we're not about to overwrite anything
    target_file_path = os.path.join(target_dir, target_filename)
    if os.path.exists(target_file_path):
        LOG.error('File with specified name and path <%s> already exists' %
                  target_file_path)
        raise FileExistsError('The specified temporary file cannot be stored'
                              ' to the specified location - file exists.')

    su = StoredUpload(upload_id=temp_upload.upload_id,
                      file=destination_file_path,
                      uploaded=temp_upload.uploaded,
                      uploaded_by=temp_upload.uploaded_by)

    try:
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        shutil.copy2(temp_upload.get_file_path(), target_file_path)
        su.save()
        temp_upload.delete()
    except IOError as e:
        LOG.error('Error moving temporary file to permanent storage location')
        raise e

    return su
Beispiel #3
0
    def setUp(self):
        self.storage_backend = local_settings.STORAGES_BACKEND
        # Set storage backend to sftp storage
        local_settings.STORAGES_BACKEND = \
            'storages.backends.sftpstorage.SFTPStorage'

        # Setting up a mock for the storage backend based on examples at
        # https://docs.python.org/3.7/library/unittest.mock-examples.html# \
        # applying-the-same-patch-to-every-test-method
        patcher = patch('storages.backends.sftpstorage.SFTPStorage')
        patcher.start()
        self.addCleanup(patcher.stop)

        # Mock the temporary storage object so that we're not unnecessarily
        # saving files to the local file system.
        patcher2 = patch('django.core.files.storage.FileSystemStorage')
        patcher2.start()
        self.addCleanup(patcher2.stop)
        # Reinitialse stored upload after we changed the STORAGES_BACKEND
        # class name above. This ensures that we're looking at the mocked
        # backend class.
        import django_drf_filepond.api
        self.api = django_drf_filepond.api
        django_drf_filepond.api.storage_backend_initialised = False
        django_drf_filepond.api._init_storage_backend()
        self.mock_storage_backend = django_drf_filepond.api.storage_backend
        self.delete_upload = django_drf_filepond.api.delete_stored_upload

        # Check that we're using a mocked storage backend
        self.assertTrue(
            isinstance(django_drf_filepond.api.storage_backend, MagicMock),
            ('The created storage backend should be mocked but it is not of '
             'type unittest.mock.MagicMock...'))

        # Set up a "StoredUpload"
        self.upload_id = _get_file_id()
        self.file_id = _get_file_id()
        # self.file_content = ('This is some test file data for an '
        #                      'uploaded file.')
        self.fn = 'my_test_file.txt'
        self.test_target_filepath = os.path.join('test_storage', self.fn)
        self.su = StoredUpload(upload_id=self.upload_id,
                               file=self.test_target_filepath,
                               uploaded=timezone.now(),
                               stored=timezone.now())
        self.su.save()
 def setUp(self):
     # Set up an initial file upload
     self.upload_id = _get_file_id()
     self.file_id = _get_file_id()
     self.file_content = ('This is some test file data for an '
                          'uploaded file.')
     self.fn = 'my_test_file.txt'
     self.test_target_filename = '/test_storage/testfile.txt'
     self.su = StoredUpload(upload_id=self.upload_id,
                            file=self.test_target_filename[1:],
                            uploaded=timezone.now())
     self.su.save()
     # Create file
     self.file_store_path = getattr(local_settings, 'FILE_STORE_PATH', None)
     file_full_path = os.path.join(self.file_store_path,
                                   self.test_target_filename[1:])
     if not os.path.exists(os.path.dirname(file_full_path)):
         os.mkdir(os.path.dirname(file_full_path))
     with open(file_full_path, 'w') as f:
         f.write(self.file_content)
Beispiel #5
0
    def setUp(self):
        # Set up an initial file upload
        self.upload_id = _get_file_id()
        self.file_id = _get_file_id()
        self.file_content = ('This is some test file data for an '
                             'uploaded file.')
        self.fn = 'my_test_file.txt'
        self.test_filename = 'sdf5dua32defh754dhsrr2'
        uploaded_file = SimpleUploadedFile(self.fn,
                                           str.encode(self.file_content))
        tu = TemporaryUpload(upload_id=self.upload_id,
                             file_id=self.file_id,
                             file=uploaded_file,
                             upload_name=self.fn,
                             upload_type=TemporaryUpload.FILE_DATA)
        tu.save()

        # Now set up a stored version of this upload
        su = StoredUpload(upload_id=self.upload_id,
                          file_path=('%s' % (self.fn)),
                          uploaded=tu.uploaded)
        su.save()
Beispiel #6
0
def _store_upload_remote(destination_file_path, destination_file_name,
                         temp_upload):
    # Use the storage backend to write the file to the storage backend
    target_filename = destination_file_name
    if not target_filename:
        target_filename = temp_upload.upload_name

    su = None
    destination_file = os.path.join(destination_file_path, target_filename)
    try:
        storage_backend.save(destination_file, temp_upload.file)
        su = StoredUpload(upload_id=temp_upload.upload_id,
                          file_path=destination_file,
                          uploaded=temp_upload.uploaded)
        su.save()
        temp_upload.delete()
    except Exception as e:
        errorMsg = ('Error storing temporary upload to remote storage: [%s]'
                    % str(e))
        LOG.error(errorMsg)
        raise e

    return su
Beispiel #7
0
def store_upload(upload_id, destination_file_path):
    if ((not hasattr(local_settings, 'FILE_STORE_PATH'))
            or (not local_settings.FILE_STORE_PATH)
            or (not os.path.exists(local_settings.FILE_STORE_PATH))
            or (not os.path.isdir(local_settings.FILE_STORE_PATH))):

        raise ImproperlyConfigured('A required setting is missing in your '
                                   'application configuration.')

    file_path_base = local_settings.FILE_STORE_PATH

    if not file_path_base or file_path_base == '':
        raise ValueError('The FILE_STORE_PATH is not set to a directory.')

    id_fmt = re.compile('^([%s]){22}$' % (shortuuid.get_alphabet()))

    if not id_fmt.match(upload_id):
        LOG.error('The provided upload ID <%s> is of an invalid format.' %
                  upload_id)
        raise ValueError('The provided upload ID is of an invalid format.')

    if not destination_file_path or destination_file_path == '':
        raise ValueError('No destination file path provided.')

    if destination_file_path.startswith(os.sep):
        destination_file_path = destination_file_path[1:]

    try:
        tu = TemporaryUpload.objects.get(upload_id=upload_id)
    except TemporaryUpload.DoesNotExist:
        raise ValueError('Record for the specified upload_id doesn\'t exist')

    target_dir = os.path.join(file_path_base,
                              os.path.dirname(destination_file_path))
    if destination_file_path.endswith(os.sep):
        # Assume a directory was provided, get the file name from tu and
        # add this to the provided path.
        destination_file_path += tu.upload_name

    target_file_path = os.path.join(file_path_base, destination_file_path)

    # Check we're not about to overwrite anything
    if os.path.exists(target_file_path):
        LOG.error('File with specified name and path <%s> already exists' %
                  destination_file_path)
        raise FileExistsError('The specified temporary file cannot be stored'
                              ' to the specified location - file exists.')

    su = StoredUpload(upload_id=tu.upload_id,
                      file_path=destination_file_path,
                      uploaded=tu.uploaded)

    try:
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        shutil.copy2(tu.get_file_path(), target_file_path)
        su.save()
        tu.delete()
    except IOError as e:
        LOG.error('Error moving temporary file to permanent storage location')
        raise e

    return su