Skip to content

Commit

Permalink
[Fixes #12732] Asset file migration for documents does not work for d… (
Browse files Browse the repository at this point in the history
#12733) (#12736)

* [Fixes #12732] Asset file migration for documents does not work for document

* [Fixes #12732] Asset file migration for documents does not work for document

(cherry picked from commit 0b792cf)

Co-authored-by: mattiagiupponi <[email protected]>
  • Loading branch information
github-actions[bot] and mattiagiupponi authored Nov 25, 2024
1 parent f45c0a5 commit 3a3c061
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
21 changes: 17 additions & 4 deletions geonode/assets/management/commands/migrate_file_to_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,19 @@ def handle(self, **options):

logger.info("Moving file to the asset folder")

dest = shutil.move(source, handler._create_asset_dir())

logger.info("Fixing perms")
if len(asset.location) == 1:
# In older installations, all documents are stored in a single folder.
# Instead of moving the entire folder, we can simply move the individual document.
# This approach prevents the risk of breaking the other documents
# that are stored in the same folder
# oldpath = {MEDIA_ROOT}/documents/document/file.extension
dest = shutil.move(asset.location[0], handler._create_asset_dir())
else:
dest = shutil.move(source, handler._create_asset_dir())

logger.info(f"New destination path: {dest}")

logger.info("Fixing file/folder perms if required")
if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None:
os.chmod(os.path.dirname(dest), settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS)

Expand All @@ -113,7 +123,10 @@ def handle(self, **options):

logger.info("Updating location field with new folder value")

asset.location = [x.replace(source, dest) for x in asset.location]
if len(asset.location) == 1:
asset.location = dest
else:
asset.location = [x.replace(source, dest) for x in asset.location]
asset.save()

logger.info("Checking if geoserver should be updated")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by Django 4.2.9 on 2024-03-12 11:55
import itertools
import logging
import os

Expand All @@ -10,7 +11,7 @@

from geonode.base.models import Link
from geonode.assets.models import LocalAsset
from geonode.utils import build_absolute_uri
from geonode.utils import build_absolute_uri, get_supported_datasets_file_types

logger = logging.getLogger(__name__)

Expand All @@ -24,9 +25,9 @@ def get_ext(filename):
logger.warning(f"Could not find extension for Resource '{res_hm.title}, file '{filename}': {e}")
return None

ResourceBase_hm = apps.get_model('base', 'ResourceBase')
Dataset_hm = apps.get_model('layers', 'Dataset')
Document_hm = apps.get_model('documents', 'Document')
ResourceBase_hm = apps.get_model("base", "ResourceBase")
Dataset_hm = apps.get_model("layers", "Dataset")
Document_hm = apps.get_model("documents", "Document")

if hasattr(ResourceBase_hm, "files"):
# looping on available resources with files to generate the LocalAssets
Expand All @@ -37,12 +38,7 @@ def get_ext(filename):

files = res_hm.files
# creating the local asset object
asset = LocalAsset(
title="Files",
description="Original uploaded files",
owner=owner,
location=files
)
asset = LocalAsset(title="Files", description="Original uploaded files", owner=owner, location=files)
asset.save()

### creating the association between asset and Link
Expand All @@ -60,10 +56,14 @@ def get_ext(filename):
ext = get_ext(files[0])
else:
ext = None
supported_file_types = get_supported_datasets_file_types()
for file in files:
for filetype in settings.SUPPORTED_DATASET_FILE_TYPES:
for filetype in supported_file_types:
file_ext = get_ext(file)
if file_ext in filetype["ext"]:
_ext = list(
itertools.chain.from_iterable(y for y in [x["required_ext"] for x in filetype["formats"]])
)
if file_ext in _ext:
ext = filetype["id"]
break
if ext:
Expand All @@ -75,14 +75,13 @@ def get_ext(filename):
link_type="uploaded",
name="Original upload",
extension=ext or "unknown",
url=url
url=url,
)


class Migration(migrations.Migration):

dependencies = [

("base", "0091_create_link_asset_alter_link_type"),
]

Expand Down

0 comments on commit 3a3c061

Please sign in to comment.