Class: S3CorsRulesets
- Inherits:
-
Object
- Object
- S3CorsRulesets
- Defined in:
- lib/s3_cors_rulesets.rb
Constant Summary collapse
- ASSETS =
{ allowed_headers: ["Authorization"], allowed_methods: %w[GET HEAD], allowed_origins: ["*"], max_age_seconds: 3000, }.freeze
- BACKUP_DIRECT_UPLOAD =
{ allowed_headers: ["*"], expose_headers: ["ETag"], allowed_methods: %w[GET HEAD PUT], allowed_origins: ["*"], max_age_seconds: 3000, }.freeze
- DIRECT_UPLOAD =
{ allowed_headers: %w[ Authorization Content-Disposition Content-Type X-Amz-Acl X-Amz-Meta-Sha1-Checksum ], expose_headers: ["ETag"], allowed_methods: %w[GET HEAD PUT], allowed_origins: ["*"], max_age_seconds: 3000, }.freeze
- RULE_STATUS_SKIPPED =
"rules_skipped_from_settings"
- RULE_STATUS_EXISTED =
"rules_already_existed"
- RULE_STATUS_APPLIED =
"rules_applied"
Class Method Summary collapse
-
.sync(use_db_s3_config:, s3_client: nil) ⇒ Object
Used by the s3:ensure_cors_rules rake task to make sure the relevant CORS rules are applied to allow for direct uploads to S3, and in the case of assets rules so there are fonts and other public assets for the site loaded correctly.
Class Method Details
.sync(use_db_s3_config:, s3_client: nil) ⇒ Object
Used by the s3:ensure_cors_rules rake task to make sure the relevant CORS rules are applied to allow for direct uploads to S3, and in the case of assets rules so there are fonts and other public assets for the site loaded correctly.
The use_db_s3_config param comes from ENV, and if the S3 client is not provided it is initialized by the S3Helper.
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 |
# File 'lib/s3_cors_rulesets.rb', line 47 def self.sync(use_db_s3_config:, s3_client: nil) return if !SiteSetting.s3_install_cors_rule return if !(GlobalSetting.use_s3? || SiteSetting.enable_s3_uploads) assets_rules_status = RULE_STATUS_SKIPPED backup_rules_status = RULE_STATUS_SKIPPED direct_upload_rules_status = RULE_STATUS_SKIPPED s3_helper = S3Helper.build_from_config(s3_client: s3_client, use_db_s3_config: use_db_s3_config) if !Rails.env.test? puts "Attempting to apply ASSETS S3 CORS ruleset in bucket #{s3_helper.s3_bucket_name}." end assets_rules_status = s3_helper.ensure_cors!([S3CorsRulesets::ASSETS]) ? RULE_STATUS_APPLIED : RULE_STATUS_EXISTED if SiteSetting.enable_backups? && SiteSetting.backup_location == BackupLocationSiteSetting::S3 backup_s3_helper = S3Helper.build_from_config( s3_client: s3_client, use_db_s3_config: use_db_s3_config, for_backup: true, ) if !Rails.env.test? puts "Attempting to apply BACKUP_DIRECT_UPLOAD S3 CORS ruleset in bucket #{backup_s3_helper.s3_bucket_name}." end backup_rules_status = ( if backup_s3_helper.ensure_cors!([S3CorsRulesets::BACKUP_DIRECT_UPLOAD]) RULE_STATUS_APPLIED else RULE_STATUS_EXISTED end ) end if SiteSetting.enable_direct_s3_uploads if !Rails.env.test? puts "Attempting to apply DIRECT_UPLOAD S3 CORS ruleset in bucket #{s3_helper.s3_bucket_name}." end direct_upload_rules_status = ( if s3_helper.ensure_cors!([S3CorsRulesets::DIRECT_UPLOAD]) RULE_STATUS_APPLIED else RULE_STATUS_EXISTED end ) end { assets_rules_status: assets_rules_status, backup_rules_status: backup_rules_status, direct_upload_rules_status: direct_upload_rules_status, } end |