Dreamine.Web 1.0.0.0
WPF와 Blazor를 한 코드 흐름으로 연결하고 반복적인 MVVM 코드를 줄이는 오픈소스 FullKit 공식 웹 애플리케이션입니다.
로딩중...
검색중...
일치하는것 없음
PlaygroundMediaService.cs
이 파일의 문서화 페이지로 가기
1using System.IO;
2using System.Text.RegularExpressions;
3using Microsoft.AspNetCore.Components.Forms;
4
6
15public sealed class PlaygroundMediaService
16{
25 private static readonly Regex UnsafeName = new(@"[^a-zA-Z0-9._-]+", RegexOptions.Compiled);
34 private readonly string _uploadRoot;
35
45 {
46 _uploadRoot = Path.Combine(AppContext.BaseDirectory, "wwwroot", "uploads", "playground");
47 }
48
105 public async Task<string> SaveAsync(IBrowserFile file, string demoId, string platform, CancellationToken ct = default)
106 {
107 if (file.Size <= 0) throw new InvalidOperationException("Empty files cannot be uploaded.");
108
109 var safeDemoId = SafeSegment(demoId, "demo");
110 var safePlatform = SafeSegment(platform, "platform");
111 var extension = Path.GetExtension(file.Name);
112 if (string.IsNullOrWhiteSpace(extension)) extension = GuessExtension(file.ContentType);
113
114 var targetDir = Path.Combine(_uploadRoot, safeDemoId);
115 Directory.CreateDirectory(targetDir);
116
117 var fileName = $"{safePlatform}-{DateTime.UtcNow:yyyyMMddHHmmss}-{Guid.NewGuid().ToString("N")[..8]}{extension.ToLowerInvariant()}";
118 var path = Path.Combine(targetDir, fileName);
119
120 await using var input = file.OpenReadStream(maxAllowedSize: 200 * 1024 * 1024, cancellationToken: ct);
121 await using var output = File.Create(path);
122 await input.CopyToAsync(output, ct);
123
124 return $"/uploads/playground/{safeDemoId}/{fileName}";
125 }
126
159 private static string SafeSegment(string? value, string fallback)
160 {
161 value = string.IsNullOrWhiteSpace(value) ? fallback : value.Trim();
162 var safe = UnsafeName.Replace(value, "-").Trim('-', '.');
163 return string.IsNullOrWhiteSpace(safe) ? fallback : safe;
164 }
165
190 private static string GuessExtension(string? contentType) => contentType?.ToLowerInvariant() switch
191 {
192 "image/png" => ".png",
193 "image/jpeg" => ".jpg",
194 "image/gif" => ".gif",
195 "image/webp" => ".webp",
196 "image/svg+xml" => ".svg",
197 "video/mp4" => ".mp4",
198 "video/webm" => ".webm",
199 "video/quicktime" => ".mov",
200 _ => ".bin"
201 };
202}
async Task< string > SaveAsync(IBrowserFile file, string demoId, string platform, CancellationToken ct=default)
static string SafeSegment(string? value, string fallback)
static string GuessExtension(string? contentType)