头像上传bug修复

This commit is contained in:
guange 2015-05-24 13:52:27 +08:00
parent 0769da1426
commit c9b46e4e3a
3 changed files with 61 additions and 37 deletions

View File

@ -24,48 +24,58 @@ class AvatarController < ApplicationController
else else
@image_file=params[:filename] @image_file=params[:filename]
end end
@temp_file = StringIO.new(@temp_file)
end end
end end
end end
if @temp_file && (@temp_file.size > 0) if @temp_file && (@temp_file.size > 0)
diskfile=disk_filename(@source_type,@source_id) if @temp_file.size > Setting.upload_avatar_max_size.to_i
@urlfile='/' << File.join("images","avatars",avatar_directory(@source_type),avatar_filename(@source_id,@image_file)) @status = 1
@msg = l(:error_upload_avatar_to_large, :max_size => number_to_human_size(Setting.upload_avatar_max_size.to_i))
elsif Trustie::Utils::Image.new(@temp_file).image?
diskfile=disk_filename(@source_type,@source_id)
@urlfile='/' << File.join("images","avatars",avatar_directory(@source_type),avatar_filename(@source_id,@image_file))
# 用户头像上传时进行特别处理 # 用户头像上传时进行特别处理
if @source_type == 'User' if @source_type == 'User'
diskfile += "temp" diskfile += "temp"
@urlfile += "temp" @urlfile += "temp"
end
logger.info("Saving avatar '#{diskfile}' (#{@temp_file.size} bytes)")
path = File.dirname(diskfile)
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
md5 = Digest::MD5.new
File.open(diskfile, "wb") do |f|
if @temp_file.respond_to?(:read)
buffer = ""
while (buffer = @temp_file.read(8192))
f.write(buffer)
md5.update(buffer)
end
else
f.write(@temp_file)
md5.update(@temp_file)
end end
logger.info("Saving avatar '#{diskfile}' (#{@temp_file.size} bytes)")
path = File.dirname(diskfile)
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
md5 = Digest::MD5.new
File.open(diskfile, "wb") do |f|
if @temp_file.respond_to?(:read)
@temp_file.rewind
buffer = ""
while (buffer = @temp_file.read(8192))
f.write(buffer)
md5.update(buffer)
end
else
f.write(@temp_file)
md5.update(@temp_file)
end
end
Trustie::Utils::Image.new(diskfile,true).compress(300)
@status = 0
@msg = ''
else
@status = 2
@msg = l(:not_valid_image_file)
end end
# self.digest = md5.hexdigest
end end
@temp_file = nil @temp_file = nil
image = Trustie::Utils::Image.new(diskfile,true)
image.compress(300)
respond_to do |format| respond_to do |format|
format.json{ format.json{
render :inline => "#{@urlfile.to_s}?#{Time.now.to_i}",:content_type => 'text/html' render :inline => {status: @status, message:@msg, url:"#{@urlfile.to_s}?#{Time.now.to_i}"}.to_json,:content_type => 'text/html'
return return
} }
format.js format.js

View File

@ -17,21 +17,20 @@ module Trustie
end end
def jpeg?(data) def jpeg?(data)
data[0,4]== 0xff.chr + 0xd8.chr + 0xff.chr + 0xe0.chr data[0,3]== 0xff.chr + 0xd8.chr + 0xff.chr
end end
def png?(data) def png?(data)
data[0,2]==0x89.chr + 80.chr data[0,2]==0x89.chr + 80.chr
end end
def image? def image?
begin data = ''
f = File.open(@file,'rb') # rb means to read using binary if @file.respond_to?(:read)
return false if f.size < 9 data = @file.read(9)
data = f.read(9) # magic numbers are up to 9 bytes @file.rewind
return bitmap?(data) || gif?(data) || jpeg?(data) || png?(data)
ensure
f.close
end end
return false if data.size < 9
bitmap?(data) || gif?(data) || jpeg?(data) || png?(data)
end end
def compress(size=300) def compress(size=300)

View File

@ -0,0 +1,15 @@
require 'rails_helper'
RSpec.describe "avatar request", type: :request do
describe "上传头像" do
let(:user){FactoryGirl.create(:user)}
it "参数正确,可以成功上传头像" do
data = File.open("#{Rails.root}/spec/fixtures/test.jpg").read
binding.pry
post upload_avatar_path(source_type: 'User', source_id: user.id, filename: 'test.jpg')
expect(response).to have_http_status(:success)
expect(response.body).to include(/\/images\/avatars\/User\//)
end
end
end