Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Shop scoped file system abstraction
- #
- #
- # store.files['templates/product.liquid'].value #=> ...source...
- # store.files['templates/product.liquid'].exist? #=> true
- # store.files['templates/product.liquid'] = 'New source' #=> updates database table
- #
- # store.files['files/logo.gif'] = '..gif98' #=> updates file system
- #
- # store.files['downloadables/logo.gif'] = '..gif98' #=> updates s3
- class ShopAssetCloud < AssetCloud::Base
- attr_accessor :shop
- attr_accessor :shop_root
- # By default deny any operations
- bucket AssetCloud::InvalidBucket
- # Liquid templates are stored under templates/ and layout/ paths.
- # We now keep liquid templates in the database and not in the file
- # system anymore so the TemplateTableBucket is responsible for
- # translating the calls into ActiveRecord speak. (temporarily disabled)
- # bucket :templates, TemplateTableBucket
- # bucket :layout, TemplateTableBucket
- bucket :templates, TemplateMultiplexerBucket
- bucket :layout, TemplateMultiplexerBucket
- # products images, theme assets and uploaded auxillary files
- bucket :products, S3FileMultiplexerBucket
- bucket :assets, S3FileMultiplexerBucket
- bucket :files, S3FileMultiplexerBucket
- # Keep temporary files in private storage
- # We will dump things like uploaded CSV files here which may
- # too big for the DB.
- bucket :tmp, PrivateStorageBucket
- # digital goods
- # bucket :downloadables, AssetCloud::ProtectedS3Bucket
- def initialize(shop, root, http)
- self.shop = shop
- self.shop_root = shop.resources_dir
- super(root, http)
- end
- def total_size
- shop.stored_assets.sum(:size) || 0
- end
- def delete_all
- shop.stored_assets.destroy_all
- end
- def path
- File.join(root, shop_root)
- end
- def url_for(key, secure = false)
- bucket = bucket_for(key)
- if bucket.respond_to?(:url_for)
- bucket.url_for(key, secure)
- else
- shop.url_for(super, secure)
- end
- end
- def stat(key)
- if asset = shop.stored_assets.find_by_key(key)
- AssetCloud::Metadata.new(true, asset.size, asset.created_at, asset.updated_at)
- else
- AssetCloud::Metadata.new(false)
- end
- end
- def ls(key)
- shop.stored_assets.ls(key).collect do |a|
- AssetCloud::Asset.at(self, a.key, nil, AssetCloud::Metadata.new(true, a.size, a.created_at, a.updated_at))
- end
- end
- after_write :update_stored_asset_size
- after_delete :remove_stored_asset
- protected
- def update_stored_asset_size(key, data)
- shop.new_version!
- shop.stored_assets.update(key, data.length)
- end
- def remove_stored_asset(key)
- shop.new_version!
- shop.stored_assets.remove(key)
- end
- end
Add Comment
Please, Sign In to add comment