Skip to content

Commit

Permalink
take base_folder as part of __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Aug 10, 2023
1 parent defd95c commit 3c764e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
12 changes: 1 addition & 11 deletions tests/providers/s3/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,6 @@ async def test_folder(self, provider, mock_time):
assert path.is_dir
assert not path.is_root

@pytest.mark.asyncio
async def test_root(self, provider, mock_time):
provider.settings['id'] = 'that kerning:/'
path = await provider.validate_path('/')
assert path.name == ''
assert not path.is_file
assert path.is_dir
assert path.is_root

@pytest.mark.asyncio
async def test_subfolder(self, provider, mock_time):
path = await provider.validate_path('/')
Expand All @@ -338,7 +329,6 @@ async def test_subfolder(self, provider, mock_time):
assert path.is_dir
assert not path.is_root


class TestCRUD:

@pytest.mark.asyncio
Expand Down Expand Up @@ -458,7 +448,7 @@ async def test_upload_to_subfolder_as_root(self,
metadata, created = await provider.upload(file_stream, path)

assert metadata.kind == 'file'
assert metadata.path == '/my-subfolder/foobah'
assert metadata.path == '/foobah'
assert not created
assert aiohttpretty.has_call(method='PUT', uri=url)
assert aiohttpretty.has_call(method='HEAD', uri=metadata_url)
Expand Down
2 changes: 2 additions & 0 deletions waterbutler/providers/s3/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def name(self):

@property
def path(self):
if self.raw.get('base_folder'):
return '/' + strip_char(self.raw['Key'], self.raw['base_folder'])
return '/' + self.raw['Key']


Expand Down
20 changes: 9 additions & 11 deletions waterbutler/providers/s3/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ def __init__(self, auth, credentials, settings, **kwargs):
self.connection = S3Connection(credentials['access_key'],
credentials['secret_key'], calling_format=OrdinaryCallingFormat())
self.bucket = self.connection.get_bucket(settings['bucket'], validate=False)
self.base_folder = self.settings.get('id', ':/').split(':/')[1]
self.encrypt_uploads = self.settings.get('encrypt_uploads', False)
self.region = None

async def validate_v1_path(self, path, **kwargs):
await self._check_region()

# The user selected base folder, the root of the where that user's node is connected.
path = f"/{self.settings.get('id', ':/').split(':/')[1] + path.lstrip('/')}"
path = f"/{self.base_folder + path.lstrip('/')}"

implicit_folder = path.endswith('/')

Expand Down Expand Up @@ -99,8 +100,7 @@ async def validate_v1_path(self, path, **kwargs):

async def validate_path(self, path, **kwargs):
# The user selected base folder, the root of the where that user's node is connected.
path = f"/{self.settings.get('id', ':/').split(':/')[1] + path.lstrip('/')}"
return WaterButlerPath(path)
return WaterButlerPath(f"/{self.base_folder + path.lstrip('/')}")

def can_duplicate_names(self):
return True
Expand All @@ -116,7 +116,6 @@ async def intra_copy(self, dest_provider, source_path, dest_path):
`dest_provider` must have read access to `source.bucket`.
"""
await self._check_region()

exists = await dest_provider.exists(dest_path)

dest_key = dest_provider.bucket.new_key(dest_path.path)
Expand Down Expand Up @@ -683,15 +682,14 @@ async def _metadata_file(self, path, revision=None):
throws=exceptions.MetadataError,
)
await resp.release()
return S3FileMetadataHeaders(path.path, resp.headers)
metadata = S3FileMetadataHeaders(path.path, resp.headers)
metadata.raw['base_folder'] = self.base_folder
return metadata

async def _metadata_folder(self, path):
await self._check_region()

# The user selected base folder, the root of the where that user's node is connected.
prefix = self.settings['id'].split(':/')[1] if path == '/' and self.settings.get('id') else path.path

params = {'prefix': prefix, 'delimiter': '/'}
params = {'prefix': path.path, 'delimiter': '/'}

resp = await self.make_request(
'GET',
Expand Down Expand Up @@ -728,10 +726,10 @@ async def _metadata_folder(self, path):

# The user selected base folder, the root of the where that user's node is connected.
for item in prefixes:
item['base_folder'] = self.settings.get('id', ':/').split(':/')[1]
item['base_folder'] = self.base_folder

for item in contents:
item['base_folder'] = self.settings.get('id', ':/').split(':/')[1]
item['base_folder'] = self.base_folder

items = [
S3FolderMetadata(item)
Expand Down

0 comments on commit 3c764e0

Please sign in to comment.