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 37f33b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 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
14 changes: 5 additions & 9 deletions waterbutler/providers/s3/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def __init__(self, path, headers):

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

@property
def size(self):
Expand Down Expand Up @@ -70,9 +68,7 @@ class S3FileMetadata(S3Metadata, metadata.BaseFileMetadata):

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

@property
def size(self):
Expand Down Expand Up @@ -113,7 +109,7 @@ def name(self):

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


class S3FolderMetadata(S3Metadata, metadata.BaseFolderMetadata):
Expand All @@ -124,8 +120,8 @@ def name(self):

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


Expand Down
32 changes: 14 additions & 18 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 @@ -640,9 +639,14 @@ async def metadata(self, path, revision=None, **kwargs):
await self._check_region()

if path.is_dir:
return (await self._metadata_folder(path))
metadata = await self._metadata_folder(path)
for item in metadata:
item.raw['base_folder'] = self.base_folder
else:
metadata = await self._metadata_file(path, revision=revision)
metadata.raw['base_folder'] = self.base_folder

return (await self._metadata_file(path, revision=revision))
return metadata

async def create_folder(self, path, folder_precheck=True, **kwargs):
"""
Expand All @@ -664,7 +668,9 @@ async def create_folder(self, path, folder_precheck=True, **kwargs):
throws=exceptions.CreateFolderError
)

return S3FolderMetadata({'Prefix': path.path})
metadata = S3FolderMetadata({'Prefix': path.path})
metadata.raw['base_folder'] = self.base_folder
return metadata

async def _metadata_file(self, path, revision=None):
await self._check_region()
Expand All @@ -688,10 +694,7 @@ async def _metadata_file(self, path, revision=None):
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 @@ -726,13 +729,6 @@ async def _metadata_folder(self, path):
if isinstance(prefixes, dict):
prefixes = [prefixes]

# 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]

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

items = [
S3FolderMetadata(item)
for item in prefixes if item['Prefix'] != path.path
Expand Down

0 comments on commit 37f33b1

Please sign in to comment.