GlobalConstants.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using AipGateway.API.Domain.Models.Request;
  2. using Microsoft.AspNetCore.Http;
  3. namespace AipGateway.API.Application
  4. {
  5. public class GlobalConstants
  6. {
  7. public static readonly string API_PREFIX = "/api/v1";
  8. public static readonly string API_AIP = API_PREFIX + "/aip";
  9. public static readonly string API_DB = API_PREFIX + "/db";
  10. public static readonly string API_FILE = API_PREFIX + "/file";
  11. public static readonly string API_STREAM = API_PREFIX + "/stream";
  12. public static readonly string API_IP_ADDRESS = "api-ip-address";
  13. public static readonly string API_START_TM = "api-start-tm";
  14. public static readonly string API_START_MILLISECONDS = "api-start-milliseconds";
  15. public static readonly string API_GUID = "api-guid";
  16. public static readonly string API_ID = "api-id";
  17. public static readonly string API_KEY = "api-key";
  18. public static readonly string API_DECRYPT_KEY = "api-decrypt-key";
  19. public static readonly string API_JOB_OWNER = "api-job-owner";
  20. public static readonly string API_RESULT_CODE = "api-result-code";
  21. public static readonly string API_RESULT_MESSAGE = "api-result-message";
  22. public static readonly string API_REQUEST = "api-request";
  23. public static readonly string API_RESULT = "api-result";
  24. public static readonly int API_DB_RELOAD = 1;
  25. public static readonly int API_DB_LINKED_SYSTEMS = 3;
  26. public static readonly int API_DB_LINKED_SERVERS = 4;
  27. public static readonly int API_DB_LINKED_API_KEYS = 5;
  28. public static readonly int API_DB_LINKED_DECRYPT_KEYS = 6;
  29. public static readonly int API_AIP_DOWNLOAD = 10;
  30. public static readonly int API_AIP_LABELS = 11;
  31. public static readonly int API_AIP_POLICIES = 12;
  32. public static readonly int API_AIP_PROTECTIONS = 13;
  33. public static readonly int API_FILE_INFO = 21;
  34. public static readonly int API_FILE_SET_LABEL = 22;
  35. public static readonly int API_FILE_DELETE_LABEL = 23;
  36. public static readonly int API_FILE_SET_PROTECTION = 24;
  37. public static readonly int API_FILE_DELETE_PROTECTIN = 25;
  38. public static readonly int API_FILE_SET_LABEL_PROTECTION = 26;
  39. public static readonly int API_FILE_DELETE_LABEL_PROTECTION = 27;
  40. public static readonly int API_FILE_ENCRYPT = 28;
  41. public static readonly int API_FILE_DECRYPT = 29;
  42. public static readonly int API_FILE_SET_LABELS = 30;
  43. public static readonly int API_FILE_DELETE_LABELS = 31;
  44. public static readonly int API_STREAM_INFO = 41;
  45. public static readonly int API_STREAM_SET_LABEL = 42;
  46. public static readonly int API_STREAM_DELETE_LABEL = 43;
  47. public static readonly int API_STREAM_SET_PROTECTION = 44;
  48. public static readonly int API_STREAM_DELETE_PROTECTIN = 45;
  49. public static readonly int API_STREAM_SET_LABEL_PROTECTION = 46;
  50. public static readonly int API_STREAM_DELETE_LABEL_PROTECTION = 67;
  51. public static readonly int API_STREAM_ENCRYPT = 48;
  52. public static readonly int API_STREAM_DECRYPT = 49;
  53. public static readonly int API_STREAM_SET_LABELS = 50;
  54. public static readonly int API_STREAM_DELETE_LABELS = 51;
  55. public static readonly int API_DUMMY = 99;
  56. public static readonly int API_FILE_DATA = 13;
  57. public static readonly string API_RESULT_SUCCESS = "Success";
  58. public static readonly string API_RESULT_FAIL = "Fail";
  59. public static readonly string API_RESULT_ERROR = "Error";
  60. public static string GetApiGuid(HttpContext context)
  61. {
  62. string? guid = context.Items[API_GUID] as string;
  63. if (guid == null)
  64. {
  65. guid = Guid.NewGuid().ToString();
  66. context.Items.Add(API_GUID, guid);
  67. }
  68. return guid;
  69. }
  70. public static void SetAuthorization(HttpContext context, int apiId, string apiKey, RequestBase? req)
  71. {
  72. context.Items[API_ID] = apiId.ToString();
  73. context.Items[API_KEY] = apiKey;
  74. if (req != null)
  75. {
  76. context.Items[API_DECRYPT_KEY] = req.decryptKey;
  77. context.Items[API_JOB_OWNER] = req.email;
  78. req.apiGuid = GetApiGuid(context);
  79. }
  80. }
  81. public static string getAttributeStr(HttpContext context, string attribute)
  82. {
  83. string? value = context.Items[attribute] as string;
  84. if (value == null)
  85. {
  86. return "";
  87. }
  88. return value;
  89. }
  90. public static int getAttributeInt(HttpContext context, string attribute)
  91. {
  92. string? value = context.Items[attribute] as string;
  93. if (value == null)
  94. {
  95. return -1;
  96. }
  97. int.TryParse(value, out int numValue);
  98. return numValue;
  99. }
  100. public static long getAttributeLong(HttpContext context, string attribute)
  101. {
  102. string? value = context.Items[attribute] as string;
  103. if (value == null)
  104. {
  105. return -1;
  106. }
  107. long.TryParse(value, out long numValue);
  108. return numValue;
  109. }
  110. public static int GetApiId(HttpContext context)
  111. {
  112. //int apiId = -1;
  113. string? temp = context.Items[API_ID] as string;
  114. if (temp == null)
  115. {
  116. return -1;
  117. }
  118. int.TryParse(temp, out int apiId);
  119. return apiId;
  120. }
  121. public static string GetApiKey(HttpContext context)
  122. {
  123. string? key = context.Items[API_KEY] as string;
  124. if (key == null)
  125. {
  126. key = "x";
  127. }
  128. return key;
  129. }
  130. }
  131. }