Skip to content

Commit

Permalink
fix to upload icons to servers
Browse files Browse the repository at this point in the history
  • Loading branch information
tagomoris committed Oct 22, 2017
1 parent bc7813a commit b380245
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions ruby/app.rb
@@ -1,4 +1,5 @@
require 'digest/sha1' require 'digest/sha1'
require 'net/http'
require 'mysql2' require 'mysql2'
require 'mysql2-cs-bind' require 'mysql2-cs-bind'
require 'connection_pool' require 'connection_pool'
Expand All @@ -12,6 +13,7 @@
Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379")) Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379"))
end end


WEB_SERVERS = ENV.fetch("SERVERS", "localhost:5000").split(',')


Mysql2::Client.new( Mysql2::Client.new(
host: ENV.fetch('ISUBATA_DB_HOST') { 'localhost' }, host: ENV.fetch('ISUBATA_DB_HOST') { 'localhost' },
Expand All @@ -34,12 +36,16 @@
end end


class App < Sinatra::Base class App < Sinatra::Base
PUBLIC_FOLDER = File.expand_path('../../public', __FILE__)
IMAGES_FOLDER = File.join(File.expand_path('../../public', __FILE__), "images")

configure do configure do
set :session_secret, 'tonymoris' set :session_secret, 'tonymoris'
set :public_folder, File.expand_path('../../public', __FILE__) set :public_folder, PUBLIC_FOLDER
set :avatar_max_size, 1 * 1024 * 1024 set :avatar_max_size, 1 * 1024 * 1024


enable :sessions enable :sessions
# enable :logging
end end


configure :development do configure :development do
Expand Down Expand Up @@ -264,7 +270,7 @@ def user
@self_profile = user['id'] == @user['id'] @self_profile = user['id'] == @user['id']
erb :profile erb :profile
end end

get '/add_channel' do get '/add_channel' do
if user.nil? if user.nil?
return redirect '/login', 303 return redirect '/login', 303
Expand Down Expand Up @@ -292,6 +298,15 @@ def user
redirect "/channel/#{channel_id}", 303 redirect "/channel/#{channel_id}", 303
end end


def upload_icon(server, path, data)
host, port = server.split(':')
port = (port || 80).to_i
p(host: host, port: port, path: path, data_size: data.size)
Net::HTTP.start(host, port) do |http|
http.put(path, data, {'Content-Type' => 'application/octet-stream'})
end
end

post '/profile' do post '/profile' do
if user.nil? if user.nil?
return redirect '/login', 303 return redirect '/login', 303
Expand Down Expand Up @@ -326,8 +341,11 @@ def user
end end
end end


if !avatar_name.nil? && !avatar_data.nil? if avatar_name && avatar_data
db.xquery('INSERT INTO image (name, data) VALUES (?, ?)', avatar_name, avatar_data) path = "/icons/#{avatar_name}"
WEB_SERVERS.each do |server|
upload_icon(server, path, avatar_data)
end
db.xquery('UPDATE user SET avatar_icon = ? WHERE id = ?', avatar_name, user['id']) db.xquery('UPDATE user SET avatar_icon = ? WHERE id = ?', avatar_name, user['id'])
end end


Expand All @@ -338,14 +356,20 @@ def user
redirect '/', 303 redirect '/', 303
end end


put '/icons/:file_name' do
file_name = params[:file_name]
content_body = request.body.read
File.open(File.join(IMAGES_FOLDER, file_name), 'w') do |f|
f.write content_body
end
200
end

get '/icons/:file_name' do get '/icons/:file_name' do
file_name = params[:file_name] file_name = params[:file_name]
row = db.xquery('SELECT * FROM image WHERE name = ?', file_name).first path = File.join(IMAGES_FOLDER, file_name)
ext = file_name.include?('.') ? File.extname(file_name) : '' if File.exist?(path)
mime = ext2mime(ext) return File.open(path){|f| f.read }
if !row.nil? && !mime.empty?
content_type mime
return row['data']
end end
404 404
end end
Expand Down

0 comments on commit b380245

Please sign in to comment.