Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

validate shop storage before theme upload, duplicate and install, #388.

  • Loading branch information...
commit b603070056d9746d5140ec253b418d790eecb6b8 1 parent 9494ce2
@saberma saberma authored
View
7 app/assets/javascripts/backbone/views/themes/index.js.coffee
@@ -17,7 +17,7 @@ App.Views.Theme.Index = Backbone.View.extend
upload: ->
if (App.published_themes.length + App.unpublished_themes.length) >= 8
- $('.message').show()
+ $('#exceed_message').show()
error_msg '不能上传主题!最多只能安装8个主题!'
return false
self = this
@@ -41,8 +41,11 @@ App.Views.Theme.Index = Backbone.View.extend
else if responseJSON['missing']
error_msg("上传失败,缺少 #{responseJSON['missing']}", 5000)
else if responseJSON['exceed'] # 超过个数限制(注意要计算解压失败后的theme记录role=wait,这些记录不会在页面中显示)
- $('.message').show()
+ $('#exceed_message').show()
error_msg '不能上传主题!最多只能安装8个主题!'
+ else if responseJSON['storage_full'] # 商店容量已经用完
+ $('#storage_full_message').show()
+ error_msg '操作不成功!商店容量已用完!'
else if responseJSON['id'] # 上传成功,校验通过
self.cancel()
$('#theme-progress-bar').show()
View
5 app/assets/javascripts/backbone/views/themes/show.js.coffee
@@ -37,8 +37,11 @@ App.Views.Theme.Show = Backbone.View.extend
$.post "/admin/themes/#{@model.id}/duplicate", (data) ->
self.$('.duplicate-theme a').removeClass('disabledrow')
if data['exceed']
- $('.message').show()
+ $('#exceed_message').show()
error_msg '操作不成功!最多只能安装8个主题!'
+ else if data['storage_full']
+ $('#storage_full_message').show()
+ error_msg '操作不成功!商店容量已用完!'
else
App.unpublished_themes.add data
false
View
7 app/controllers/admin/themes_controller.rb
@@ -17,6 +17,8 @@ def install
if authorization.valid?
if shop.themes.exceed? # 超出主题数则不更新
render json: {error: '商店的主题总数不能超过8个,请删除不再使用的主题!'} and return
+ elsif !shop.storage_idle? # 超出商店容量
+ render json: {error: I18n.t('activerecord.errors.models.shop.attributes.storage.full')} and return
else
theme = Theme.where(handle: params[:handle], style_handle: params[:style_handle]).first
shop.themes.install theme
@@ -43,6 +45,9 @@ def upload # 上传主题(只检查必须的文件,解压操作转入后台运
if shop.themes.exceed?
request.raw_post # fixed: 需要先接收数据,否则浏览器直接显示上传被cancle,无法获取返回的json数据
render text: {exceed: true}.to_json and return
+ elsif !shop.storage_idle?
+ request.raw_post
+ render text: {storage_full: true}.to_json and return
end
path = Rails.root.join 'tmp', 'themes', shop.id.to_s
qqfile = QqFile.new params[:qqfile], request
@@ -114,6 +119,8 @@ def background_queue_status # 检查主题是否解压完成
def duplicate # 复制主题
if shop.themes.exceed?
render json: {exceed: true}
+ elsif !shop.storage_idle?
+ render json: {storage_full: true}
else
duplicate_theme = theme.duplicate
render json: duplicate_theme.to_json(except: [:created_at, :updated_at])
View
10 app/models/shop.rb
@@ -81,7 +81,7 @@ def plan_unlimited?
end
def storage # 已占用的容量(如要支持windows可修改为循环获取目录大小)
- #Rails.cache.fetch("shop_storage_#{self.id}") do
+ #Rails.cache.fetch(self.storage_cache_key) do
`du -sm #{self.path} | awk '{print $1}'`.to_i # 以M为单位
#end
end
@@ -150,6 +150,14 @@ def format_money_in_emails(money)
end
+ begin 'cache_key' # 缓存时使用的key
+
+ def storage_cache_key
+ "shop_storage_#{self.id}"
+ end
+
+ end
+
after_destroy do # 删除对应的目录
FileUtils.rm_rf self.path
FileUtils.rm_rf self.public_path
View
6 app/validators/storage_validator.rb
@@ -3,10 +3,10 @@ class StorageValidator < ActiveModel::Validator #验证容量是否超过商店
# 需要限制的地方:
# 1. 图片上传
- # 2. 主题上传
- # 3. 附件上传
+ # 2. 主题上传、复制和安装(直接判断)
+ # 3. 附件上传(直接判断)
def validate(record)
- record.errors[:base] << "商店容量已经用完,详情请进入[帐号]页面查看" unless record.shop.storage_idle?
+ record.errors[:base] << I18n.t('activerecord.errors.models.shop.attributes.storage.full') unless record.shop.storage_idle?
end
end
View
3  app/views/admin/themes/index.html.haml
@@ -8,7 +8,8 @@
.import-wrapper
#theme-progress-bar.box2.import-in-progress.c.toggle(style='display: none') 正在处理您的主题文件…
#finished-dialog.box2.import-complete.c.toggle(style='display: none')
-.message.c.hide 最多只能安装8个主题,安装更多主题前请先删除现有主题。
+#exceed_message.message.c.hide 最多只能安装8个主题,安装更多主题前请先删除现有主题。
+#storage_full_message.message.c.hide=I18n.t('activerecord.errors.models.shop.attributes.storage.full')
#upload-theme.box2.ssb.clearfix(style="display:none;")
.fl.st(style="margin-right: 15px")
%a(href="#")=image_tag 'admin/icons/arrow-up-large.gif'
View
2  config/locales/cn.yml
@@ -200,6 +200,8 @@ cn:
taken: 已被占用
name:
blank: 不能为空
+ storage:
+ full: 商店容量已经用完,详情请进入[帐号]页面查看
product:
attributes:
title:
View
53 spec/controllers/admin/themes_controller_spec.rb
@@ -33,14 +33,32 @@
context '#duplicate' do # 复制主题
- it 'should be duplicate' do
+ before do
shop.themes.install theme_dark # 原主题会置为[未发布]状态
+ end
+
+ it 'should be duplicate' do
expect do
put :duplicate, id: theme.id
JSON(response.body)['shop_theme']['role'].should eql 'unpublished'
end.should change(ShopTheme, :count).by(1)
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
+ put :duplicate, id: theme.id
+ JSON(response.body)['storage_full'].should eql true
+ end
+
+ end
+
+ end
+
end
context '#upload' do # 上传主题
@@ -105,7 +123,19 @@
end
- describe 'ie', focus: true do # 支持ie浏览器上传
+ context 'shop storage is not idle' do # 商店容量已用完
+
+ before { Rails.cache.write(shop.storage_cache_key, 101) }
+
+ it 'should be fail' do
+ raw_attach_file File.join(zip_path, 'woodland-missing-templates-index.zip')
+ post :upload, qqfile: 'woodland.zip'
+ JSON(response.body)['storage_full'].should eql true
+ end
+
+ end
+
+ describe 'ie' do # 支持ie浏览器上传
it 'should be success' do
post :upload, qqfile: Rack::Test::UploadedFile.new(File.join(zip_path, 'woodland.zip'))
@@ -119,17 +149,34 @@
context '#api' do # 安装主题
- it 'should be install' do # issues#228
+ before do
authorization = mock('authorization')
authorization.stub!(:owner).and_return(shop)
authorization.stub!(:valid?).and_return(true)
OAuth2::Provider.stub!(:access_token).and_return(authorization)
+ end
+
+ it 'should be install' do # issues#228
expect do
post :install, handle: theme_slate.handle, style_handle: theme_slate.style_handle
response.should be_success
end.should change(ShopTheme, :count).by(1)
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
+ post :install, handle: theme_slate.handle, style_handle: theme_slate.style_handle
+ JSON(response.body)['error'].should_not be_blank
+ end
+
+ end
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.