Skip to content

Commit

Permalink
Add: Add Lazy convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Oct 2, 2023
1 parent b399e12 commit 587ffd7
Show file tree
Hide file tree
Showing 33 changed files with 217 additions and 73 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ once_cell = "1.18.0"
uuid = { version = "1.4.1", features = ["v4"] }

rstest = "0.18.2"

dotenv = "0.15.0"
6 changes: 3 additions & 3 deletions backend/app/controllers/api/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def data
}
end

def reconvert_chart
def expire_data
count = 0
chart_list = FileResource.where(kind: :chart)
chart_list = FileResource.where(kind: :data)
render json: { code: "ok", data: { count: chart_list.count } }

AllChartConvertJob.perform_later()
chart_list.destroy_all
end

def show_user
Expand Down
6 changes: 4 additions & 2 deletions backend/app/controllers/api/my_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ def delete_alt_user
params.require(:handle)
require_login!

handle = params[:handle][1..].to_i
handle = params[:handle]
user = User.find_by(handle:)
if user.nil? || user.owner_id != session[:user_id]
render json: { code: "not_found" }, status: 404
return
end

session_user = User.find(session[:user_id])

user.charts.update_all(
author_id: session[:user_id],
author_name: nil
author_name: session_user.name
)
user.destroy!
render json: { code: "ok" }
Expand Down
96 changes: 90 additions & 6 deletions backend/app/controllers/sonolus/asset_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,103 @@ def show
name += ".#{params[:format]}" if params[:format].present?
item = Sonolus::AssetController.asset_get(type.delete_suffix("s"), name)
if item.nil?
render json: { error: "Not Found" }, status: 404
render json: { error: "not_found", message: "Not Found" }, status: 404
else
render json: { item:, description: "", recommended: [] }
end
end

def show_static
params.permit(:type, :name, :format)
params.permit(:type, :name)
type = params[:type]
name = params[:name]
name += ".#{params[:format]}" if params[:format].present?

send_file Rails.root.join("assets", type, name)
end

def generate
params.permit(:chart, :type)
chart = Chart.find_by(name: params[:chart])
if chart.nil?
return(
render json: { code: "not_found", message: "Not Found" }, status: 404
)
end
type = params[:type].to_sym
if file = FileResource.find_by(chart_id: chart.id, kind: type)
return redirect_to file.to_frontend
end
generating =
$redis.with { |conn| conn.get("sonolus:generate:#{chart.id}:#{type}") }
should_generate = generating.nil? || generating.to_i < Time.now.to_i - 60
if should_generate
$redis.with do |conn|
conn.set("sonolus:generate:#{chart.id}:#{type}", Time.now.to_i)
end
Rails.logger.info("Generating #{type} for #{chart.name}...")
begin
case type
when :background_v1
ImageConvertJob.perform_now(
chart.name,
chart.resources[:cover],
:background_v1
)
when :background_v3
ImageConvertJob.perform_now(
chart.name,
chart.resources[:cover],
:background_v3
)
when :data
ChartConvertJob.perform_now(chart.resources[:chart])
else
return(
render json: {
code: "not_found",
message: "Not Found"
},
status: 404
)
end
ensure
$redis.with do |conn|
conn.del("sonolus:generate:#{chart.id}:#{type}")
end
end

Rails.logger.info("Generated #{type} for #{chart.name}")

return(
redirect_to FileResource.find_by(
chart_id: chart.id,
kind: type
).to_frontend
)
else
Rails.logger.info("Already generating #{type} for #{chart.name}")
end
FileResource.uncached do
50.times do
break if FileResource.where(chart_id: chart.id, kind: type).exists?

Rails.logger.info("Waiting for generation of #{type}...")

sleep 1
end
if FileResource.where(chart_id: chart.id, kind: type).exists?
Rails.logger.info("Redirecting to #{type} for #{chart.name}")
redirect_to FileResource.find_by(
chart_id: chart.id,
kind: type
).to_frontend
else
Rails.logger.info("Failed to generate #{type} for #{chart.name}")
render json: { code: "not_found", message: "Not Found" }, status: 404
end
end
end

def self.asset_get(type, name)
begin
data =
Expand All @@ -66,13 +148,15 @@ def self.asset_get(type, name)
elsif v.start_with?("!asset:")
v = asset_get(k, v.delete_prefix("!asset:"))
elsif v.start_with?("!file:")
name, srl_type = v.delete_prefix("!file:").split("/")
srl_type ||= name
v = {
hash:
Digest::SHA1.file(
Rails.root.join("assets", "#{type}s", v.delete_prefix("!file:"))
Rails.root.join("assets", "#{type}s", name)
).hexdigest,
url: "/sonolus/assets/#{type}s/#{v.delete_prefix("!file:")}",
type: v.delete_prefix("!file:")
url: "/sonolus/assets/#{type}s/#{name}",
type: srl_type
}
end
[k, v]
Expand Down
5 changes: 4 additions & 1 deletion backend/app/controllers/sonolus/levels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ def test_list
*(self.class.test_search_options.map { |o| o[:query] })
)

alt_users = User.where(owner_id: current_user.id)
charts =
Chart.where(author_id: current_user.id).where.not(visibility: :public)
Chart
.where(author_id: [current_user.id] + alt_users.map(&:id))
.where.not(visibility: :public)

charts = charts.order(updated_at: :desc)
if params[:q_title].present?
Expand Down
12 changes: 0 additions & 12 deletions backend/app/jobs/all_chart_convert_job.rb

This file was deleted.

8 changes: 6 additions & 2 deletions backend/app/jobs/image_convert_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def perform(chart_name, image_file_id, type)
image_file_id
end
)

logger.info "ImageConvertJob: #{image_file.id}, #{type}"
response =
HTTP
Expand All @@ -34,9 +35,12 @@ def perform(chart_name, image_file_id, type)
resource =
FileResource.upload_from_string(chart_name, type, image_data.body.to_s)
HTTP.delete("#{ENV.fetch("SUB_IMAGE_HOST", nil)}/download/#{response[:id]}")
if type == :cover
ImageConvertJob.perform_later(chart_name, resource, :background)
case type
when :cover
ImageConvertJob.perform_later(chart_name, resource, :background_v3)
image_file.delete
when :background_v3
ImageConvertJob.perform_later(chart_name, resource, :background_v1)
end
end
end
32 changes: 22 additions & 10 deletions backend/app/models/chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def resources
cover: base.find(&:cover?),
preview: base.find(&:preview?),
data: base.find(&:data?),
background: base.find(&:background?)
background_v3: base.find(&:background_v3?),
background_v1: base.find(&:background_v1?)
}
end

Expand Down Expand Up @@ -98,15 +99,20 @@ def to_sonolus
cover: resources[:cover]&.to_srl,
bgm: resources[:bgm]&.to_srl,
preview: resources[:preview]&.to_srl,
data: resources[:data]&.to_srl,
data:
resources[:data]&.to_srl ||
{
url: "/sonolus/assets/generate?chart=#{name}&type=data",
type: "LevelData"
},
rating:,
version: 1,
useSkin: {
useDefault: true
},
useBackground: {
useDefault: false,
item: to_sonolus_background(resources)
item: to_sonolus_background(resources, version: 3)
},
useEffect: {
useDefault: true
Expand All @@ -118,32 +124,38 @@ def to_sonolus
}
end

def to_sonolus_background(resources)
def to_sonolus_background(resources, version: 3)
config = {
hash:
Digest::SHA1.file(
Rails.root.join("assets/backgrounds/BackgroundConfiguration")
Rails.root.join("assets/backgrounds/configuration.json.gz")
).hexdigest,
url: "/sonolus/assets/backgrounds/BackgroundConfiguration",
url: "/sonolus/assets/backgrounds/configuration.json.gz",
type: "BackgroundConfiguration"
}
data = {
hash:
Digest::SHA1.file(
Rails.root.join("assets/backgrounds/BackgroundData")
Rails.root.join("assets/backgrounds/v#{version}_data.json.gz")
).hexdigest,
url: "/sonolus/assets/backgrounds/BackgroundData",
url: "/sonolus/assets/backgrounds/v#{version}_data.json.gz",
type: "BackgroundData"
}
{
name: "chcy-bg-#{name}",
name: "chcy-bg-#{name}-v#{version}",
version: 2,
title:,
subtitle: "#{composer}#{artist.presence ? " / #{artist}" : ""}",
author: "#{author_name.presence || author.name}##{author.display_handle}",
thumbnail: resources[:cover]&.to_srl&.merge(type: "BackgroundThumbnail"),
data:,
image: resources[:background]&.to_srl,
image:
resources[:"background_v#{version}"]&.to_srl ||
{
url:
"/sonolus/assets/generate?chart=#{name}&type=background_v#{version}",
type: "BackgroundImage"
},
configuration: config
}
end
Expand Down
8 changes: 5 additions & 3 deletions backend/app/models/file_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ class FileResource < ApplicationRecord
chart: 3,
preview: 4,
base_bgm: 5,
background: 6,
base_cover: 7
background_v1: 6,
base_cover: 7,
background_v3: 8
}

TYPES = {
data: "LevelData",
cover: "LevelCover",
bgm: "LevelBgm",
preview: "LevelPreview",
background: "BackgroundImage"
background_v3: "BackgroundImage",
background_v1: "BackgroundImage"
}.freeze

def self.upload(chart, kind, file)
Expand Down
5 changes: 3 additions & 2 deletions backend/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ class User < ApplicationRecord
foreign_key: :author_id,
dependent: :destroy,
inverse_of: :author
belongs_to :user,
belongs_to :owner,
class_name: "User",
optional: true,
foreign_key: :owner_id,
inverse_of: :alt_users
has_many :alt_users,
foreign_key: :owner_id,
dependent: :destroy,
class_name: "User",
inverse_of: :user
inverse_of: :owner
has_many :likes, dependent: :destroy
enum discord_status: %i[no linked joined]

Expand Down
Binary file removed backend/assets/backgrounds/BackgroundData
Binary file not shown.
Binary file removed backend/assets/backgrounds/BackgroundImage
Binary file not shown.
Binary file removed backend/assets/backgrounds/BackgroundThumbnail
Binary file not shown.
9 changes: 9 additions & 0 deletions backend/assets/backgrounds/pjsekai-old.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: pjsekai-old
version: 2
title: Live v1
subtitle: プロジェクトセカイ カラフルステージ!
author: Burrito
thumbnail: "!file:v1_thumbnail.png/BackgroundThumbnail"
data: "!file:v1_data.json.gz/BackgroundData"
image: "!file:v1.png/BackgroundImage"
configuration: "!file:configuration.json.gz/BackgroundConfiguration"
10 changes: 5 additions & 5 deletions backend/assets/backgrounds/pjsekai.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: pjsekai
version: 2
title: Live
title: Live v3
subtitle: プロジェクトセカイ カラフルステージ!
author: Burrito
thumbnail: "!file:BackgroundThumbnail"
data: "!file:BackgroundData"
image: "!file:BackgroundImage"
configuration: "!file:BackgroundConfiguration"
thumbnail: "!file:v3_thumbnail.png/BackgroundThumbnail"
data: "!file:v3_data.json.gz/BackgroundData"
image: "!file:v3.png/BackgroundImage"
configuration: "!file:configuration.json.gz/BackgroundConfiguration"
Binary file added backend/assets/backgrounds/v1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/assets/backgrounds/v1_data.json.gz
Binary file not shown.
Binary file added backend/assets/backgrounds/v1_thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/assets/backgrounds/v3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/assets/backgrounds/v3_data.json.gz
Binary file not shown.
Binary file added backend/assets/backgrounds/v3_thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 587ffd7

Please sign in to comment.