2020-09-14 07:32:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UploadsHelpers
|
|
|
|
def setup_s3
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
|
|
|
|
SiteSetting.s3_region = "us-west-1"
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket"
|
|
|
|
|
|
|
|
SiteSetting.s3_access_key_id = "some key"
|
|
|
|
SiteSetting.s3_secret_access_key = "some secrets3_region key"
|
|
|
|
|
2024-11-06 20:06:39 -05:00
|
|
|
dualstack = SiteSetting.Upload.use_dualstack_endpoint ? ".dualstack" : ""
|
|
|
|
|
2020-09-14 07:32:25 -04:00
|
|
|
stub_request(
|
|
|
|
:head,
|
2024-11-06 20:06:39 -05:00
|
|
|
"https://#{SiteSetting.s3_upload_bucket}.s3#{dualstack}.#{SiteSetting.s3_region}.amazonaws.com/",
|
2020-09-14 07:32:25 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
def enable_secure_uploads
|
2020-11-05 19:33:19 -05:00
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-11-05 19:33:19 -05:00
|
|
|
end
|
|
|
|
|
2020-09-14 07:32:25 -04:00
|
|
|
def stub_upload(upload)
|
2024-11-06 20:06:39 -05:00
|
|
|
dualstack = SiteSetting.Upload.use_dualstack_endpoint ? ".dualstack" : ""
|
|
|
|
|
2021-12-03 17:05:27 -05:00
|
|
|
url =
|
2024-11-06 20:06:39 -05:00
|
|
|
%r{https://#{SiteSetting.s3_upload_bucket}.s3#{dualstack}.#{SiteSetting.s3_region}.amazonaws.com/original/\d+X.*#{upload.sha1}.#{upload.extension}\?acl}
|
2020-09-14 07:32:25 -04:00
|
|
|
stub_request(:put, url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_s3_store
|
|
|
|
store = FileStore::S3Store.new
|
|
|
|
client = Aws::S3::Client.new(stub_responses: true)
|
|
|
|
store.s3_helper.stubs(:s3_client).returns(client)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
|
|
|
end
|
|
|
|
end
|