Skip to content
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

Static icon image #10

Merged
merged 9 commits into from Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 38 additions & 13 deletions webapp/ruby/app.rb
Expand Up @@ -200,13 +200,19 @@ def fill_reaction_response(tx, reaction_model)
def fill_user_response(tx, user_model)
theme_model = tx.xquery('SELECT * FROM themes WHERE user_id = ?', user_model.fetch(:id)).first

icon_model = tx.xquery('SELECT image FROM icons WHERE user_id = ?', user_model.fetch(:id)).first
# icon_model = tx.xquery('SELECT image FROM icons WHERE user_id = ?', user_model.fetch(:id)).first
icon_path = "../img/#{user_model.fetch(:id)}.jpg"
image =
if icon_model
icon_model.fetch(:image)
if File.exist?(icon_path)
File.binread(icon_path)
else
FALLBACK_IMAGE_BIN
end
# if icon_model
# icon_model.fetch(:image)
# else
# File.binread(FALLBACK_IMAGE)
# end
icon_hash = Digest::SHA256.hexdigest(image)

{
Expand All @@ -227,10 +233,17 @@ def batch_fill_user_response(tx, user_models)

theme_models = tx.xquery('SELECT * FROM themes WHERE user_id IN (?)', user_models.map { _1[:id] }.uniq).group_by { _1[:user_id] }.transform_values(&:first)

icon_models = tx.xquery('SELECT * FROM icons WHERE user_id IN (?)', user_models.map { _1[:id] }.uniq).group_by { _1[:user_id] }.transform_values(&:first)
# icon_models = tx.xquery('SELECT * FROM icons WHERE user_id IN (?)', user_models.map { _1[:id] }.uniq).group_by { _1[:user_id] }.transform_values(&:first)
icon_hashes = user_models.map { _1[:id] }.uniq.map do |user_id|
image = if icon_models[user_id]
icon_models[user_id].fetch(:image)
# image = if icon_models[user_id]
# icon_models[user_id].fetch(:image)
# else
# FALLBACK_IMAGE_BIN
# end
icon_path = "../img/#{user_id}.jpg"
image = if File.exist?(icon_path)
# icon_models[user_id].fetch(:image)
File.binread(icon_path)
else
FALLBACK_IMAGE_BIN
end
Expand Down Expand Up @@ -807,12 +820,20 @@ def batch_fill_user_response(tx, user_models)
unless user
raise HttpError.new(404, 'not found user that has the given username')
end
tx.xquery('SELECT image FROM icons WHERE user_id = ?', user.fetch(:id)).first
# tx.xquery('SELECT image FROM icons WHERE user_id = ?', user.fetch(:id)).first
icon_path = "../img/#{user.fetch(:id)}.jpg"
image =
if File.exist?(icon_path)
icon_path
else
nil
end
end

content_type 'image/jpeg'
if image
image[:image]
# image[:image]
send_file image
else
send_file FALLBACK_IMAGE
end
Expand All @@ -834,13 +855,17 @@ def batch_fill_user_response(tx, user_models)

req = decode_request_body(PostIconRequest)
image = Base64.decode64(req.image)

icon_id = db_transaction do |tx|
tx.xquery('DELETE FROM icons WHERE user_id = ?', user_id)
tx.xquery('INSERT INTO icons (user_id, image) VALUES (?, ?)', user_id, image)
tx.last_id
File.open("../img/#{user_id}.jpg", mode = "w") do |f|
f.write(image)
end

# icon_id = db_transaction do |tx|
# tx.xquery('DELETE FROM icons WHERE user_id = ?', user_id)
# tx.xquery('INSERT INTO icons (user_id, image) VALUES (?, ?)', user_id, image)
# tx.last_id
# end
icon_id = rand(10000000);

status 201
json(
id: icon_id,
Expand Down
2 changes: 1 addition & 1 deletion webapp/sql/init.sh
Expand Up @@ -70,4 +70,4 @@ mysql -u"$ISUCON_DB_USER" \

bash ../pdns/init_zone.sh


find ../img/ | grep -v -e "../img/$" -e "NoImage" | xargs rm -f