Guest User

Untitled

a guest
Feb 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. # Shop scoped file system abstraction
  2. #
  3. #
  4. # store.files['templates/product.liquid'].value #=> ...source...
  5. # store.files['templates/product.liquid'].exist? #=> true
  6. # store.files['templates/product.liquid'] = 'New source' #=> updates database table
  7. #
  8. # store.files['files/logo.gif'] = '..gif98' #=> updates file system
  9. #
  10. # store.files['downloadables/logo.gif'] = '..gif98' #=> updates s3
  11. class ShopAssetCloud < AssetCloud::Base
  12. attr_accessor :shop
  13. attr_accessor :shop_root
  14.  
  15. # By default deny any operations
  16. bucket AssetCloud::InvalidBucket
  17.  
  18. # Liquid templates are stored under templates/ and layout/ paths.
  19. # We now keep liquid templates in the database and not in the file
  20. # system anymore so the TemplateTableBucket is responsible for
  21. # translating the calls into ActiveRecord speak. (temporarily disabled)
  22. # bucket :templates, TemplateTableBucket
  23. # bucket :layout, TemplateTableBucket
  24.  
  25. bucket :templates, TemplateMultiplexerBucket
  26. bucket :layout, TemplateMultiplexerBucket
  27.  
  28. # products images, theme assets and uploaded auxillary files
  29. bucket :products, S3FileMultiplexerBucket
  30. bucket :assets, S3FileMultiplexerBucket
  31. bucket :files, S3FileMultiplexerBucket
  32.  
  33. # Keep temporary files in private storage
  34. # We will dump things like uploaded CSV files here which may
  35. # too big for the DB.
  36. bucket :tmp, PrivateStorageBucket
  37.  
  38. # digital goods
  39. # bucket :downloadables, AssetCloud::ProtectedS3Bucket
  40.  
  41. def initialize(shop, root, http)
  42. self.shop = shop
  43. self.shop_root = shop.resources_dir
  44.  
  45. super(root, http)
  46. end
  47.  
  48. def total_size
  49. shop.stored_assets.sum(:size) || 0
  50. end
  51.  
  52. def delete_all
  53. shop.stored_assets.destroy_all
  54. end
  55.  
  56. def path
  57. File.join(root, shop_root)
  58. end
  59.  
  60. def url_for(key, secure = false)
  61. bucket = bucket_for(key)
  62. if bucket.respond_to?(:url_for)
  63. bucket.url_for(key, secure)
  64. else
  65. shop.url_for(super, secure)
  66. end
  67. end
  68.  
  69. def stat(key)
  70. if asset = shop.stored_assets.find_by_key(key)
  71. AssetCloud::Metadata.new(true, asset.size, asset.created_at, asset.updated_at)
  72. else
  73. AssetCloud::Metadata.new(false)
  74. end
  75. end
  76.  
  77. def ls(key)
  78. shop.stored_assets.ls(key).collect do |a|
  79. AssetCloud::Asset.at(self, a.key, nil, AssetCloud::Metadata.new(true, a.size, a.created_at, a.updated_at))
  80. end
  81. end
  82.  
  83. after_write :update_stored_asset_size
  84. after_delete :remove_stored_asset
  85.  
  86. protected
  87.  
  88. def update_stored_asset_size(key, data)
  89. shop.new_version!
  90. shop.stored_assets.update(key, data.length)
  91. end
  92.  
  93. def remove_stored_asset(key)
  94. shop.new_version!
  95. shop.stored_assets.remove(key)
  96. end
  97.  
  98. end
Add Comment
Please, Sign In to add comment