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

Commit

Permalink
Merge pull request #7 from sue445/static_images
Browse files Browse the repository at this point in the history
GET /api/user/:username/icon : 画像をnginxで返す
  • Loading branch information
sue445 committed Nov 25, 2023
2 parents 3a75826 + 1a2f8d8 commit 4d25cf8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
/public/
/img/icons/
10 changes: 5 additions & 5 deletions Rakefile
Expand Up @@ -108,12 +108,12 @@ namespace :deploy do
# nginx
case name
when :host01
# exec ip_address, "sudo cp infra/nginx/nginx.conf /etc/nginx/nginx.conf"
# exec ip_address, "sudo nginx -t"
# exec ip_address, "sudo chmod 644 /var/log/nginx/*.log"
# exec_service ip_address, service: "nginx", enabled: true
exec ip_address, "sudo cp infra/nginx/nginx.conf /etc/nginx/nginx.conf"
exec ip_address, "sudo nginx -t"
exec ip_address, "sudo chmod 644 /var/log/nginx/*.log"
exec_service ip_address, service: "nginx", enabled: true
else
# exec_service ip_address, service: "nginx", enabled: false
exec_service ip_address, service: "nginx", enabled: false
end

# app
Expand Down
Empty file added img/icons/.keep
Empty file.
Binary file removed img/icons_dump/test001.jpg
Binary file not shown.
4 changes: 3 additions & 1 deletion infra/nginx/nginx.conf
Expand Up @@ -57,7 +57,9 @@ http {
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

# include /etc/nginx/sites-enabled/*;
include /home/isucon/webapp/infra/nginx/sites-enabled/*.conf;
}


Expand Down
22 changes: 21 additions & 1 deletion infra/nginx/sites-enabled/isupipe.conf
Expand Up @@ -41,8 +41,28 @@ server {
location / {
try_files $uri /index.html;
}

# 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;
# }

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

location /api {
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
}
}
}
44 changes: 34 additions & 10 deletions ruby/app.rb
Expand Up @@ -48,6 +48,10 @@ class App < Sinatra::Base
DEFAULT_USER_ID_KEY = 'USERID'
DEFAULT_USERNAME_KEY = 'USERNAME'

ICONS_DIR = "/home/isucon/webapp/img/icons"

ICONS_DUMP_DIR = "/home/isucon/webapp/img/icons_dump"

class HttpError < StandardError
attr_reader :code

Expand Down Expand Up @@ -219,6 +223,10 @@ def fill_user_response(tx, user_model)
halt 500
end

# iconsを全部消してicon_dumpからコピーして初期化する
system("rm -rf #{ICONS_DIR}/*", exception: true)
system("cp #{ICONS_DUMP_DIR}/* #{ICONS_DIR}/", exception: true)

json(
language: 'ruby',
)
Expand Down Expand Up @@ -754,17 +762,24 @@ def fill_user_response(tx, user_model)
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
# 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 image
image[:image]
if File.exist?("#{ICONS_DIR}/#{username}.jpg")
send_file "#{ICONS_DIR}/#{username}.jpg"
else
send_file FALLBACK_IMAGE
end
Expand All @@ -790,7 +805,16 @@ def fill_user_response(tx, user_model)
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
id = tx.last_id

# iconsにも保存する
user = tx.xquery("SELECT name FROM users WHERE id = ?", user_id).first

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

id
end

status 201
Expand Down

0 comments on commit 4d25cf8

Please sign in to comment.