-
Notifications
You must be signed in to change notification settings - Fork 5
Save scratch assets to bucket #827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ade04f7
ec2be54
ea17a52
8d8418e
3f53e45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,51 +2,103 @@ | |
|
|
||
| require 'ruby-progressbar' | ||
| require 'stringio' | ||
| require 'aws-sdk-s3' | ||
|
|
||
| class ScratchAssetImporter | ||
| def self.import(...) | ||
| new(...).import | ||
| class << self | ||
| def import_all(asset_names, asset_base_url) | ||
| bar = ProgressBar.create(format: '%t: |%B| %c of %C %E', total: asset_names.count) if show_progress? | ||
|
|
||
| asset_names.each do |asset_name| | ||
| bar.increment if show_progress? | ||
| new(asset_name, asset_base_url).import | ||
| end | ||
| end | ||
|
zetter-rpf marked this conversation as resolved.
|
||
|
|
||
| private | ||
|
|
||
| def show_progress? | ||
| !Rails.env.test? | ||
| end | ||
| end | ||
|
|
||
| attr_reader :asset_base_url, :asset_names | ||
| attr_reader :asset_base_url, :asset_name | ||
|
|
||
| ASSET_FETCHING_DELAY = 0.2 | ||
|
|
||
| def initialize(asset_names, asset_base_url) | ||
| @asset_names = asset_names | ||
| def initialize(asset_name, asset_base_url) | ||
| @asset_name = asset_name | ||
| @asset_base_url = asset_base_url | ||
| end | ||
|
|
||
| def import | ||
| bar = ProgressBar.create(format: '%t: |%B| %c of %C %E', total: asset_names.count) if show_progress? | ||
| create_scratch_asset | ||
| save_to_editor_asset_bucket | ||
| rescue StandardError => e | ||
| Rails.logger.error("Failed to import asset #{asset_name}: #{e.message}") | ||
| end | ||
|
|
||
| asset_names.each do |asset_name| | ||
| bar.increment if show_progress? | ||
| import_asset(asset_name) | ||
| def asset | ||
| @asset ||= begin | ||
| sleep(ASSET_FETCHING_DELAY) | ||
| connection.get("#{asset_name}/get/") | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def import_asset(asset_name) | ||
| def create_scratch_asset | ||
| return if ScratchAsset.global_assets.exists?(filename: asset_name) | ||
|
|
||
| sleep(ASSET_FETCHING_DELAY) | ||
| asset = connection.get("#{asset_name}/get/") | ||
| io = StringIO.new(asset.body) | ||
|
|
||
| ScratchAsset.create!(filename: asset_name, project_id: nil, uploaded_user_id: nil) | ||
| .file | ||
| .attach(io: StringIO.new(asset.body), filename: asset_name) | ||
| rescue StandardError => e | ||
| Rails.logger.error("Failed to import asset #{asset_name}: #{e.message}") | ||
| .attach(io:, filename: asset_name) | ||
| end | ||
|
|
||
| def save_to_editor_asset_bucket | ||
| return unless save_to_editor_asset_bucket? | ||
|
|
||
| body = StringIO.new(asset.body) | ||
|
|
||
| s3_client.put_object( | ||
| bucket: ENV.fetch('EDITOR_ASSETS_BUCKET'), | ||
| key: asset_key, | ||
| body:, | ||
| content_type: asset_content_type, | ||
| cache_control: 'public, max-age=604800' | ||
| ) | ||
| end | ||
|
|
||
| def save_to_editor_asset_bucket? | ||
| return false unless ENV['EDITOR_ASSETS_BUCKET'] | ||
|
|
||
| s3_client.head_object(bucket: ENV.fetch('EDITOR_ASSETS_BUCKET'), key: asset_key) | ||
| false | ||
| rescue Aws::S3::Errors::NotFound | ||
| true | ||
| end | ||
|
zetter-rpf marked this conversation as resolved.
|
||
|
|
||
| def asset_key | ||
| "internalapi/asset/#{asset_name}/get/" | ||
| end | ||
|
zetter-rpf marked this conversation as resolved.
|
||
|
|
||
| def asset_content_type | ||
| extension = File.extname(asset_name).delete('.') | ||
| Mime::Type.lookup_by_extension(extension).to_s | ||
| end | ||
|
Comment on lines
+85
to
+91
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might apply
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I might change it to raise or log instead - if a mime type isn't set we should know about it as it means it might not be served correctly
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a new commit that raises if the mime type is unexpected |
||
|
|
||
| def s3_client | ||
| @s3_client ||= Aws::S3::Client.new( | ||
| access_key_id: ENV.fetch('EDITOR_ASSETS_ACCESS_KEY_ID'), | ||
| secret_access_key: ENV.fetch('EDITOR_ASSETS_SECRET_ACCESS_KEY'), | ||
| endpoint: ENV.fetch('EDITOR_ASSETS_ENDPOINT'), | ||
| region: 'auto' | ||
| ) | ||
| end | ||
|
|
||
| def connection | ||
| @connection ||= Faraday.new(url: asset_base_url) do |faraday| | ||
| faraday.response :raise_error | ||
| end | ||
| end | ||
|
|
||
| def show_progress? | ||
| !Rails.env.test? | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.