diff --git a/Gemfile b/Gemfile index 0103a18b5..0aa77ae99 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,8 @@ gem 'acts-as-taggable-on', '2.4.1' gem 'spreadsheet' gem 'ruby-ole' gem 'rails_kindeditor',path:'lib/rails_kindeditor' +gem "rmagick", ">= 2.0.0" + group :development do gem 'grape-swagger' #gem 'grape-swagger-ui', git: 'https://github.com/guange2015/grape-swagger-ui.git' @@ -48,14 +50,6 @@ group :test do gem 'selenium-webdriver', '~> 2.42.0' gem "faker" - # platforms :mri, :mingw do - # group :rmagick do - # # RMagick 2 supports ruby 1.9 - # # RMagick 1 would be fine for ruby 1.8 but Bundler does not support - # # different requirements for the same gem on different platforms - # gem "rmagick", ">= 2.0.0" - # end - #end end # Gems used only for assets and not required diff --git a/app/controllers/avatar_controller.rb b/app/controllers/avatar_controller.rb index 771a14fe8..84b8a92e4 100644 --- a/app/controllers/avatar_controller.rb +++ b/app/controllers/avatar_controller.rb @@ -56,25 +56,9 @@ class AvatarController < ApplicationController # self.digest = md5.hexdigest end @temp_file = nil - # @avatar = Avatar.new(:receive_file => request.raw_post) - # @avatar.source_id = User.current.id - # @avatar.image_file = params[:filename].presence || Redmine::Utils.random_hex(16) - # saved = @avatar.save - begin - f = Magick::ImageList.new(diskfile) - # gif格式不再做大小处理 - if f.format != 'GIF' - width = 300.0 - proportion = (width/f[0].columns) - height = (f[0].rows*proportion) - f.resize_to_fill!(width,height) - f.write(diskfile) - end - - rescue Exception => e - logger.error "[Error] avatar : avatar_controller#upload ===> #{e}" - end + image = Trustie::Utils::Image.new(diskfile,true) + image.compress(300) respond_to do |format| format.js diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index d4f98749a..ebb7c2fc8 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -95,76 +95,46 @@ class MyController < ApplicationController @pref = @user.pref diskfile = disk_filename('User', @user.id) diskfile1 = diskfile + 'temp' - if request.post? - @user.safe_attributes = params[:user] - @user.pref.attributes = params[:pref] - @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') - @user.login = params[:login] - unless @user.user_extensions.nil? - if @user.user_extensions.identity == 2 - @user.firstname = params[:enterprise_name] - end - end - - @se = @user.extensions - if params[:occupation].to_i.to_s == params[:occupation] - @se.school_id = params[:occupation] - else - @se.occupation = params[:occupation] - end - @se.gender = params[:gender] - @se.location = params[:province] if params[:province] - @se.location_city = params[:city] if params[:city] - @se.identity = params[:identity].to_i if params[:identity] - @se.technical_title = params[:technical_title] if params[:technical_title] - @se.student_id = params[:no] if params[:no] - - if @user.save && @se.save - # 头像保存 - if File.exist?(diskfile1) - if File.exist?(diskfile) - File.delete(diskfile) - end - File.open(diskfile1, "rb") do |f| - buffer = f.read(10) - if buffer != "DELETE" - File.open(diskfile1, "rb") do |f1| - File.open(diskfile, "wb") do |f| - buffer = "" - while (buffer = f1.read(8192)) - f.write(buffer) - end - end - end - - # File.rename(diskfile + 'temp',diskfile); - end + begin + if request.post? + @user.safe_attributes = params[:user] + @user.pref.attributes = params[:pref] + @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') + @user.login = params[:login] + unless @user.user_extensions.nil? + if @user.user_extensions.identity == 2 + @user.firstname = params[:enterprise_name] end end - # 确保文件被删除 - if File.exist?(diskfile1) - File.delete(diskfile1) + @se = @user.extensions + if params[:occupation].to_i.to_s == params[:occupation] + @se.school_id = params[:occupation] + else + @se.occupation = params[:occupation] end + @se.gender = params[:gender] + @se.location = params[:province] if params[:province] + @se.location_city = params[:city] if params[:city] + @se.identity = params[:identity].to_i if params[:identity] + @se.technical_title = params[:technical_title] if params[:technical_title] + @se.student_id = params[:no] if params[:no] - @user.pref.save - @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) - set_language_if_valid @user.language - flash[:notice] = l(:notice_account_updated) - redirect_to user_url(@user) - return - else - # 确保文件被删除 - if File.exist?(diskfile1) - File.delete(diskfile1) + if @user.save && @se.save + # 头像保存 + FileUtils.mv diskfile1, diskfile, force: true + @user.pref.save + @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) + set_language_if_valid @user.language + flash[:notice] = l(:notice_account_updated) + redirect_to user_url(@user) + return + else + @user.login = lg end - @user.login = lg - end - else - # 确保文件被删除 - if File.exist?(diskfile1) - File.delete(diskfile1) end + ensure + File.delete(diskfile1) if File.exist?(diskfile1) end end diff --git a/app/views/avatar/_avatar_form.html.erb b/app/views/avatar/_avatar_form.html.erb index 56f1f130e..43aaf9132 100644 --- a/app/views/avatar/_avatar_form.html.erb +++ b/app/views/avatar/_avatar_form.html.erb @@ -73,4 +73,7 @@ <% content_for :header_tags do %> <%= javascript_include_tag 'avatars' %> <% end %> - \ No newline at end of file + + + + diff --git a/app/views/avatar/upload.js.erb b/app/views/avatar/upload.js.erb index a7a341f10..b39e308ec 100644 --- a/app/views/avatar/upload.js.erb +++ b/app/views/avatar/upload.js.erb @@ -1,4 +1,4 @@ var imgSpan = $('#avatar_image'); -imgSpan.attr({"src":'<%= @urlfile.to_s << "?" << Time.now.to_s%>'}); +imgSpan.attr({"src":'<%= "#{@urlfile.to_s}?#{Time.now.to_i}" %>'}); diff --git a/lib/tasks/avatar.rake b/lib/tasks/avatar.rake new file mode 100644 index 000000000..9fe4f34bb --- /dev/null +++ b/lib/tasks/avatar.rake @@ -0,0 +1,11 @@ +desc "compress and backup avatar" +task :compress_avatar => :environment do + path = File.join(Rails.root, "public/images/avatars/User") + Dir.foreach(path) do |f| + if f.to_s =~ /^\d+$/ + puts f + image = Trustie::Utils::Image.new(File.join(path,f), true) + image.compress(300) + end + end +end diff --git a/lib/trustie.rb b/lib/trustie.rb index ff70d118c..b6cec3c86 100644 --- a/lib/trustie.rb +++ b/lib/trustie.rb @@ -1 +1,2 @@ -require 'trustie/utils' \ No newline at end of file +require 'trustie/utils' +require 'trustie/utils/image' diff --git a/lib/trustie/utils.rb b/lib/trustie/utils.rb index 7d0b2a272..45e5f0028 100644 --- a/lib/trustie/utils.rb +++ b/lib/trustie/utils.rb @@ -17,4 +17,4 @@ end if __FILE__ == $0 puts Trustie::Utils.digest('/Users/guange/Downloads/QQ_V4.0.2.dmg') -end \ No newline at end of file +end diff --git a/lib/trustie/utils/image.rb b/lib/trustie/utils/image.rb new file mode 100644 index 000000000..849c155ee --- /dev/null +++ b/lib/trustie/utils/image.rb @@ -0,0 +1,35 @@ +#coding=utf-8 + +module Trustie + module Utils + class Image + def initialize(file, bak) + @file = file + @bak = bak + end + + def compress(size=300) + backup if @bak + begin + f = Magick::ImageList.new(@file) + if f.format != 'GIF' + width = size + if f[0].columns > width + proportion = (width/f[0].columns.to_f) + height = (f[0].rows*proportion) + f.resize_to_fill!(width,height.to_i) + f.write(@file) + end + end + rescue Exception => e + logger.error "[Error] compress : ===> #{e}" + end + end + + def backup + FileUtils.cp @file, "#{@file}.bak" + end + + end + end +end diff --git a/public/javascripts/avatars.js b/public/javascripts/avatars.js index 3d8420dc0..a8f10edf0 100644 --- a/public/javascripts/avatars.js +++ b/public/javascripts/avatars.js @@ -190,6 +190,9 @@ function dragOutHandler(e) { } function setupFileDrop() { + $('#avatar_image').on('click', function(){ + console.log("click"); + }); if (window.File && window.FileList && window.ProgressEvent && window.FormData) { $.event.fixHooks.drop = { props: [ 'dataTransfer' ] };