Skip to content

Commit ad82968

Browse files
hotfix image document with no file generated
1 parent 68e7801 commit ad82968

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

src/core/media/documents/document-show/DocumentShowController.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type DocumentData = {
3636
description?: string | null;
3737
documentLanguage?: string | null;
3838
documentType?: { id: string; name: string; code?: string | null } | null;
39+
image?: { size?: number | string | null; name?: string | null; url?: string | null } | null;
3940
file?: { size?: number | string | null; name?: string | null; url?: string | null } | null;
4041
};
4142

src/core/media/documents/document-show/containers/document-show-view/DocumentShowView.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Button } from "../../../../../../shared/components/atoms/button";
55
import { Icon } from "../../../../../../shared/components/atoms/icon";
66
import { Image } from "../../../../../../shared/components/atoms/image";
77
import { Link } from "../../../../../../shared/components/atoms/link";
8-
import { humanFileSize, truncateText } from "../../../../files/media";
8+
import { getFileName, getFileSize, truncateText } from "../../../../files/media";
99
import { Toast } from "../../../../../../shared/modules/toast";
1010
import PreviewDocument from "../../../../files/containers/PreviewDocument.vue";
1111
@@ -21,6 +21,7 @@ const props = defineProps<{
2121
description?: string | null;
2222
documentLanguage?: string | null;
2323
documentType?: { id: string; name: string; code?: string | null } | null;
24+
image?: { size?: number | string | null; name?: string | null; url?: string | null } | null;
2425
file?: { size?: number | string | null; name?: string | null } | null;
2526
}
2627
}>();
@@ -46,7 +47,7 @@ const openPreview = () => {
4647
return;
4748
}
4849
previewDocumentUrl.value = props.document.fileUrl;
49-
previewDocumentName.value = props.document.file?.name || '';
50+
previewDocumentName.value = getFileName(props.document);
5051
previewModalVisible.value = true;
5152
};
5253
@@ -58,8 +59,9 @@ const downloadDocument = () => {
5859
link.href = props.document.fileUrl;
5960
link.target = '_blank';
6061
link.rel = 'noopener noreferrer';
61-
if (props.document.file?.name) {
62-
link.download = props.document.file.name;
62+
const fileName = getFileName(props.document);
63+
if (fileName !== '-') {
64+
link.download = fileName;
6365
}
6466
window.document.body.appendChild(link);
6567
link.click();
@@ -95,11 +97,11 @@ const downloadDocument = () => {
9597
<Flex vertical>
9698
<FlexCell>
9799
<label class="font-semibold block text-sm leading-6 text-gray-900">{{ t('shared.labels.name') }}</label>
98-
<span class="text-gray-900">{{ document.file?.name || '—' }}</span>
100+
<span class="text-gray-900">{{ getFileName(document) }}</span>
99101
</FlexCell>
100102
<FlexCell>
101103
<label class="mt-2 font-semibold block text-sm leading-6 text-gray-900">{{ t('media.media.labels.fileSize') }}</label>
102-
<span class="text-gray-900">{{ document.file?.size ? humanFileSize(document.file.size) : '—' }}</span>
104+
<span class="text-gray-900">{{ getFileSize(document) }}</span>
103105
</FlexCell>
104106
<FlexCell>
105107
<label class="mt-2 font-semibold block text-sm leading-6 text-gray-900">{{ t('media.documents.labels.fileUrl') }}</label>

src/core/media/files/media.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,53 @@ export const getRouteName = (type: string) => {
2828
}
2929
};
3030

31-
export const getFileName = (media: any) => {
32-
const cleanFileName = (name) => name.replace(/^images\//, '');
31+
const cleanFileName = (name?: string | null) => (name || '').replace(/^images\//, '');
32+
33+
const getNameFromUrl = (value?: string | null) => {
34+
if (!value) {
35+
return '';
36+
}
37+
const cleanValue = value.split('#')[0].split('?')[0];
38+
return cleanValue.split('/').pop() || cleanValue;
39+
};
40+
41+
const getDocumentBinary = (media: any) => {
42+
if (media?.type !== TYPE_DOCUMENT) {
43+
return null;
44+
}
45+
46+
if (media?.file) {
47+
return media.file;
48+
}
49+
50+
if (media?.isDocumentImage && media?.image) {
51+
return media.image;
52+
}
3353

34-
if (media.type === TYPE_IMAGE) return truncateText(cleanFileName(media.image.name), 25);
35-
if (media.type === TYPE_DOCUMENT) return truncateText(cleanFileName(media.file.name), 25);
36-
if (media.type === TYPE_VIDEO) return truncateText(media.videoUrl, 25);
54+
return null;
55+
};
56+
57+
export const getFileName = (media: any) => {
58+
if (media?.type === TYPE_IMAGE) {
59+
const fileName = cleanFileName(media?.image?.name) || cleanFileName(getNameFromUrl(media?.imageWebUrl));
60+
return fileName ? truncateText(fileName, 25) : '-';
61+
}
62+
if (media?.type === TYPE_DOCUMENT) {
63+
const documentBinary = getDocumentBinary(media);
64+
const fileName = cleanFileName(documentBinary?.name)
65+
|| cleanFileName(getNameFromUrl(documentBinary?.url || media?.fileUrl))
66+
|| cleanFileName(media?.title);
67+
return fileName ? truncateText(fileName, 25) : '-';
68+
}
69+
if (media?.type === TYPE_VIDEO) {
70+
return media?.videoUrl ? truncateText(media.videoUrl, 25) : '-';
71+
}
3772
return '-';
3873
};
3974

4075
export const getFileSize = (media: any) => {
41-
if (media.type === TYPE_IMAGE) return humanFileSize(media.image.size);
42-
if (media.type === TYPE_DOCUMENT) return humanFileSize(media.file.size);
76+
if (media?.type === TYPE_IMAGE) return humanFileSize(media?.image?.size);
77+
if (media?.type === TYPE_DOCUMENT) return humanFileSize(getDocumentBinary(media)?.size);
4378
return '-';
4479
};
4580

@@ -52,10 +87,19 @@ export const formatDate = (dateString) => {
5287
};
5388

5489
export const humanFileSize = (bytes, si=false, dp=1) => {
90+
if (bytes === null || bytes === undefined || bytes === '') {
91+
return '-';
92+
}
93+
94+
let normalizedBytes = typeof bytes === 'number' ? bytes : Number(bytes);
95+
if (Number.isNaN(normalizedBytes)) {
96+
return '-';
97+
}
98+
5599
const thresh = si ? 1000 : 1024;
56100

57-
if (Math.abs(bytes) < thresh) {
58-
return bytes + ' B';
101+
if (Math.abs(normalizedBytes) < thresh) {
102+
return normalizedBytes + ' B';
59103
}
60104

61105
const units = si
@@ -65,10 +109,10 @@ export const humanFileSize = (bytes, si=false, dp=1) => {
65109
const r = 10**dp;
66110

67111
do {
68-
bytes /= thresh;
112+
normalizedBytes /= thresh;
69113
++u;
70-
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
114+
} while (Math.round(Math.abs(normalizedBytes) * r) / r >= thresh && u < units.length - 1);
71115

72116

73-
return bytes.toFixed(dp) + ' ' + units[u];
117+
return normalizedBytes.toFixed(dp) + ' ' + units[u];
74118
}

src/shared/api/queries/media.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ export const fileQuery = gql`
173173
id
174174
type
175175
fileUrl
176+
isDocumentImage
176177
updatedAt
178+
image {
179+
name
180+
size
181+
url
182+
}
177183
file {
178184
name
179185
size
@@ -212,6 +218,11 @@ export const getFileQuery = gql`
212218
fileUrl
213219
imageWebUrl
214220
isDocumentImage
221+
image {
222+
size
223+
name
224+
url
225+
}
215226
title
216227
description
217228
documentLanguage
@@ -252,6 +263,7 @@ export const mediaProductThroughQuery = gql`
252263
id
253264
proxyId
254265
type
266+
isDocumentImage
255267
imageType
256268
updatedAt
257269
title

0 commit comments

Comments
 (0)