Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
nginxは諦めてsinatraで返す
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Nov 25, 2023
1 parent 212ea6c commit 1a2f8d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
34 changes: 17 additions & 17 deletions infra/nginx/sites-enabled/isupipe.conf
Expand Up @@ -42,24 +42,24 @@ server {
try_files $uri /index.html;
}

location ~ /api/user/(?<username>[^/]+)/icon {
alias /home/isucon/webapp/img/icons/;
default_type image/jpeg;
# location ~ /api/user/(?<username>[^/]+)/icon {
# alias /home/isucon/webapp/img/icons/;
# default_type image/jpeg;
#
# error_page 404 /home/isucon/webapp/img/NoImage.jpg;
#
#
# if (!-f /home/isucon/webapp/img/icons/$username.jpg) {
# return 404;
# }
#
# try_files /home/isucon/webapp/img/icons/$username.jpg =404;
# }

error_page 404 /home/isucon/webapp/img/NoImage.jpg;


if (!-f /home/isucon/webapp/img/icons/$username.jpg) {
return 404;
}

try_files /home/isucon/webapp/img/icons/$username.jpg =404;
}

location = /home/isucon/webapp/img/NoImage.jpg {
internal;
root /; # 画像のパスの基準となるディレクトリを指定
}
# location = /home/isucon/webapp/img/NoImage.jpg {
# internal;
# root /; # 画像のパスの基準となるディレクトリを指定
# }

location /api {
proxy_set_header Host $host;
Expand Down
44 changes: 25 additions & 19 deletions ruby/app.rb
Expand Up @@ -759,24 +759,31 @@ def fill_user_response(tx, user_model)
BCRYPT_DEFAULT_COST = 4
FALLBACK_IMAGE = '../img/NoImage.jpg'

# get '/api/user/:username/icon' do
# username = params[:username]
#
# image = db_transaction do |tx|
# user = tx.xquery('SELECT * FROM users WHERE name = ?', username).first
# 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
# end
#
# content_type 'image/jpeg'
# if image
# image[:image]
# else
# send_file FALLBACK_IMAGE
# end
# end
get '/api/user/:username/icon' do
username = params[:username]

# image = db_transaction do |tx|
# user = tx.xquery('SELECT * FROM users WHERE name = ?', username).first
# 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
# end
#
# content_type 'image/jpeg'
# if image
# image[:image]
# else
# send_file FALLBACK_IMAGE
# end

content_type 'image/jpeg'
if File.exist?("#{ICONS_DIR}/#{username}.jpg")
send_file "#{ICONS_DIR}/#{username}.jpg"
else
send_file FALLBACK_IMAGE
end
end

PostIconRequest = Data.define(:image)

Expand All @@ -802,7 +809,6 @@ def fill_user_response(tx, user_model)

# iconsにも保存する
user = tx.xquery("SELECT name FROM users WHERE id = ?", user_id).first
raise "WIP: #{ICONS_DIR}/#{user[:name]}.jpg"

File.open("#{ICONS_DIR}/#{user[:name]}.jpg", 'wb') do |f|
f.write(image)
Expand Down

0 comments on commit 1a2f8d8

Please sign in to comment.