Skip to content

Commit

Permalink
Merge pull request #14007 from unoplatform/dev/mazi/wasm-fileext
Browse files Browse the repository at this point in the history
fix: Ensure browser does not truncate file name
  • Loading branch information
MartinZikmund authored Oct 18, 2023
2 parents 0b444c5 + 810f523 commit 3e74597
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Uno.UI/ts/Windows/Storage/Pickers/FileSavePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
types: [],
};

if (suggestedFileName != "") {
options.suggestedName = suggestedFileName;
}

const acceptTypes = <Uno.Storage.Pickers.NativeFilePickerAcceptType[]>JSON.parse(fileTypesJson);

for (const acceptType of acceptTypes) {
for (const acceptType of acceptTypes) {
const pickerAcceptType: FilePickerAcceptType = {
accept: {},
description: acceptType.description,
Expand All @@ -42,6 +38,20 @@
options.types.push(pickerAcceptType);
}

if (suggestedFileName != "") {
// In case the suggested file name does not end with any extension provided by the app
// we attach the first one if such exists. This is because JS could otherwise truncate
// the filename incorrectly, e.g.:
// "this.is.a.filename" would get truncated to "this"
var lowerCaseFileName = suggestedFileName.toLowerCase();
if (!acceptTypes.some(f => f.accept.some(a => a.extensions.some(e => lowerCaseFileName.endsWith(e.toLowerCase())))) &&
acceptTypes.length > 0) {
suggestedFileName += acceptTypes[0].accept[0].extensions[0];
}

options.suggestedName = suggestedFileName;
}

try {
const selectedFile = await showSaveFilePicker(options);
const info = Uno.Storage.NativeStorageItem.getInfos(selectedFile)[0];
Expand Down

0 comments on commit 3e74597

Please sign in to comment.