-
Notifications
You must be signed in to change notification settings - Fork 5
Scratch asset importing #755
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d07ae9
Task to import Scratch assets
zetter-rpf ea6a95e
Add a delay between fetching assets
zetter-rpf 607c38c
Make the rails log level configurable in development
zetter-rpf 39dd62c
Display a progress bar
zetter-rpf 23387d7
Make sure Scratch assets can be displayed in library
zetter-rpf 1328bd0
Explicitly require stringio
zetter-rpf 9e4139e
Remove unused import
zetter-rpf a8df6e2
Raise early if unexpected HTTP response
zetter-rpf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'ruby-progressbar' | ||
| require 'stringio' | ||
|
|
||
| class ScratchAssetImporter | ||
| def self.import(...) | ||
| new(...).import | ||
| end | ||
|
|
||
| attr_reader :asset_base_url, :asset_names | ||
|
|
||
| ASSET_FETCHING_DELAY = 0.2 | ||
|
|
||
| def initialize(asset_names, asset_base_url) | ||
| @asset_names = asset_names | ||
| @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? | ||
|
|
||
| asset_names.each do |asset_name| | ||
| bar.increment if show_progress? | ||
| import_asset(asset_name) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def import_asset(asset_name) | ||
| return if ScratchAsset.exists?(filename: asset_name) | ||
|
|
||
| sleep(ASSET_FETCHING_DELAY) | ||
zetter-rpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| asset = connection.get("#{asset_name}/get/") | ||
| ScratchAsset.create!(filename: asset_name).file.attach(io: StringIO.new(asset.body), filename: asset_name) | ||
zetter-rpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rescue StandardError => e | ||
| Rails.logger.error("Failed to import asset #{asset_name}: #{e.message}") | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ScratchConfigImporter | ||
| def self.import(...) | ||
| new(...).import | ||
| end | ||
|
|
||
| attr_reader :asset_base_url, :asset_config_url | ||
|
|
||
| def initialize(asset_config_url, asset_base_url) | ||
| @asset_config_url = asset_config_url | ||
| @asset_base_url = asset_base_url | ||
| end | ||
|
|
||
| def import | ||
| config = connection.get.body | ||
| asset_config = JSON.parse(config, symbolize_names: true) | ||
| asset_names = extract_asset_names(asset_config) | ||
| ScratchAssetImporter.import(asset_names, asset_base_url) | ||
| end | ||
zetter-rpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def connection | ||
| Faraday.new(url: asset_config_url) do |faraday| | ||
| faraday.response :raise_error | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def extract_asset_names(config) | ||
| names = [] | ||
| config.each do |item| | ||
| names << item[:md5ext] if item[:md5ext] | ||
| names.concat(extract_asset_names(item.fetch(:costumes, []))) | ||
| names.concat(extract_asset_names(item.fetch(:sounds, []))) | ||
| end | ||
| names | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'seeds_helper' | ||
|
|
||
| namespace :scratch_assets do | ||
| desc 'Import scratch assets' | ||
| task import_all: %i[import_backdrops import_costumes import_sounds import_sprites] | ||
|
|
||
| task import_backdrops: :environment do | ||
| Rails.logger.info 'Importing backdrops...' | ||
| config_url = "#{config_base_url}backdrops.json" | ||
| ScratchConfigImporter.import(config_url, import_base_url) | ||
| end | ||
|
|
||
| task import_costumes: :environment do | ||
| Rails.logger.info 'Importing costumes...' | ||
| config_url = "#{config_base_url}costumes.json" | ||
| ScratchConfigImporter.import(config_url, import_base_url) | ||
| end | ||
zetter-rpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| task import_sounds: :environment do | ||
| Rails.logger.info 'Importing sounds...' | ||
| config_url = "#{config_base_url}sounds.json" | ||
| ScratchConfigImporter.import(config_url, import_base_url) | ||
| end | ||
|
|
||
| task import_sprites: :environment do | ||
| Rails.logger.info 'Importing sprites...' | ||
| config_url = "#{config_base_url}sprites.json" | ||
| ScratchConfigImporter.import(config_url, import_base_url) | ||
| end | ||
|
|
||
| def config_base_url | ||
| ENV.fetch('SCRATCH_ASSET_CONFIG_BASE_URL') | ||
| end | ||
|
|
||
| def import_base_url | ||
| ENV.fetch('SCRATCH_ASSET_IMPORT_BASE_URL') | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
| require 'scratch_asset_importer' | ||
zetter-rpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| RSpec.describe ScratchAssetImporter do | ||
| describe '.import' do | ||
| it 'imports assets from the config' do | ||
| image = Rails.root.join('spec/fixtures/files/test_image_1.png').read | ||
| stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image) | ||
|
|
||
| described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/') | ||
|
|
||
| scratch_asset = ScratchAsset.find_by(filename: '123abc.png') | ||
| expect(scratch_asset).to be_present | ||
| expect(scratch_asset.file.download).to eq(image) | ||
| end | ||
|
|
||
| it 'does nothing if asset already exists' do | ||
| create(:scratch_asset, :with_file, filename: '123abc.png') | ||
|
|
||
| expect do | ||
| described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/') | ||
| end.not_to change(ScratchAsset, :count) | ||
| end | ||
|
|
||
| it 'can import multiple assets' do | ||
| image = Rails.root.join('spec/fixtures/files/test_image_1.png').read | ||
|
|
||
| stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image) | ||
| stub_request(:get, 'https://example.net/internalapi/asset/456xyz.png/get/').to_return(status: 200, body: image) | ||
|
|
||
| described_class.import(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/') | ||
| expect(ScratchAsset.find_by(filename: '123abc.png')).to be_present | ||
| expect(ScratchAsset.find_by(filename: '456xyz.png')).to be_present | ||
| end | ||
|
|
||
| it 'skips assets that fail to import' do | ||
| image = Rails.root.join('spec/fixtures/files/test_image_1.png').read | ||
|
|
||
| stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 500, body: 'error') | ||
| stub_request(:get, 'https://example.net/internalapi/asset/456xyz.png/get/').to_return(status: 200, body: image) | ||
|
|
||
| described_class.import(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/') | ||
| expect(ScratchAsset.find_by(filename: '123abc.png')).not_to be_present | ||
| expect(ScratchAsset.find_by(filename: '456xyz.png')).to be_present | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
| require 'scratch_asset_importer' | ||
zetter-rpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| RSpec.describe ScratchConfigImporter do | ||
| before do | ||
| allow(ScratchAssetImporter).to receive(:import) | ||
| end | ||
|
|
||
| describe '.import' do | ||
| it 'imports assets from the config' do | ||
| config = [{ md5ext: '123abc.png' }].to_json | ||
| stub_request(:get, 'https://example.com/config/backdrops.json').to_return(status: 200, body: config) | ||
|
|
||
| described_class.import('https://example.com/config/backdrops.json', 'https://example.net/internalapi/asset/') | ||
|
|
||
| expect(ScratchAssetImporter).to have_received(:import).with(['123abc.png'], 'https://example.net/internalapi/asset/') | ||
| end | ||
|
|
||
| it 'handles assets nested under sounds and costumes' do | ||
| config = [ | ||
| { | ||
| costumes: [{ | ||
| md5ext: '123abc.png' | ||
| }], | ||
| sounds: [{ | ||
| md5ext: '456xyz.png' | ||
| }] | ||
| } | ||
| ].to_json | ||
| stub_request(:get, 'https://example.com/config/sprites.json').to_return(status: 200, body: config) | ||
|
|
||
| described_class.import('https://example.com/config/sprites.json', 'https://example.net/internalapi/asset/') | ||
|
|
||
| expect(ScratchAssetImporter).to have_received(:import).with(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/') | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.