Skip to content

Commit

Permalink
Allow customizing attachments namespace dir
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Oct 8, 2024
1 parent f23a6a0 commit 8c174e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
12 changes: 8 additions & 4 deletions schemas/qwc-data-service.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
"description": "Base dir where attachments are stored",
"type": "string"
},
"attachments_namespace_dir": {
"description": "The namespace dir, below attachments_base_dir. You can use {tenant}, {map}, {dataset}. Default is '{tenant}/{map}.{dataset}'",
"type": "string"
},
"attachment_store_pattern": {
"description": "The path pattern to use when saving attachments to disk. You can use {random}, {filename}, {ext} and {<field>}, where field refers to the value of the field <field> of the committed feature. Default is '{random}/{filename}'",
"type": "string"
},
"allowed_attachment_extensions": {
"description": "Comma separated list of allowed attachment file extensions, i.e. '.png,.jpg'",
"type": "string"
Expand Down Expand Up @@ -75,10 +83,6 @@
"max_attachment_file_size_per_dataset": {
"description": "Lookup of maximum attachment file size in bytes per dataset",
"type": "object"
},
"attachment_store_pattern": {
"description": "The path pattern to use when saving attachments to disk. You can use {random}, {filename}, {ext} and {<field>}, where field refers to the respective field value of the committed feature. Default is '{random}/{filename}'",
"type": "string"
}
}
},
Expand Down
24 changes: 21 additions & 3 deletions src/attachments_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, tenant, logger):
self.allowed_extensions_per_dataset = config.get(
'allowed_extensions_per_dataset', {}
)
self.attachments_namespace_dir = config.get('attachments_namespace_dir', "{tenant}/{map}.{dataset}")
self.attachment_store_pattern = config.get('attachment_store_pattern', "{random}/{filename}")
for dataset in self.allowed_extensions_per_dataset:
self.allowed_extensions_per_dataset[dataset] = self.allowed_extensions_per_dataset[dataset].split(",")
Expand Down Expand Up @@ -134,7 +135,11 @@ def save_attachment(self, dataset, file, fields):
ext=os.path.splitext(file.filename)[1],
**fields
)
target_path = os.path.join(self.attachments_base_dir, self.tenant, dataset, slug)
target_path = os.path.join(
self.attachments_base_dir,
self.namespace_dir(dataset),
slug
)

# create target dir
target_dir = os.path.dirname(target_path)
Expand All @@ -156,7 +161,9 @@ def remove_attachment(self, dataset, slug):
:param slug: File slug (identifier)
"""
target_dir = os.path.join(
self.attachments_base_dir, self.tenant, dataset)
self.attachments_base_dir,
self.namespace_dir(dataset)
)
try:
os.remove(os.path.join(target_dir, slug))
self.logger.info("Removed attachment: %s" % slug)
Expand All @@ -175,7 +182,10 @@ def resolve_attachment(self, dataset, slug):
"""Resolve attachment slug to full path"""
path = os.path.realpath(
os.path.join(
self.attachments_base_dir, self.tenant, dataset, slug))
self.attachments_base_dir,
self.namespace_dir(dataset),
slug)
)
if os.path.isfile(path) and path.startswith(
self.attachments_base_dir):
return path
Expand All @@ -188,3 +198,11 @@ def generate_slug(self, length):
"""
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for c in range(length))

def namespace_dir(self, dataset):
mapname, datasetname = dataset.split(".", 1)
return self.attachments_namespace_dir.format(
tenant=self.tenant,
map=mapname,
dataset=datasetname
)

0 comments on commit 8c174e8

Please sign in to comment.