| | | 1 | | using JetBrains.Annotations; |
| | | 2 | | using Refit; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Api.Client.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for <see cref="IApiResponse"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | [UsedImplicitly] |
| | | 10 | | public static class ApiResponseExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Returns the downloaded file name or the specified default file name. |
| | | 14 | | /// </summary> |
| | | 15 | | public static string GetDownloadedFileNameOrDefault(this IApiResponse response, string defaultFileName = "download.b |
| | | 16 | | { |
| | 0 | 17 | | var fileName = defaultFileName; |
| | | 18 | | |
| | 0 | 19 | | if (response.ContentHeaders is null) |
| | 0 | 20 | | return fileName; |
| | | 21 | | |
| | 0 | 22 | | if (!response.ContentHeaders.TryGetValues("content-disposition", out var contentDispositionHeader)) // Only avai |
| | 0 | 23 | | return fileName; |
| | | 24 | | |
| | 0 | 25 | | var contentDisposition = contentDispositionHeader.FirstOrDefault(); |
| | | 26 | | |
| | 0 | 27 | | if (string.IsNullOrWhiteSpace(contentDisposition)) |
| | 0 | 28 | | return fileName; |
| | | 29 | | |
| | | 30 | | // Parse the Content-Disposition header value (e.g., "attachment; filename=workflow-definitions.zip; filename*=U |
| | | 31 | | // Split by semicolon and look for the filename parameter |
| | 0 | 32 | | var parts = contentDisposition.Split(';', StringSplitOptions.RemoveEmptyEntries); |
| | | 33 | | |
| | 0 | 34 | | foreach (var part in parts) |
| | | 35 | | { |
| | 0 | 36 | | var trimmedPart = part.Trim(); |
| | | 37 | | |
| | | 38 | | // Look for filename= (prefer this over filename* for simplicity) |
| | 0 | 39 | | if (!trimmedPart.StartsWith("filename=", StringComparison.OrdinalIgnoreCase) || |
| | 0 | 40 | | trimmedPart.StartsWith("filename*=", StringComparison.OrdinalIgnoreCase)) |
| | | 41 | | continue; |
| | | 42 | | |
| | 0 | 43 | | fileName = trimmedPart["filename=".Length..].Trim('"', '\''); |
| | 0 | 44 | | break; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | return fileName; |
| | | 48 | | } |
| | | 49 | | } |