Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

validate shop storage before theme asset upload, #388.

  • Loading branch information...
commit 86246c60beb10efc1f468f73536953ebb09210bd 1 parent b603070
@saberma saberma authored
View
5 ...pts/backbone/views/assets/index/sidebar.js.coffee → ...backbone/views/assets/index/sidebar.js.coffee.erb
@@ -36,7 +36,10 @@ App.Views.Asset.Index.Sidebar = Backbone.View.extend
onComplete: (id, file_name, responseJSON)->
$('#indicator').hide()
$(document).unbind 'mousemove'
- self.options.assets.assets.add responseJSON
+ if responseJSON['storage_full']
+ error_msg '<%= I18n.t("activerecord.errors.models.shop.attributes.storage.full") %>'
+ else
+ self.options.assets.assets.add responseJSON
$('.qq-upload-list').hide() # 不显示上传文件列表
$(".qq-upload-button").contents().first().replaceWith("选择文件")
View
5 ...ts/backbone/views/themes/settings/index.js.coffee → ...ackbone/views/themes/settings/index.js.coffee.erb
@@ -48,9 +48,12 @@ App.Views.Theme.Settings.Index = Backbone.View.extend
this.params = key: "assets/#{name}", name: name, max_width: max_width, max_height: max_height
this.params[csrf_param] = csrf_token
onComplete: (id, file_name, responseJSON)->
- msg '上传成功!'
$('#indicator').hide()
$(document).unbind 'mousemove'
+ if responseJSON['storage_full']
+ error_msg '<%= I18n.t("activerecord.errors.models.shop.attributes.storage.full") %>'
+ else
+ msg '上传成功!'
$('.qq-upload-list').hide() # 不显示上传文件列表
$(".qq-upload-button").each ->
$(this).css('padding', '2px 0').width(70).contents().first().replaceWith("选择文件")
View
3  app/controllers/admin/assets_controller.rb
@@ -23,6 +23,9 @@ def create # 新增文件
def upload # 上传asset附件
qqfile = QqFile.new params[:qqfile], request
image = qqfile.body
+ unless shop.storage_idle? # 商店容量已经用完
+ render text: {storage_full: true}.to_json and return
+ end
max_width = params['max_width']
max_height = params['max_height']
unless max_width.blank? and max_height.blank? # 限制宽高
View
4 app/models/shop.rb
@@ -81,9 +81,9 @@ def plan_unlimited?
end
def storage # 已占用的容量(如要支持windows可修改为循环获取目录大小)
- #Rails.cache.fetch(self.storage_cache_key) do
+ Rails.cache.fetch(self.storage_cache_key) do
`du -sm #{self.path} | awk '{print $1}'`.to_i # 以M为单位
- #end
+ end
end
def available?
View
8 app/validators/storage_validator.rb
@@ -2,9 +2,11 @@
class StorageValidator < ActiveModel::Validator #验证容量是否超过商店限制
# 需要限制的地方:
- # 1. 图片上传
- # 2. 主题上传、复制和安装(直接判断)
- # 3. 附件上传(直接判断)
+ # 1. 商品图片上传
+ # 2. 富文本框的图片上传
+ # 3. 主题上传、复制和安装(直接判断)
+ # 4. 附件上传(直接判断)
+ # 5. 主题设置中的图片上传(直接判断)
def validate(record)
record.errors[:base] << I18n.t('activerecord.errors.models.shop.attributes.storage.full') unless record.shop.storage_idle?
end
View
20 spec/controllers/admin/assets_controller_spec.rb
@@ -32,16 +32,32 @@
let(:file_path) { Rails.root.join('spec', 'factories', 'data', 'themes') }
- describe 'ie', focus: true do # 支持ie浏览器上传
+ before do
+ post :upload, key: 'assets/product.jpg', theme_id: theme.id, id: 0, qqfile: Rack::Test::UploadedFile.new(File.join(file_path, 'product.jpg'))
+ end
+
+ describe 'ie' do # 支持ie浏览器上传
it 'should be success' do
- post :upload, key: 'assets/product.jpg', theme_id: theme.id, id: 0, qqfile: Rack::Test::UploadedFile.new(File.join(file_path, 'product.jpg'))
response.should be_success
response.content_type.should eql 'text/html' # context_type不为'text/html'时,ie会将json作为文件下载
end
end
+ describe 'validate' do # 校验
+
+ context 'shop storage is not idle' do # 商店容量已用完
+
+ before { Rails.cache.write(shop.storage_cache_key, 101) }
+
+ it 'should be fail' do
+ JSON(response.body)['storage_full'].should be_true
+ end
+
+ end
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.