From 49d836d3314ac2c3a445818e124b2f66d5908c64 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Fri, 15 May 2015 23:47:22 +0800 Subject: [PATCH 01/35] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=B4=E5=83=8F?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=92=8C=E5=9B=BE=E7=89=87=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=9C=A8=E5=90=8E=E5=8F=B0=E5=8A=A0=E5=85=A5=E9=99=90=E5=88=B6?= =?UTF-8?q?,=E4=B8=94=E9=80=9A=E8=BF=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/avatar_controller.rb | 68 ++++++++++++++------------ config/locales/en.yml | 1 + config/locales/zh.yml | 1 + lib/trustie/utils/image.rb | 28 ++++++++++- public/javascripts/jq-upload/upload.js | 15 ++++-- spec/requests/course_request_spec.rb | 42 +++++++++++++++- 6 files changed, 120 insertions(+), 35 deletions(-) diff --git a/app/controllers/avatar_controller.rb b/app/controllers/avatar_controller.rb index ef7cdf2f7..afd206c92 100644 --- a/app/controllers/avatar_controller.rb +++ b/app/controllers/avatar_controller.rb @@ -1,9 +1,9 @@ class AvatarController < ApplicationController - - + + include ActionView::Helpers::NumberHelper #before_filter :set_cache_buster include AvatarHelper - + def upload # Make sure that API users get used to set this content type # as it won't trigger Rails' automatic parsing of the request body for parameters @@ -29,43 +29,51 @@ class AvatarController < ApplicationController end if @temp_file && (@temp_file.size > 0) - diskfile=disk_filename(@source_type,@source_id) - @urlfile='/' << File.join("images","avatars",avatar_directory(@source_type),avatar_filename(@source_id,@image_file)) + if @temp_file.size > Setting.upload_avatar_max_size.to_i + @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.tempfile.path).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" @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 + + 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 + + Trustie::Utils::Image.new(diskfile,true).compress(300) + @status = 0 + @msg = '' + else + @status = 2 + @msg = l(:not_valid_image_file) end -# self.digest = md5.hexdigest end @temp_file = nil - image = Trustie::Utils::Image.new(diskfile,true) - image.compress(300) - respond_to do |format| 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 } format.js diff --git a/config/locales/en.yml b/config/locales/en.yml index 8989ae36a..844818ae4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1521,4 +1521,5 @@ en: label_commit_failed: commit failed #api end error_upload_avatar_to_large: "too big (%{max_size})" + not_valid_image_file: not a valid image file diff --git a/config/locales/zh.yml b/config/locales/zh.yml index 5fcb1fb5c..a91faac0f 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -1984,3 +1984,4 @@ zh: label_code: 代码 error_upload_avatar_to_large: "超过大小限制 (%{max_size})" + not_valid_image_file: 不是有效的图片文件 diff --git a/lib/trustie/utils/image.rb b/lib/trustie/utils/image.rb index 9178b291a..f4ec328b3 100644 --- a/lib/trustie/utils/image.rb +++ b/lib/trustie/utils/image.rb @@ -3,11 +3,37 @@ module Trustie module Utils class Image - def initialize(file, bak) + def initialize(file, bak=false) @file = file @bak = bak end + def bitmap?(data) + data[0,2]==77.chr + 66.chr + end + + def gif?(data) + data[0,4]==71.chr + 73.chr + 70.chr + 56.chr + end + + def jpeg?(data) + data[0,4]== 0xff.chr + 0xd8.chr + 0xff.chr + 0xe0.chr + end + def png?(data) + data[0,2]==0x89.chr + 80.chr + end + + def image? + begin + f = File.open(@file,'rb') # rb means to read using binary + return false if f.size < 9 + data = f.read(9) # magic numbers are up to 9 bytes + return bitmap?(data) || gif?(data) || jpeg?(data) || png?(data) + ensure + f.close + end + end + def compress(size=300) backup if @bak begin diff --git a/public/javascripts/jq-upload/upload.js b/public/javascripts/jq-upload/upload.js index 985450648..71dbefc32 100644 --- a/public/javascripts/jq-upload/upload.js +++ b/public/javascripts/jq-upload/upload.js @@ -44,9 +44,18 @@ $(function() { }, done: function(e, data) { var imgSpan = jQuery('#avatar_image'); - imgSpan.attr({ - "src": data.result.text ? data.result.text() : data.result - }); + var result = data.result.text ? data.result.text() : data.result; + if(result){ + var o = JSON.parse(result); + if(o.status == 0){ + imgSpan.attr({ + "src": o.url + }); + } else { + alert(o.message); + } + } + } }); }); diff --git a/spec/requests/course_request_spec.rb b/spec/requests/course_request_spec.rb index d58df398e..99e4486b0 100644 --- a/spec/requests/course_request_spec.rb +++ b/spec/requests/course_request_spec.rb @@ -35,13 +35,53 @@ RSpec.describe "课程", :type => :request do context "修改课程图片" do include Rack::Test::Methods let(:avatar) {Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test.jpg",'image/jpg')} + context "正常图片上传成功" do subject(:resp) {post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),"avatar"=>{image: avatar}} it{ expect(subject).to be_ok } it{ expect(subject.body).not_to be_empty } + it "状态要为0" do + o = ActiveSupport::JSON.decode(subject.body) + expect(o["status"]).to eq(0) + end + it "要回传图片地址" do + o = ActiveSupport::JSON.decode(subject.body) + expect(o["url"]).not_to be_empty + end + end + + context "不是图片,上传失败" do + let(:invalid_avatar) {Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/hah.txt",'text/plain')} + before do + resp = post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),"avatar"=>{image: invalid_avatar} + @o = ActiveSupport::JSON.decode(resp.body) + end + it "状态要为0" do + expect(@o["status"]).not_to eq(0) + end + it "要回传错误信息" do + expect(@o["message"]).to be_include("图片") + end + end + + context "文件过大,上传失败" do + before do + big_file = Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test.jpg",'image/jpg') + allow(ActionDispatch::Http::UploadedFile).to receive(:new).and_return(double('BigFile',size: 10*1024*1024, original_filename: 'rais.jpg', tempfile: nil)) + # trace = TracePoint.new(:call) do |tp| + # p [tp.lineno, tp.defined_class, tp.method_id, tp.event] if tp.method_id == :post + # end + resp = post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),'avatar[image]'=> big_file + @o = ActiveSupport::JSON.decode(resp.body) + end + it "状态要为0" do + expect(@o["status"]).not_to eq(0) + end + it "要回传错误信息" do + expect(@o["message"]).to be_include("大") + end end - it "不是图片,上传失败" end end From d546069051efe627fa2eb234255dadbb04d3c05e Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sat, 16 May 2015 09:59:14 +0800 Subject: [PATCH 02/35] =?UTF-8?q?spec=20=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/forum_observer_spec.rb | 5 ----- spec/models/memo_observer_spec.rb | 5 ----- spec/requests/course_request_spec.rb | 4 ++-- 3 files changed, 2 insertions(+), 12 deletions(-) delete mode 100644 spec/models/forum_observer_spec.rb delete mode 100644 spec/models/memo_observer_spec.rb diff --git a/spec/models/forum_observer_spec.rb b/spec/models/forum_observer_spec.rb deleted file mode 100644 index 76d68fafa..000000000 --- a/spec/models/forum_observer_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe ForumObserver do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/memo_observer_spec.rb b/spec/models/memo_observer_spec.rb deleted file mode 100644 index 82603ba7a..000000000 --- a/spec/models/memo_observer_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe MemoObserver do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/requests/course_request_spec.rb b/spec/requests/course_request_spec.rb index 99e4486b0..feca37259 100644 --- a/spec/requests/course_request_spec.rb +++ b/spec/requests/course_request_spec.rb @@ -56,7 +56,7 @@ RSpec.describe "课程", :type => :request do resp = post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),"avatar"=>{image: invalid_avatar} @o = ActiveSupport::JSON.decode(resp.body) end - it "状态要为0" do + it "状态不为0" do expect(@o["status"]).not_to eq(0) end it "要回传错误信息" do @@ -74,7 +74,7 @@ RSpec.describe "课程", :type => :request do resp = post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),'avatar[image]'=> big_file @o = ActiveSupport::JSON.decode(resp.body) end - it "状态要为0" do + it "状态不为0" do expect(@o["status"]).not_to eq(0) end it "要回传错误信息" do From 9b0602fd9ca506e0d1502951bee9c4383bfd1e96 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Mon, 18 May 2015 17:26:50 +0800 Subject: [PATCH 03/35] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E4=B8=8B=E8=BD=BDspec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/locales/commons/en.yml | 3 ++- config/locales/commons/zh.yml | 3 ++- spec/factories/attachments.rb | 10 ++++++++ spec/factories/homeworks.rb | 17 ++++++++++++++ spec/factories/users.rb | 8 +++++++ spec/requests/zipdown_request_spec.rb | 34 +++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 spec/factories/attachments.rb create mode 100644 spec/factories/homeworks.rb diff --git a/config/locales/commons/en.yml b/config/locales/commons/en.yml index 5eb92f07e..c0e29b797 100644 --- a/config/locales/commons/en.yml +++ b/config/locales/commons/en.yml @@ -113,6 +113,7 @@ en: one: "1 error prohibited this %{model} from being saved" other: "%{count} errors prohibited this %{model} from being saved" messages: + record_invalid: "validate error: %{errors}" inclusion: "is not included in the list" exclusion: "is reserved" invalid: "is invalid" @@ -428,4 +429,4 @@ en: previous: "« Previous" next: "Next »" truncate: "..." - \ No newline at end of file + diff --git a/config/locales/commons/zh.yml b/config/locales/commons/zh.yml index 231c5280a..9ba8cba7a 100644 --- a/config/locales/commons/zh.yml +++ b/config/locales/commons/zh.yml @@ -121,6 +121,7 @@ zh: one: "由于发生了一个错误 %{model} 无法保存" other: "%{count} 个错误使得 %{model} 无法保存" messages: + record_invalid: "校验失败: %{errors}" inclusion: "不包含于列表中" exclusion: "是保留关键字" invalid: "是无效的" @@ -435,4 +436,4 @@ zh: last: "末页 »" previous: "« 上一页" next: "下一页 »" - truncate: "..." \ No newline at end of file + truncate: "..." diff --git a/spec/factories/attachments.rb b/spec/factories/attachments.rb new file mode 100644 index 000000000..da8e787ed --- /dev/null +++ b/spec/factories/attachments.rb @@ -0,0 +1,10 @@ +#coding=utf-8 +# +FactoryGirl.define do + factory :attachment do + filename "11.gif" + filesize 296833 + digest "8a74e086d7716f89bc4fbac0606589c7" + disk_directory "2015/05" + end +end diff --git a/spec/factories/homeworks.rb b/spec/factories/homeworks.rb new file mode 100644 index 000000000..208255a8d --- /dev/null +++ b/spec/factories/homeworks.rb @@ -0,0 +1,17 @@ +#coding=utf-8 +# +#:author_id, :budget, :deadline, :name, :description, :homework_type, :password + +FactoryGirl.define do + factory :homework, class: Bid do + name "test homework" + budget 0 + deadline {(Time.now+1.days).strftime('%Y-%m-%d')} + description "description" + homework_type 3 + reward_type 3 + end + + factory :homework_attach, class: HomeworkAttach do + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 2c695b920..4595aacc5 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,4 +6,12 @@ FactoryGirl.define do password "foobar111" password_confirmation "foobar111" end + + factory :student, class: User do + login "student" + mail "student@example.com" + password "foobar111" + password_confirmation "foobar111" + end + end diff --git a/spec/requests/zipdown_request_spec.rb b/spec/requests/zipdown_request_spec.rb index eb179f104..72831d26f 100644 --- a/spec/requests/zipdown_request_spec.rb +++ b/spec/requests/zipdown_request_spec.rb @@ -1,6 +1,40 @@ require 'rails_helper' +require 'shared_account_spec' +# "attachments"=>{"1"=>{"filename"=>"11.gif", "description"=>"", "is_public_checkbox"=>"1", "token"=>"33731.8a74e086d7716f89bc4fbac0606589c7"}} RSpec.describe "作业打包下载", :type => :request do + let(:student){FactoryGirl.create(:student)} describe "单独下载某学生作业" do + include_context "create user" + before { + FactoryGirl.create(:user) + shared_login + @homework = FactoryGirl.create(:homework, author_id: current_user.id) + + @attch = HomeworkAttach.new + @attch.bid_id = @homework.id + @attch.user_id = student.id + @attachment = Attachment.new(:file => File.open(File.join(Rails.root, "spec/fixtures/test.jpg"))) + @attachment.author = User.current + @attachment.container_type = 'HomeworkAttach' + @attachment.container_id = @attch.id + @attachment.filename = "test.jpg" + @attachment.save + params = {"1"=>{"filename" => "test.jpg", "description" =>"", + "is_public_checkbox"=>"1", + "token" => "#{@attachment.id}.#{@attachment.digest}" } + } + @attch.save_attachments(params) + @attch.name = "test.jpg" + @attch.save! + } + it "正常下载" do + uu = current_user + allow(uu).to receive(:admin?).and_return(true) + allow(User).to receive(:current).and_return(uu) + get zipdown_download_user_homework_path, {homework:@attch.id} + expect(response).to have_http_status(:success) + expect(response.content_type).to eq(Mime::Type.new("applcation/zip",:zip)) + end end end From 1355dc805f45ec643624f160914681d6a6f71e80 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Mon, 18 May 2015 17:31:53 +0800 Subject: [PATCH 04/35] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E8=BF=94=E5=9B=9E500=E9=94=99=E8=AF=AF=20#2610?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/layouts/base_users.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/base_users.html.erb b/app/views/layouts/base_users.html.erb index 1f74ccd5d..269017eb8 100644 --- a/app/views/layouts/base_users.html.erb +++ b/app/views/layouts/base_users.html.erb @@ -199,7 +199,7 @@ <% end %> - <% elsif @user.user_extensions.identity == 3 && @user.user_extensions.occupation.empty? %> + <% elsif @user.user_extensions.identity == 3 && @user.user_extensions.occupation %>
+