租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.6 KiB

6 months ago
  1. /* eslint-disable no-use-before-define */
  2. const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
  3. const { isArray } = require('../utils/isArray');
  4. const { deepCopy } = require('../utils/deepCopy');
  5. const { isObject } = require('../utils/isObject');
  6. const { obj2xml } = require('../utils/obj2xml');
  7. const { checkObjectTag } = require('../utils/checkObjectTag');
  8. const { getStrBytesCount } = require('../utils/getStrBytesCount');
  9. const proto = exports;
  10. proto.putBucketLifecycle = async function putBucketLifecycle(name, rules, options) {
  11. _checkBucketName(name);
  12. if (!isArray(rules)) {
  13. throw new Error('rules must be Array');
  14. }
  15. const params = this._bucketRequestParams('PUT', name, 'lifecycle', options);
  16. const Rule = [];
  17. const paramXMLObj = {
  18. LifecycleConfiguration: {
  19. Rule
  20. }
  21. };
  22. rules.forEach(_ => {
  23. defaultDaysAndDate2Expiration(_); // todo delete, 兼容旧版本
  24. checkRule(_);
  25. if (_.id) {
  26. _.ID = _.id;
  27. delete _.id;
  28. }
  29. Rule.push(_);
  30. });
  31. const paramXML = obj2xml(paramXMLObj, {
  32. headers: true,
  33. firstUpperCase: true
  34. });
  35. params.content = paramXML;
  36. params.mime = 'xml';
  37. params.successStatuses = [200];
  38. const result = await this.request(params);
  39. return {
  40. res: result.res
  41. };
  42. };
  43. // todo delete, 兼容旧版本
  44. function defaultDaysAndDate2Expiration(obj) {
  45. if (obj.days) {
  46. obj.expiration = {
  47. days: obj.days
  48. };
  49. }
  50. if (obj.date) {
  51. obj.expiration = {
  52. createdBeforeDate: obj.date
  53. };
  54. }
  55. }
  56. function checkDaysAndDate(obj, key) {
  57. const { days, createdBeforeDate } = obj;
  58. if (!days && !createdBeforeDate) {
  59. throw new Error(`${key} must includes days or createdBeforeDate`);
  60. } else if (days && (isArray(days) || !/^[1-9][0-9]*$/.test(days))) {
  61. throw new Error('days must be a positive integer');
  62. } else if (createdBeforeDate && !/\d{4}-\d{2}-\d{2}T00:00:00.000Z/.test(createdBeforeDate)) {
  63. throw new Error('createdBeforeDate must be date and conform to iso8601 format');
  64. }
  65. }
  66. function checkNoncurrentDays(obj, key) {
  67. const { noncurrentDays } = obj;
  68. if (!noncurrentDays) {
  69. throw new Error(`${key} must includes noncurrentDays`);
  70. } else if (noncurrentDays && (isArray(noncurrentDays) || !/^[1-9][0-9]*$/.test(noncurrentDays))) {
  71. throw new Error('noncurrentDays must be a positive integer');
  72. }
  73. }
  74. function handleCheckTag(tag) {
  75. if (!isArray(tag) && !isObject(tag)) {
  76. throw new Error('tag must be Object or Array');
  77. }
  78. tag = isObject(tag) ? [tag] : tag;
  79. const tagObj = {};
  80. const tagClone = deepCopy(tag);
  81. tagClone.forEach(v => {
  82. tagObj[v.key] = v.value;
  83. });
  84. checkObjectTag(tagObj);
  85. }
  86. function checkStorageClass(storageClass) {
  87. if (!['IA', 'Archive', 'ColdArchive', 'DeepColdArchive'].includes(storageClass))
  88. throw new Error(`StorageClass must be IA or Archive or ColdArchive or DeepColdArchive`);
  89. }
  90. function checkRule(rule) {
  91. if (rule.id && getStrBytesCount(rule.id) > 255) throw new Error('ID is composed of 255 bytes at most');
  92. if (rule.prefix === undefined) throw new Error('Rule must includes prefix');
  93. if (!['Enabled', 'Disabled'].includes(rule.status)) throw new Error('Status must be Enabled or Disabled');
  94. if (
  95. !rule.expiration &&
  96. !rule.noncurrentVersionExpiration &&
  97. !rule.abortMultipartUpload &&
  98. !rule.transition &&
  99. !rule.noncurrentVersionTransition
  100. ) {
  101. throw new Error(
  102. 'Rule must includes expiration or noncurrentVersionExpiration or abortMultipartUpload or transition or noncurrentVersionTransition'
  103. );
  104. }
  105. if (rule.transition) {
  106. checkStorageClass(rule.transition.storageClass);
  107. checkDaysAndDate(rule.transition, 'Transition');
  108. }
  109. if (rule.expiration) {
  110. if (!rule.expiration.expiredObjectDeleteMarker) {
  111. checkDaysAndDate(rule.expiration, 'Expiration');
  112. } else if (rule.expiration.days || rule.expiration.createdBeforeDate) {
  113. throw new Error('expiredObjectDeleteMarker cannot be used with days or createdBeforeDate');
  114. }
  115. }
  116. if (rule.abortMultipartUpload) {
  117. checkDaysAndDate(rule.abortMultipartUpload, 'AbortMultipartUpload');
  118. }
  119. if (rule.noncurrentVersionTransition) {
  120. checkStorageClass(rule.noncurrentVersionTransition.storageClass);
  121. checkNoncurrentDays(rule.noncurrentVersionTransition, 'NoncurrentVersionTransition');
  122. }
  123. if (rule.noncurrentVersionExpiration) {
  124. checkNoncurrentDays(rule.noncurrentVersionExpiration, 'NoncurrentVersionExpiration');
  125. }
  126. if (rule.tag) {
  127. if (rule.abortMultipartUpload) {
  128. throw new Error('Tag cannot be used with abortMultipartUpload');
  129. }
  130. handleCheckTag(rule.tag);
  131. }
  132. }