S3UploadSettings.cs 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using UnityEditor;
  6. public sealed class S3UploadCredentials
  7. {
  8. public string AccessKey;
  9. public string SecretKey;
  10. public string SessionToken;
  11. }
  12. public static class S3UploadSettings
  13. {
  14. const string MissingConfigurationMessage = "请先填写并保存 S3 上传配置(Bucket、Region、Access Key 和 Secret Key)。";
  15. public const string Profile = "app-runtimes";
  16. public static string Bucket => EditorPrefs.GetString("AppRuntimes.S3.Bucket", "ios-games");
  17. public static string Region => EditorPrefs.GetString("AppRuntimes.S3.Region", "us-east-1");
  18. public static string CredentialsPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".aws", "credentials");
  19. public static S3UploadCredentials LoadCredentials() => ParseProfile(File.Exists(CredentialsPath) ? File.ReadAllText(CredentialsPath) : "", Profile);
  20. public static S3UploadCredentials ParseProfile(string text, string profile)
  21. {
  22. var credentials = new S3UploadCredentials();
  23. bool selected = false;
  24. foreach (var raw in text.Split('\n'))
  25. {
  26. var line = raw.Trim();
  27. if (line.StartsWith("[") && line.EndsWith("]")) { selected = line == "[" + profile + "]"; continue; }
  28. if (!selected || line.StartsWith("#") || line.StartsWith(";")) continue;
  29. int separator = line.IndexOf('=');
  30. if (separator < 0) continue;
  31. var key = line.Substring(0, separator).Trim();
  32. var value = line.Substring(separator + 1).Trim();
  33. if (key == "aws_access_key_id") credentials.AccessKey = value;
  34. if (key == "aws_secret_access_key") credentials.SecretKey = value;
  35. if (key == "aws_session_token") credentials.SessionToken = value;
  36. }
  37. return credentials;
  38. }
  39. public static string UpdateProfile(string text, string profile, S3UploadCredentials credentials)
  40. {
  41. if (!Regex.IsMatch(profile, @"\A[a-zA-Z0-9_-]+\z")) throw new ArgumentException("Invalid AWS profile name.");
  42. ValidateCredentials(credentials);
  43. var output = new StringBuilder();
  44. bool selected = false;
  45. foreach (var raw in text.Split('\n'))
  46. {
  47. var line = raw.Trim();
  48. if (line.StartsWith("[") && line.EndsWith("]")) selected = line == "[" + profile + "]";
  49. if (!selected) output.AppendLine(raw.TrimEnd('\r'));
  50. }
  51. output.AppendLine("[" + profile + "]");
  52. output.AppendLine("aws_access_key_id = " + credentials.AccessKey.Trim());
  53. output.AppendLine("aws_secret_access_key = " + credentials.SecretKey.Trim());
  54. if (!string.IsNullOrWhiteSpace(credentials.SessionToken)) output.AppendLine("aws_session_token = " + credentials.SessionToken.Trim());
  55. return output.ToString();
  56. }
  57. public static void ValidateCredentials(S3UploadCredentials credentials)
  58. {
  59. if (credentials == null || string.IsNullOrWhiteSpace(credentials.AccessKey) || string.IsNullOrWhiteSpace(credentials.SecretKey))
  60. throw new InvalidOperationException(MissingConfigurationMessage);
  61. foreach (var value in new[] { credentials.AccessKey, credentials.SecretKey, credentials.SessionToken })
  62. if (value != null && (value.Contains("\n") || value.Contains("\r"))) throw new ArgumentException("AWS credentials must not contain line breaks.");
  63. }
  64. public static void ValidateDestination(string bucket, string region)
  65. {
  66. if (string.IsNullOrWhiteSpace(bucket) || string.IsNullOrWhiteSpace(region))
  67. throw new ArgumentException(MissingConfigurationMessage);
  68. if (bucket == null || !Regex.IsMatch(bucket, @"\A[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]\z") || bucket.Contains(".."))
  69. throw new ArgumentException("S3 Bucket 格式不正确。");
  70. if (region == null || !Regex.IsMatch(region, @"\A[a-z]{2}(?:-[a-z]+)+-\d+\z"))
  71. throw new ArgumentException("S3 Region 格式不正确。");
  72. }
  73. public static void Save(string bucket, string region, S3UploadCredentials credentials)
  74. {
  75. bucket = bucket.Trim(); region = region.Trim();
  76. ValidateDestination(bucket, region);
  77. var content = UpdateProfile(File.Exists(CredentialsPath) ? File.ReadAllText(CredentialsPath) : "", Profile, credentials);
  78. Directory.CreateDirectory(Path.GetDirectoryName(CredentialsPath));
  79. var temporary = CredentialsPath + "." + Guid.NewGuid().ToString("N") + ".tmp";
  80. try
  81. {
  82. File.WriteAllText(temporary, content, new UTF8Encoding(false));
  83. if (File.Exists(CredentialsPath)) File.Replace(temporary, CredentialsPath, null);
  84. else File.Move(temporary, CredentialsPath);
  85. }
  86. finally { if (File.Exists(temporary)) File.Delete(temporary); }
  87. EditorPrefs.SetString("AppRuntimes.S3.Bucket", bucket);
  88. EditorPrefs.SetString("AppRuntimes.S3.Region", region);
  89. }
  90. }