From 35a93fa3e82779350eeb41b330c852ee416ed3be Mon Sep 17 00:00:00 2001 From: floatline Date: Sat, 27 Jul 2024 14:40:57 +0000 Subject: [PATCH 1/4] Generate new locale --- nextgisweb/feature_attachment/locale/ru.po | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/nextgisweb/feature_attachment/locale/ru.po b/nextgisweb/feature_attachment/locale/ru.po index db830638d0..07defc0c77 100644 --- a/nextgisweb/feature_attachment/locale/ru.po +++ b/nextgisweb/feature_attachment/locale/ru.po @@ -52,12 +52,12 @@ msgid "File name" msgstr "Имя файла" #: nodepkg/attachment-editor/AttachmentEditor.tsx:172 -#: nodepkg/attachment-table/AttachmentTable.tsx:63 +#: nodepkg/attachment-table/AttachmentTable.tsx:66 msgid "Size" msgstr "Размер" #: nodepkg/attachment-editor/AttachmentEditor.tsx:178 -#: nodepkg/attachment-table/AttachmentTable.tsx:78 +#: nodepkg/attachment-table/AttachmentTable.tsx:81 msgid "Description" msgstr "Описание" @@ -120,7 +120,11 @@ msgstr "Экспорт" msgid "Import" msgstr "Импорт" -#: nodepkg/attachment-table/AttachmentTable.tsx:72 +#: nodepkg/attachment-table/AttachmentTable.tsx:48 +msgid "Empty filename" +msgstr "Пустое имя файла" + +#: nodepkg/attachment-table/AttachmentTable.tsx:75 msgid "MIME type" msgstr "Тип MIME" @@ -144,23 +148,23 @@ msgstr "Некорректный идентификатор объекта дл msgid "Invalid keyname for file '{}'." msgstr "Некорректный ключ для файла '{}'." -#: util.py:74 +#: util.py:75 msgid "Invalid name for file '{}'." msgstr "Некорректное имя для файла '{}'." -#: util.py:80 +#: util.py:82 msgid "Invalid MIME type for file '{}'." msgstr "Некорректный MIME-тип для файла '{}'." -#: util.py:87 +#: util.py:89 msgid "Invalid description for file '{}'." msgstr "Некорректное описание для файла '{}'." -#: util.py:100 +#: util.py:102 msgid "Could not determine feature ID for file '{}'." msgstr "Не удалось определить идентификатор объекта для файла '{}'." -#: util.py:118 +#: util.py:122 msgid "File '{}' isn't found in the archive." msgstr "Файл '{}' не найден в архиве." From 71a2b2070395491b380208524e1703474dfa3baa Mon Sep 17 00:00:00 2001 From: floatline Date: Sat, 27 Jul 2024 14:40:57 +0000 Subject: [PATCH 2/4] Fix the behavior of download and export methods to work with null values as filenames --- nextgisweb/feature_attachment/api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nextgisweb/feature_attachment/api.py b/nextgisweb/feature_attachment/api.py index 2044eba69a..ff1e489b92 100644 --- a/nextgisweb/feature_attachment/api.py +++ b/nextgisweb/feature_attachment/api.py @@ -1,3 +1,4 @@ +import mimetypes import re from io import BytesIO from itertools import count @@ -81,6 +82,8 @@ def download( fobj = FileObj.filter_by(id=fileobj_id).one() response = UnsafeFileResponse(fobj.filename(), content_type=mime_type, request=request) + if name is None: + name = "" response.content_disposition = f"filename*=utf-8''{quote_plus(name)}" return response @@ -188,7 +191,8 @@ def export(resource, request): name = obj.name if name is None or name.strip() == "": - name = f"{att_idx:010d}" + extension = mimetypes.guess_extension(obj.mime_type) + name = f"{att_idx:010d}{extension}" if name in feature_anames: # Make attachment's name unique From 701f6a9897cc7bd1036053ebaac02389bdae5c67 Mon Sep 17 00:00:00 2001 From: floatline Date: Sat, 27 Jul 2024 14:40:57 +0000 Subject: [PATCH 3/4] Use a placeholder for filenames with null values --- .../nodepkg/attachment-table/AttachmentTable.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx b/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx index 9c4c4e4a60..a33c43bc62 100644 --- a/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx +++ b/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx @@ -44,6 +44,9 @@ export function AttachmentTable({ className: "name", title: gettext("File name"), render: (name: string, attachment: FeatureAttachment) => { + if (!name) { + name = "<" + gettext("Empty filename") + ">"; + } const href = routeURL("feature_attachment.download", { id: resourceId, fid: featureId, From 22bbdc0f0f3bdebc3206486b9bbe10dbe8575f2d Mon Sep 17 00:00:00 2001 From: Aleksandr Dezhin Date: Sat, 27 Jul 2024 15:24:48 +0000 Subject: [PATCH 4/4] FIXUP --- nextgisweb/feature_attachment/api.py | 6 +++--- nextgisweb/feature_attachment/locale/ru.po | 12 ++++++------ .../nodepkg/attachment-table/AttachmentTable.tsx | 8 +++----- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/nextgisweb/feature_attachment/api.py b/nextgisweb/feature_attachment/api.py index ff1e489b92..88cba6ddc9 100644 --- a/nextgisweb/feature_attachment/api.py +++ b/nextgisweb/feature_attachment/api.py @@ -1,7 +1,7 @@ -import mimetypes import re from io import BytesIO from itertools import count +from mimetypes import guess_extension from tempfile import NamedTemporaryFile from typing import List, Optional from urllib.parse import quote_plus @@ -83,7 +83,7 @@ def download( fobj = FileObj.filter_by(id=fileobj_id).one() response = UnsafeFileResponse(fobj.filename(), content_type=mime_type, request=request) if name is None: - name = "" + name = f"unnamed{aid}{guess_extension(mime_type) or '.bin'}" response.content_disposition = f"filename*=utf-8''{quote_plus(name)}" return response @@ -191,7 +191,7 @@ def export(resource, request): name = obj.name if name is None or name.strip() == "": - extension = mimetypes.guess_extension(obj.mime_type) + extension = guess_extension(obj.mime_type) or ".bin" name = f"{att_idx:010d}{extension}" if name in feature_anames: diff --git a/nextgisweb/feature_attachment/locale/ru.po b/nextgisweb/feature_attachment/locale/ru.po index 07defc0c77..82bb025ccb 100644 --- a/nextgisweb/feature_attachment/locale/ru.po +++ b/nextgisweb/feature_attachment/locale/ru.po @@ -52,12 +52,12 @@ msgid "File name" msgstr "Имя файла" #: nodepkg/attachment-editor/AttachmentEditor.tsx:172 -#: nodepkg/attachment-table/AttachmentTable.tsx:66 +#: nodepkg/attachment-table/AttachmentTable.tsx:63 msgid "Size" msgstr "Размер" #: nodepkg/attachment-editor/AttachmentEditor.tsx:178 -#: nodepkg/attachment-table/AttachmentTable.tsx:81 +#: nodepkg/attachment-table/AttachmentTable.tsx:78 msgid "Description" msgstr "Описание" @@ -120,11 +120,11 @@ msgstr "Экспорт" msgid "Import" msgstr "Импорт" -#: nodepkg/attachment-table/AttachmentTable.tsx:48 -msgid "Empty filename" -msgstr "Пустое имя файла" +#: nodepkg/attachment-table/AttachmentTable.tsx:55 +msgid "Unnamed" +msgstr "Безымянный" -#: nodepkg/attachment-table/AttachmentTable.tsx:75 +#: nodepkg/attachment-table/AttachmentTable.tsx:72 msgid "MIME type" msgstr "Тип MIME" diff --git a/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx b/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx index a33c43bc62..e623ea2c73 100644 --- a/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx +++ b/nextgisweb/feature_attachment/nodepkg/attachment-table/AttachmentTable.tsx @@ -44,9 +44,6 @@ export function AttachmentTable({ className: "name", title: gettext("File name"), render: (name: string, attachment: FeatureAttachment) => { - if (!name) { - name = "<" + gettext("Empty filename") + ">"; - } const href = routeURL("feature_attachment.download", { id: resourceId, fid: featureId, @@ -54,8 +51,8 @@ export function AttachmentTable({ }); return ( - - {name} + + {name || {gettext("Unnamed")}} ); }, @@ -123,6 +120,7 @@ export function AttachmentTable({ showHeader={!isSmall} size="small" tableLayout="auto" + bordered /> )}