-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathFileSystem.windows.cs
More file actions
234 lines (200 loc) · 6.74 KB
/
Copy pathFileSystem.windows.cs
File metadata and controls
234 lines (200 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Windows.Storage;
using Package = Windows.ApplicationModel.Package;
namespace Microsoft.Maui.Storage
{
partial class FileSystemImplementation : IFileSystem
{
private readonly Lazy<string> _platformCacheDirectory = new(valueFactory: () =>
{
if (AppInfoUtils.IsPackagedApp)
{
return ApplicationData.Current.LocalCacheFolder.Path;
}
else
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Cache");
if (!File.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
});
private readonly Lazy<string> _platformAppDataDirectory = new(valueFactory: () =>
{
if (AppInfoUtils.IsPackagedApp)
{
return ApplicationData.Current.LocalFolder.Path;
}
else
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Data");
if (!File.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
});
static string CleanPath(string path) =>
string.Join("_", path.Split(Path.GetInvalidFileNameChars()));
static string AppSpecificPath =>
Path.Combine(CleanPath(AppInfoImplementation.PublisherName), CleanPath(AppInfo.PackageName));
string PlatformCacheDirectory => _platformCacheDirectory.Value;
string PlatformAppDataDirectory => _platformAppDataDirectory.Value;
Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
{
if (filename == null)
throw new ArgumentNullException(nameof(filename));
if (AppInfoUtils.IsPackagedApp)
{
filename = FileSystemUtils.NormalizePath(filename);
return Package.Current.InstalledLocation.OpenStreamForReadAsync(filename);
}
else
{
var file = FileSystemUtils.PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult((Stream)File.OpenRead(file));
}
}
Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
var file = FileSystemUtils.PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult(File.Exists(file));
}
}
public partial class FileBase
{
// Static mapping for file extensions to MIME types
// Used as fallback when Windows StorageFile.ContentType doesn't provide correct MIME type
static readonly FrozenDictionary<string, string> ExtensionToMimeTypeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
// Image formats
{ ".jpg", MediaTypeNames.Image.Jpeg },
{ ".jpeg", MediaTypeNames.Image.Jpeg },
{ ".png", "image/png" },
{ ".gif", MediaTypeNames.Image.Gif },
{ ".bmp", "image/bmp" },
{ ".svg", "image/svg+xml" },
{ ".webp", "image/webp" },
{ ".tiff", MediaTypeNames.Image.Tiff },
{ ".tif", MediaTypeNames.Image.Tiff },
{ ".ico", "image/x-icon" },
// Audio formats
{ ".mp3", "audio/mpeg" },
{ ".wav", "audio/wav" },
{ ".flac", "audio/flac" },
{ ".aac", "audio/aac" },
{ ".ogg", "audio/ogg" },
{ ".wma", "audio/x-ms-wma" },
// Video formats
{ ".mp4", "video/mp4" },
{ ".avi", "video/x-msvideo" },
{ ".mov", "video/quicktime" },
{ ".wmv", "video/x-ms-wmv" },
{ ".webm", "video/webm" },
{ ".mkv", "video/x-matroska" },
{ ".flv", "video/x-flv" },
// Document formats
{ ".pdf", MediaTypeNames.Application.Pdf },
{ ".doc", "application/msword" },
{ ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ ".xls", "application/vnd.ms-excel" },
{ ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
{ ".ppt", "application/vnd.ms-powerpoint" },
{ ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
{ ".txt", MediaTypeNames.Text.Plain },
{ ".rtf", MediaTypeNames.Application.Rtf },
// Web formats
{ ".html", MediaTypeNames.Text.Html },
{ ".htm", MediaTypeNames.Text.Html },
{ ".css", "text/css" },
{ ".js", "application/javascript" },
{ ".json", "application/json" },
{ ".xml", MediaTypeNames.Text.Xml },
// Archive formats
{ ".zip", MediaTypeNames.Application.Zip },
{ ".rar", "application/x-rar-compressed" },
{ ".7z", "application/x-7z-compressed" },
{ ".tar", "application/x-tar" },
{ ".tar.gz", "application/gzip" },
{ ".gz", "application/gzip" }
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
internal FileBase(IStorageFile file)
: this(file?.Path)
{
File = file;
// Set the ContentType, but prefer our mapping for known problematic extensions
var fileContentType = file?.ContentType;
var extension = Path.GetExtension(file?.Path ?? "");
// If Windows provides a generic "application/octet-stream" or empty ContentType,
// try to get a more specific MIME type from our extension mapping
if (string.IsNullOrWhiteSpace(fileContentType) ||
fileContentType == "application/octet-stream")
{
var betterContentType = PlatformGetContentType(extension);
ContentType = betterContentType ?? fileContentType;
}
else
{
ContentType = fileContentType;
}
}
void PlatformInit(FileBase file)
{
File = file.File;
}
internal IStorageFile File { get; set; }
// Use extension mapping as fallback when Windows doesn't provide correct MIME type
string PlatformGetContentType(string extension)
{
if (string.IsNullOrWhiteSpace(extension))
return null;
// Trim whitespace and convert to lowercase for consistent matching
extension = extension.Trim().ToLowerInvariant();
if (string.IsNullOrEmpty(extension))
return null;
if (!extension.StartsWith("."))
extension = "." + extension;
// Try exact match first
if (ExtensionToMimeTypeMap.TryGetValue(extension, out var mimeType))
return mimeType;
var fileName = Path.GetFileNameWithoutExtension(extension);
if (!string.IsNullOrEmpty(fileName) && fileName.Contains('.', StringComparison.Ordinal))
{
// Try progressively longer extensions (e.g., .tar.gz, then .gz)
var parts = fileName.Split('.');
for (int i = parts.Length - 1; i >= 0; i--)
{
var compoundExtension = "." + string.Join(".", parts.Skip(i)) + extension;
if (ExtensionToMimeTypeMap.TryGetValue(compoundExtension, out mimeType))
return mimeType;
}
}
return null;
}
internal async virtual Task<Stream> PlatformOpenReadAsync()
{
if (File is null && FullPath is not null)
{
File = await StorageFile.GetFileFromPathAsync(FullPath);
}
return await File.OpenStreamForReadAsync();
}
}
public partial class FileResult
{
internal FileResult(IStorageFile file)
: base(file)
{
}
}
}