示例#1
0
        private async void MessageSaveExecute(Message message)
        {
            var result = message.GetFileAndName(true);

            var file = result.File;

            if (file == null || !file.Local.IsDownloadingCompleted)
            {
                return;
            }

            var cached = await ProtoService.GetFileAsync(file);

            if (cached == null)
            {
                return;
            }

            var fileName = result.FileName;

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = System.IO.Path.GetFileName(file.Local.Path);
            }

            var clean = ProtoService.Execute(new CleanFileName(fileName));

            if (clean is Text text && !string.IsNullOrEmpty(text.TextValue))
            {
                fileName = text.TextValue;
            }

            var extension = System.IO.Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(extension))
            {
                extension = ".dat";
            }

            var picker = new FileSavePicker();

            picker.FileTypeChoices.Add($"{extension.TrimStart('.').ToUpper()} File", new[] { extension });
            picker.SuggestedStartLocation = PickerLocationId.Downloads;
            picker.SuggestedFileName      = fileName;

            try
            {
                var picked = await picker.PickSaveFileAsync();

                if (picked != null)
                {
                    await cached.CopyAndReplaceAsync(picked);
                }
            }
            catch { }
        }