Skip to content

Commit

Permalink
[Fixes #10142] storage_manager copy dont assign the folder/file permi… (
Browse files Browse the repository at this point in the history
#10143) (#10154)

* [Fixes #10142] storage_manager copy dont assign the folder/file permissions correcly

* [Fixes #10142] storage_manager copy dont assign the folder/file permissions correcly

* [Fixes #10142] storage_manager copy dont assign the folder/file permissions correcly

* [Fixes #10142] storage_manager copy dont assign the folder/file permissions correcly

* [Fixes #10142] storage_manager copy dont assign the folder/file permissions correcly

Co-authored-by: mattiagiupponi <51856725+mattiagiupponi@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and mattiagiupponi authored Oct 17, 2022
1 parent 3fc43a7 commit 237437d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 10 additions & 1 deletion geonode/storage/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ def copy_files_list(self, files: List[str]):
out = []
random_suffix = f'{uuid1().hex[:8]}'
new_path = mkdtemp()

if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None:
# value is always set by default as None
# https://docs.djangoproject.com/en/3.2/ref/settings/#file-upload-directory-permissions
os.chmod(new_path, settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS)
_new_path = None
for f in files:
with self.open(f, 'rb+') as open_file:
old_file_name, _ = os.path.splitext(os.path.basename(f))
Expand All @@ -199,7 +205,10 @@ def copy_files_list(self, files: List[str]):
new_file = f"{new_path}/{suffixed_name}{ext}"
else:
new_file = f"{new_path}/{old_file_name}_{random_suffix}{ext}"
out.append(self.copy_single_file(open_file, new_file))
_new_path = self.copy_single_file(open_file, new_file)
out.append(_new_path)
if _new_path:
os.chmod(_new_path, settings.FILE_UPLOAD_PERMISSIONS)
return out

def copy_single_file(self, old_file: BinaryIO, new_file: str):
Expand Down
19 changes: 18 additions & 1 deletion geonode/storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from geonode.storage.gcs import GoogleStorageManager
from geonode.storage.dropbox import DropboxStorageManager
from geonode.base.populate_test_data import create_single_dataset
from geonode.tests.base import GeoNodeBaseTestSupport


class TestDropboxStorageManager(SimpleTestCase):
Expand Down Expand Up @@ -281,7 +282,7 @@ def test_aws_size(self, aws):
aws.assert_called_once_with('name')


class TestStorageManager(TestCase):
class TestStorageManager(GeoNodeBaseTestSupport):

def setUp(self):
self.sut = StorageManager
Expand Down Expand Up @@ -400,6 +401,22 @@ def test_storage_manager_replace_single_file(self, path, strg):
output = self.sut().replace(dataset, new_file)
self.assertListEqual([expected], output['files'])

@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o777)
@override_settings(FILE_UPLOAD_PERMISSIONS=0o777)
def test_storage_manager_copy(self):
'''
Test that the copy works as expected and the permissions are corerct
'''
dataset = create_single_dataset(name="test_copy")
dataset.files = [os.path.join(f"{self.project_root}", "tests/data/test_sld.sld")]
dataset.save()
output = self.sut().copy(dataset)

self.assertTrue(os.path.exists(output.get("files")[0]))
self.assertEqual(os.stat(os.path.exists(output.get("files")[0])).st_mode, 8592)
os.remove(output.get("files")[0])
self.assertFalse(os.path.exists(output.get("files")[0]))


class TestDataRetriever(TestCase):
@classmethod
Expand Down

0 comments on commit 237437d

Please sign in to comment.