diff --git a/.gitignore b/.gitignore index db27736..4378c3e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ spec/dummy/public/system/ Gemfile.lock tmp/ .rbenv-gemsets +.env \ No newline at end of file diff --git a/Gemfile b/Gemfile index 06fe80f..8622f5f 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source "https://rubygems.org" # Declare your gem's dependencies in smithycms.gemspec. # Bundler will treat runtime dependencies like base dependencies, and # development dependencies will be added by default to the :development group. + gemspec # Declare any dependencies that are still in development here instead of in diff --git a/app/controllers/smithy/assets_controller.rb b/app/controllers/smithy/assets_controller.rb index 3a9baef..0618ef6 100644 --- a/app/controllers/smithy/assets_controller.rb +++ b/app/controllers/smithy/assets_controller.rb @@ -16,6 +16,7 @@ def new def create @asset = Asset.new(filtered_params) + @asset.set_name(filtered_params) @asset.save flash.notice = "Your asset was created" if @asset.persisted? respond_with @asset do |format| diff --git a/app/models/smithy/asset.rb b/app/models/smithy/asset.rb index 7824488..59f616a 100644 --- a/app/models/smithy/asset.rb +++ b/app/models/smithy/asset.rb @@ -1,31 +1,30 @@ module Smithy class Asset < ActiveRecord::Base validates_presence_of :file, :name + attachment :file has_many :images, :dependent => :destroy - extend ::Dragonfly::Model - dragonfly_accessor :file - include ::Smithy::Dragonfly::AssetHelper - - before_validation :set_name - before_save :set_file_uid_manually + # extend ::Dragonfly::Model + # dragonfly_accessor :file + # include ::Smithy::Dragonfly::AssetHelper + # before_save :set_file_uid_manually default_scope -> { order(:name) } - def file - # check for the jquery uploaded file first, just in case one got past the manual check. Also keeps backwards-compatibility - if self.uploaded_file_url? - dragonfly_attachments[:file].app.datastore = self.class.dragonfly_remote_datastore - self.file_url = URI.escape(self.uploaded_file_url) - elsif dragonfly_attachments[:file].to_value - dragonfly_attachments[:file].app.datastore = self.class.dragonfly_datastore - end - dragonfly_attachments[:file].to_value - end + # def file + # # check for the jquery uploaded file first, just in case one got past the manual check. Also keeps backwards-compatibility + # # if self.uploaded_file_url? + # # dragonfly_attachments[:file].app.datastore = self.class.dragonfly_remote_datastore + # # self.file_url = URI.escape(self.uploaded_file_url) + # # elsif dragonfly_attachments[:file].to_value + # # dragonfly_attachments[:file].app.datastore = self.class.dragonfly_datastore + # # end + # # dragonfly_attachments[:file].to_value + # end def file_type - ext = File.extname(file.name).sub(/^\./, '') + ext = File.extname(self.name).sub(/^\./, '') case ext when 'jpg', 'jpeg', 'gif', 'png' :image @@ -63,27 +62,26 @@ def to_liquid } end - private - def set_content_types - set_content_type(self.file, :content_type) + def set_name(params) + filename = params[:file].original_filename + if filename && !self.name? + self.name = filename end + end - # this allows dragonfly to take over management of the uploaded file. We are - # assuming that jquery-upload and dragonfly are using the same data storage... - def set_file_uid_manually - if self.uploaded_file_url? # this means it was uploaded via jquery-upload - uri = URI.parse(URI.encode(self.uploaded_file_url)) - self.file_uid = self.uploaded_file_url.sub("#{uri.scheme}://#{uri.host}/", '') - self.uploaded_file_url = nil - end - end + private + # def set_content_types + # set_content_type(self.file, :content_type) + # end - def set_name - if self.uploaded_file_url? - self.name = File.basename(self.uploaded_file_url, '.*').titleize unless self.name? - elsif self.file.present? - self.name = File.basename(self.file.name, '.*').titleize unless self.name? - end - end + # # this allows dragonfly to take over management of the uploaded file. We are + # # assuming that jquery-upload and dragonfly are using the same data storage... + # def set_file_uid_manually + # if self.uploaded_file_url? # this means it was uploaded via jquery-upload + # uri = URI.parse(URI.encode(self.uploaded_file_url)) + # self.file_uid = self.uploaded_file_url.sub("#{uri.scheme}://#{uri.host}/", '') + # self.uploaded_file_url = nil + # end + # end end end diff --git a/app/views/smithy/assets/_asset.html.erb b/app/views/smithy/assets/_asset.html.erb index 27fadb8..377ab1a 100644 --- a/app/views/smithy/assets/_asset.html.erb +++ b/app/views/smithy/assets/_asset.html.erb @@ -1,8 +1,8 @@
- <%= link_to asset.file.remote_url do %> + <%= link_to attachment_url(asset, :file) do %> <% if asset.file_type == :image %> - <%= image_tag asset.file.thumb("340x226#").url, width: 170, height: 114, alt: '' %> + <%= attachment_image_tag(asset, :file, :fill, 170, 114) %> <% elsif asset.file_type == :direct_image %> <%= image_tag asset.file.remote_url, width: 170, alt: '' %> <% else %> diff --git a/app/views/smithy/assets/_upload_form.html.erb b/app/views/smithy/assets/_upload_form.html.erb index 42f5665..183461e 100644 --- a/app/views/smithy/assets/_upload_form.html.erb +++ b/app/views/smithy/assets/_upload_form.html.erb @@ -1,6 +1,9 @@ -<%= uploader_form Smithy::Asset.new, post: assets_url, as: "asset[uploaded_file_url]" do %> +<%= semantic_form_for Smithy::Asset.new, post: assets_url do |f| %>
- <%= file_field_tag :file, multiple: true %> + <% + #file_field_tag :file, multiple: true + %> + <%= f.attachment_field :file, direct: true %> <%= submit_tag "Upload File(s)", class: 'btn btn-default' %>
<% end %> diff --git a/config/initializers/dragonfly.rb b/config/initializers/dragonfly.rb deleted file mode 100644 index c94d0a5..0000000 --- a/config/initializers/dragonfly.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'dragonfly' - -# Configure -Dragonfly.app.configure do - plugin :imagemagick, - convert_command: `which convert`.strip.presence || "/usr/local/bin/convert", - identify_command: `which identify`.strip.presence || "/usr/local/bin/identify" - - secret "7fb765cbc9f1d92d5d1a56a43193d34d4f9b54dced3e62cb4e42f25d2500dd0f" - - url_format '/uploads/assets/:job/:name' - - datastore Smithy::Asset.dragonfly_datastore -end - -# Logger -Dragonfly.logger = Rails.logger - -# Mount as middleware -Rails.application.middleware.use Dragonfly::Middleware - -# Add model functionality -if defined?(ActiveRecord::Base) - ActiveRecord::Base.extend Dragonfly::Model - ActiveRecord::Base.extend Dragonfly::Model::Validations -end diff --git a/config/initializers/refile.rb b/config/initializers/refile.rb new file mode 100644 index 0000000..9b57238 --- /dev/null +++ b/config/initializers/refile.rb @@ -0,0 +1,21 @@ +require "refile/rails" +require "refile/image_processing" +require "refile/backend/s3" + +# Manually mount Refile before Smithy Engine +Refile.automount = false +Rails.application.routes.prepend do + mount Refile.app, at: Refile.mount_point, as: :refile_app +end + +if ENV['AWS_ACCESS_KEY_ID'].present? && ENV['AWS_SECRET_ACCESS_KEY'].present? && ENV['AWS_S3_BUCKET'].present? + aws = { + access_key_id: ENV['AWS_ACCESS_KEY_ID'], + secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], + bucket: ENV['AWS_S3_BUCKET'], + } + Refile.cache = Refile::Backend::S3.new(prefix: "cache", **aws) + Refile.store = Refile::Backend::S3.new(prefix: "store", **aws) +else + Refile.store = Refile::Backend::FileSystem.new(Rails.root.join('public/system/refile', Rails.env)) +end \ No newline at end of file diff --git a/lib/smithy.rb b/lib/smithy.rb index 1f12f1c..12072aa 100644 --- a/lib/smithy.rb +++ b/lib/smithy.rb @@ -3,7 +3,6 @@ require 'smithy/dependencies' # config require 'smithy/logger' -require 'smithy/dragonfly' require 'smithy/liquid' # content formatting require 'smithy/formatter' diff --git a/lib/smithy/dependencies.rb b/lib/smithy/dependencies.rb index d8f72dd..e32bbb0 100644 --- a/lib/smithy/dependencies.rb +++ b/lib/smithy/dependencies.rb @@ -3,7 +3,6 @@ require 'awesome_nested_set' require 'bootstrap-sass' require 'awesome_nested_set' -require 'dragonfly' require 'fog' require 'font-awesome-sass' require 'formtastic' @@ -14,5 +13,7 @@ require 'jquery-fileupload-rails' require 'jquery/rails' require 'liquid' +require 'refile/rails' +require 'refile/image_processing' require 'sass-rails' require 'slodown' diff --git a/lib/smithy/dragonfly.rb b/lib/smithy/dragonfly.rb deleted file mode 100644 index aeda743..0000000 --- a/lib/smithy/dragonfly.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'smithy/dragonfly/asset_helper' -require 'smithy/dragonfly/remote_data_store' -module Smithy - module Dragonfly - - def self.resize_url(source, resize_string) - file = nil - - if source.is_a?(String) - source.strip! - if source =~ /^http/ - file = self.app.fetch_url(source) - else - file = self.app.fetch_file(File.join('public', source)) - end - elsif source.respond_to?(:url) # dragonfly uploader - file = source - else - Smithy.log :error, "Unable to resize on the fly: #{source.inspect}" - return - end - file.process(:thumb, resize_string).url - end - - def self.app - ::Dragonfly.app - end - - end -end diff --git a/lib/smithy/dragonfly/asset_helper.rb b/lib/smithy/dragonfly/asset_helper.rb deleted file mode 100644 index edefcf3..0000000 --- a/lib/smithy/dragonfly/asset_helper.rb +++ /dev/null @@ -1,67 +0,0 @@ -require 'dragonfly/s3_data_store' - -module Smithy - module Dragonfly - module AssetHelper - - extend ActiveSupport::Concern - included do - before_save :set_content_types - end - - module ClassMethods - def content_types - { - :image => ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg', 'image/x-icon', 'image/svg+xml'], - :media => [/^video/, 'application/x-shockwave-flash', 'application/x-flash-video', 'application/x-swf', /^audio/, 'application/ogg', 'application/x-mp3'], - :pdf => ['application/pdf', 'application/x-pdf'], - :stylesheet => ['text/css'], - :javascript => ['text/javascript', 'text/js', 'application/x-javascript', 'application/javascript', 'text/x-component'], - :font => ['application/x-font-ttf', 'application/vnd.ms-fontobject', 'image/svg+xml', 'application/x-woff'] - } - end - - def dragonfly_datastore - if ENV['AWS_ACCESS_KEY_ID'].present? && ENV['AWS_SECRET_ACCESS_KEY'].present? && ENV['AWS_S3_BUCKET'].present? - Dragonfly.app.configure do - # Allow amazon s3 urls to work - fetch_url_whitelist [ - /s3\.amazonaws\.com\/#{ENV['AWS_S3_BUCKET']}/, - /#{ENV['AWS_S3_BUCKET']}\.s3\.amazonaws\.com/ - ] - end - ::Dragonfly::S3DataStore.new(bucket_name: ENV['AWS_S3_BUCKET'], access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], url_scheme: 'https') - else - Dragonfly.app.configure { fetch_url_whitelist [] } - ::Dragonfly::FileDataStore.new(root_path: Rails.root.join('public/system/dragonfly', Rails.env), server_root: Rails.root.join('public')) - end - end - - def dragonfly_remote_datastore - ::Smithy::Dragonfly::DataStorage::RemoteDataStore.new - end - - end - - def set_content_types - # empty by default - override in your own models if you need to use it - end - - def set_content_type(file, content_type_column) - return unless file.present? - value = :other - content_type = file.mime_type - self.class.content_types.each_pair do |type, rules| - rules.each do |rule| - case rule - when String then value = type if content_type == rule - when Regexp then value = type if (content_type =~ rule) == 0 - end - end - end - self[content_type_column] = value.to_s - end - - end - end -end diff --git a/lib/smithy/dragonfly/remote_data_store.rb b/lib/smithy/dragonfly/remote_data_store.rb deleted file mode 100644 index 6c898d9..0000000 --- a/lib/smithy/dragonfly/remote_data_store.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'httparty' -module Smithy - module Dragonfly - module DataStorage - class RemoteDataStore - def write(content, opts={}) - # raise "Sorry friend, this datastore is read-only." - end - - def read(uid) - response = HTTParty.get uid, :timeout => 3 - raise DataNotFound unless response.ok? - content = response.body - extra_data = {} - [ - content, # either a File, String or Tempfile - extra_data # Hash with optional keys :meta, :name, :format - ] - end - - def destroy(uid) - raise "Sorry friend, this datastore is read-only." - end - end - end - end -end diff --git a/lib/smithy/liquid/filters/resize.rb b/lib/smithy/liquid/filters/resize.rb index 129bda1..eac46ea 100644 --- a/lib/smithy/liquid/filters/resize.rb +++ b/lib/smithy/liquid/filters/resize.rb @@ -3,7 +3,7 @@ module Liquid module Filters module Resize def resize(input, resize_string) - Smithy::Dragonfly.resize_url(input, resize_string) + # Smithy::Dragonfly.resize_url(input, resize_string) end end ::Liquid::Template.register_filter(Resize) diff --git a/smithycms.gemspec b/smithycms.gemspec index 2560731..1d5be3b 100644 --- a/smithycms.gemspec +++ b/smithycms.gemspec @@ -24,9 +24,10 @@ Gem::Specification.new do |s| s.add_dependency 'autoprefixer-rails' # for bootstrap-sass s.add_dependency 'awesome_nested_set', '~> 3.0.1' + s.add_dependency 'aws-sdk', '< 2' # for refile s.add_dependency 'bootstrap-sass', '~> 3.2.0' - s.add_dependency 'dragonfly', '~> 1.0.7' - s.add_dependency 'dragonfly-s3_data_store', '~> 1.0.4' + # s.add_dependency 'dragonfly', '~> 1.0.7' + # s.add_dependency 'dragonfly-s3_data_store', '~> 1.0.4' s.add_dependency 'font-awesome-sass', '~> 4.2' s.add_dependency 'fog', '~> 1.20.0' s.add_dependency 'formtastic', '~> 2.3.1' # as of writing, formtastic-bootstrap is only compatible with 2.3.x @@ -38,13 +39,17 @@ Gem::Specification.new do |s| s.add_dependency 'kaminari', '~> 0.16.1' s.add_dependency 'liquid', '~> 2.6.1' s.add_dependency 'sass-rails' + s.add_dependency 'coffee-rails' s.add_dependency 'slodown' s.add_dependency 'rack-cache' + s.add_dependency 'mini_magick' + s.add_dependency 'refile' s.add_development_dependency 'byebug' s.add_development_dependency 'capybara' s.add_development_dependency 'coveralls' s.add_development_dependency 'database_cleaner' + s.add_development_dependency 'dotenv-rails' s.add_development_dependency 'factory_girl_rails' s.add_development_dependency 'fakeweb' s.add_development_dependency 'ffaker' diff --git a/spec/dummy/db/migrate/20150409174750_convert_assets_to_refile.rb b/spec/dummy/db/migrate/20150409174750_convert_assets_to_refile.rb new file mode 100644 index 0000000..2312185 --- /dev/null +++ b/spec/dummy/db/migrate/20150409174750_convert_assets_to_refile.rb @@ -0,0 +1,10 @@ +class ConvertAssetsToRefile < ActiveRecord::Migration + def change + # remove_column :smithy_assets, :uploaded_file_url + # remove_column :smithy_assets, :file_uid + # file_name + # file_size + # file_width + add_column :smithy_assets, :file_id, :string + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 63dacb3..e71e8fd 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141113220220) do +ActiveRecord::Schema.define(version: 20150409174750) do create_table "smithy_assets", force: true do |t| t.string "name" @@ -22,30 +22,31 @@ t.integer "file_size" t.integer "file_width" t.integer "file_height" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" + t.string "file_id" end create_table "smithy_content_block_templates", force: true do |t| t.integer "content_block_id" t.string "name" t.text "content" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end add_index "smithy_content_block_templates", ["content_block_id"], name: "index_smithy_content_block_templates_on_content_block_id" create_table "smithy_content_blocks", force: true do |t| t.string "name" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end create_table "smithy_contents", force: true do |t| t.text "content" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" t.text "markdown_content" end @@ -56,8 +57,8 @@ t.integer "height" t.string "image_scaling" t.string "link_url" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" t.string "html_attributes" t.text "content" end @@ -71,8 +72,8 @@ t.string "content_block_type" t.integer "content_block_id" t.integer "position" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" t.integer "content_block_template_id" t.boolean "publishable", default: false end @@ -90,8 +91,8 @@ t.boolean "include_children" t.integer "count" t.string "sort" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end add_index "smithy_page_lists", ["page_template_id"], name: "index_smithy_page_lists_on_page_template_id" @@ -112,8 +113,8 @@ t.integer "lft" t.integer "rgt" t.integer "depth" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" t.string "external_link" end @@ -126,15 +127,15 @@ create_table "smithy_settings", force: true do |t| t.string "name" t.text "value" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end create_table "smithy_template_containers", force: true do |t| t.integer "template_id" t.string "name" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" t.integer "position" end @@ -144,15 +145,15 @@ t.string "name" t.string "template_type", default: "template" t.text "content" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end create_table "smithy_users", force: true do |t| t.string "email" t.string "password_digest" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end create_table "users", force: true do |t|