Skip to content

Commit d95b6b7

Browse files
authored
V16: Property editor file upload does not validate file types (#19714)
* fix: forwards the file extensions to the inner dropzone * fix: ensures non-mimetype extensions start with a dot (.) * chore: adds more details on how to set file extensions * feat: adds a bit of styling to code snippets in UFM * fix: return if no file in stream * fix: prevents potential race condition if src changes * chore: minor code improvement
1 parent 4deb756 commit d95b6b7

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

src/Umbraco.Web.UI.Client/src/packages/media/media/components/input-upload-field/input-upload-field.element.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,12 @@ export class UmbInputUploadFieldElement extends UmbLitElement {
3737
* @type {Array<string>}
3838
* @default
3939
*/
40-
@property({
41-
type: Array,
42-
attribute: 'allowed-file-extensions',
43-
converter(value) {
44-
if (typeof value === 'string') {
45-
return value.split(',').map((ext) => ext.trim());
46-
}
47-
return value;
48-
},
49-
})
40+
@property({ type: Array, attribute: 'allowed-file-extensions' })
5041
allowedFileExtensions?: Array<string>;
5142

5243
@state()
5344
public temporaryFile?: UmbTemporaryFileModel;
5445

55-
@state()
56-
private _extensions?: string[];
57-
5846
@state()
5947
private _previewAlias?: string;
6048

@@ -87,7 +75,14 @@ export class UmbInputUploadFieldElement extends UmbLitElement {
8775
}
8876

8977
async #setPreviewAlias(): Promise<void> {
90-
this._previewAlias = await this.#getPreviewElementAlias();
78+
// Store current src to detect changes during async operation
79+
const currentSrc = this.#src;
80+
const alias = await this.#getPreviewElementAlias();
81+
82+
// Only update if src hasn't changed in the meantime (prevents race conditions)
83+
if (this.#src === currentSrc) {
84+
this._previewAlias = alias;
85+
}
9186
}
9287

9388
async #getPreviewElementAlias() {
@@ -107,9 +102,7 @@ export class UmbInputUploadFieldElement extends UmbLitElement {
107102
if (!mimeType) return fallbackAlias;
108103

109104
// Check for an exact match
110-
const exactMatch = manifests.find((manifest) => {
111-
return stringOrStringArrayContains(manifest.forMimeTypes, mimeType);
112-
});
105+
const exactMatch = manifests.find((manifest) => stringOrStringArrayContains(manifest.forMimeTypes, mimeType));
113106
if (exactMatch) return exactMatch.alias;
114107

115108
// Check for wildcard match (e.g. image/*)
@@ -151,6 +144,11 @@ export class UmbInputUploadFieldElement extends UmbLitElement {
151144

152145
this.temporaryFile = (file as UmbUploadableFile).temporaryFile;
153146

147+
if (!this.temporaryFile?.file) {
148+
console.error('No file available for upload');
149+
return;
150+
}
151+
154152
this.#clearObjectUrl();
155153

156154
const blobUrl = URL.createObjectURL(this.temporaryFile.file);
@@ -176,7 +174,7 @@ export class UmbInputUploadFieldElement extends UmbLitElement {
176174
<umb-input-dropzone
177175
standalone
178176
disable-folder-upload
179-
accept=${ifDefined(this._extensions?.join(','))}
177+
accept=${ifDefined(this.allowedFileExtensions ? this.allowedFileExtensions.join(',') : undefined)}
180178
@change=${this.#onUpload}></umb-input-dropzone>
181179
`;
182180
}

src/Umbraco.Web.UI.Client/src/packages/media/media/property-editors/upload-field/Umbraco.UploadField.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const manifest: ManifestPropertyEditorSchema = {
1111
{
1212
alias: 'fileExtensions',
1313
label: 'Accepted file extensions',
14+
description:
15+
'Insert one extension per line, for example `.jpg`.\n\nYou can also use mime types, for example `image/*` or `application/pdf`.',
1416
propertyEditorUiAlias: 'Umb.PropertyEditorUi.AcceptedUploadTypes',
1517
},
1618
],

src/Umbraco.Web.UI.Client/src/packages/media/media/property-editors/upload-field/property-editor-ui-upload-field.element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export class UmbPropertyEditorUIUploadFieldElement extends UmbLitElement impleme
2222
public set config(config: UmbPropertyEditorConfigCollection | undefined) {
2323
if (!config) return;
2424
this._fileExtensions = config.getValueByAlias<Array<string>>('fileExtensions');
25+
if (this._fileExtensions?.length) {
26+
this._fileExtensions = this._fileExtensions.map((ext) =>
27+
ext.startsWith('.') ? ext : ext.includes('/') ? ext : `.${ext}`,
28+
);
29+
}
2530
}
2631

2732
#onChange(event: CustomEvent) {

src/Umbraco.Web.UI.Client/src/packages/ufm/components/ufm-render/ufm-render.element.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ export class UmbUfmRenderElement extends UmbLitElement {
5959
overflow: auto;
6060
}
6161
62+
code {
63+
font-family: var(--uui-font-monospace);
64+
white-space: pre-wrap;
65+
background-color: var(--uui-color-background);
66+
border-radius: var(--uui-border-radius);
67+
padding: 0.2em 0.4em;
68+
}
69+
6270
:host > :first-child {
6371
margin-block-start: 0;
6472
}

0 commit comments

Comments
 (0)