redactor-rails-paperclip 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/.gitignore +19 -0
  2. data/Gemfile +24 -0
  3. data/MIT-LICENSE +22 -0
  4. data/README.md +106 -0
  5. data/Rakefile +3 -0
  6. data/app/controller/redactor_rails/base_controller.rb +27 -0
  7. data/app/controller/redactor_rails/documents_controller.rb +3 -0
  8. data/app/controller/redactor_rails/pictures_controller.rb +3 -0
  9. data/config/routes.rb +4 -0
  10. data/lib/generators/redactor/config_generator.rb +17 -0
  11. data/lib/generators/redactor/install_generator.rb +67 -0
  12. data/lib/generators/redactor/templates/active_record/carrierwave/migration.rb +25 -0
  13. data/lib/generators/redactor/templates/active_record/carrierwave/redactor/asset.rb +5 -0
  14. data/lib/generators/redactor/templates/active_record/carrierwave/redactor/document.rb +11 -0
  15. data/lib/generators/redactor/templates/active_record/carrierwave/redactor/picture.rb +7 -0
  16. data/lib/generators/redactor/templates/active_record/paperclip/migration.rb +26 -0
  17. data/lib/generators/redactor/templates/active_record/paperclip/redactor/asset.rb +4 -0
  18. data/lib/generators/redactor/templates/active_record/paperclip/redactor/document.rb +13 -0
  19. data/lib/generators/redactor/templates/active_record/paperclip/redactor/picture.rb +13 -0
  20. data/lib/generators/redactor/templates/base/carrierwave/uploaders/redactor_rails_document_uploader.rb +15 -0
  21. data/lib/generators/redactor/templates/base/carrierwave/uploaders/redactor_rails_picture_uploader.rb +53 -0
  22. data/lib/generators/redactor/templates/config.js +17 -0
  23. data/lib/generators/redactor/templates/mongoid/carrierwave/redactor/asset.rb +9 -0
  24. data/lib/generators/redactor/templates/mongoid/carrierwave/redactor/document.rb +14 -0
  25. data/lib/generators/redactor/templates/mongoid/carrierwave/redactor/picture.rb +7 -0
  26. data/lib/redactor-rails.rb +34 -0
  27. data/lib/redactor-rails/backend/carrierwave.rb +64 -0
  28. data/lib/redactor-rails/backend/paperclip.rb +56 -0
  29. data/lib/redactor-rails/engine.rb +10 -0
  30. data/lib/redactor-rails/helper.rb +19 -0
  31. data/lib/redactor-rails/http.rb +89 -0
  32. data/lib/redactor-rails/orm/active_record.rb +24 -0
  33. data/lib/redactor-rails/orm/base.rb +38 -0
  34. data/lib/redactor-rails/orm/mongoid.rb +37 -0
  35. data/lib/redactor-rails/version.rb +3 -0
  36. data/redactor-rails.gemspec +20 -0
  37. data/vendor/assets/javascripts/redactor-rails/config.js +15 -0
  38. data/vendor/assets/javascripts/redactor-rails/css/docstyle.css +139 -0
  39. data/vendor/assets/javascripts/redactor-rails/css/style.css +26 -0
  40. data/vendor/assets/javascripts/redactor-rails/css/wym.css +151 -0
  41. data/vendor/assets/javascripts/redactor-rails/index.js +2 -0
  42. data/vendor/assets/javascripts/redactor-rails/langs/ar.js +66 -0
  43. data/vendor/assets/javascripts/redactor-rails/langs/ba.js +69 -0
  44. data/vendor/assets/javascripts/redactor-rails/langs/bg.js +67 -0
  45. data/vendor/assets/javascripts/redactor-rails/langs/by.js +65 -0
  46. data/vendor/assets/javascripts/redactor-rails/langs/cs.js +77 -0
  47. data/vendor/assets/javascripts/redactor-rails/langs/da.js +66 -0
  48. data/vendor/assets/javascripts/redactor-rails/langs/de.js +70 -0
  49. data/vendor/assets/javascripts/redactor-rails/langs/en.js +66 -0
  50. data/vendor/assets/javascripts/redactor-rails/langs/eo.js +66 -0
  51. data/vendor/assets/javascripts/redactor-rails/langs/es.js +66 -0
  52. data/vendor/assets/javascripts/redactor-rails/langs/fa.js +66 -0
  53. data/vendor/assets/javascripts/redactor-rails/langs/fi.js +69 -0
  54. data/vendor/assets/javascripts/redactor-rails/langs/fr.js +76 -0
  55. data/vendor/assets/javascripts/redactor-rails/langs/hr.js +69 -0
  56. data/vendor/assets/javascripts/redactor-rails/langs/hu.js +67 -0
  57. data/vendor/assets/javascripts/redactor-rails/langs/id.js +66 -0
  58. data/vendor/assets/javascripts/redactor-rails/langs/it.js +68 -0
  59. data/vendor/assets/javascripts/redactor-rails/langs/ja.js +66 -0
  60. data/vendor/assets/javascripts/redactor-rails/langs/ko.js +66 -0
  61. data/vendor/assets/javascripts/redactor-rails/langs/lv.js +65 -0
  62. data/vendor/assets/javascripts/redactor-rails/langs/nl.js +69 -0
  63. data/vendor/assets/javascripts/redactor-rails/langs/pl.js +66 -0
  64. data/vendor/assets/javascripts/redactor-rails/langs/pt_br.js +72 -0
  65. data/vendor/assets/javascripts/redactor-rails/langs/ro.js +66 -0
  66. data/vendor/assets/javascripts/redactor-rails/langs/ru.js +66 -0
  67. data/vendor/assets/javascripts/redactor-rails/langs/sk.js +66 -0
  68. data/vendor/assets/javascripts/redactor-rails/langs/sq.js +69 -0
  69. data/vendor/assets/javascripts/redactor-rails/langs/sr-cir.js +69 -0
  70. data/vendor/assets/javascripts/redactor-rails/langs/sr-lat.js +69 -0
  71. data/vendor/assets/javascripts/redactor-rails/langs/sv.js +66 -0
  72. data/vendor/assets/javascripts/redactor-rails/langs/tr.js +66 -0
  73. data/vendor/assets/javascripts/redactor-rails/langs/ua.js +67 -0
  74. data/vendor/assets/javascripts/redactor-rails/langs/vi.js +65 -0
  75. data/vendor/assets/javascripts/redactor-rails/langs/zh_cn.js +70 -0
  76. data/vendor/assets/javascripts/redactor-rails/langs/zh_tw.js +66 -0
  77. data/vendor/assets/javascripts/redactor-rails/redactor.js +4210 -0
  78. data/vendor/assets/javascripts/redactor-rails/redactor.min.js +12 -0
  79. data/vendor/assets/stylesheets/redactor-rails/css/redactor.css +692 -0
  80. data/vendor/assets/stylesheets/redactor-rails/css/style.css +26 -0
  81. data/vendor/assets/stylesheets/redactor-rails/index.css +1 -0
  82. metadata +161 -0
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ class RedactorRailsPictureUploader < CarrierWave::Uploader::Base
3
+ include RedactorRails::Backend::CarrierWave
4
+
5
+ # Include RMagick or ImageScience support:
6
+ # include CarrierWave::RMagick
7
+ include CarrierWave::MiniMagick
8
+ # include CarrierWave::ImageScience
9
+
10
+ # Choose what kind of storage to use for this uploader:
11
+ storage :file
12
+
13
+ # Override the directory where uploaded files will be stored.
14
+ # This is a sensible default for uploaders that are meant to be mounted:
15
+ def store_dir
16
+ "system/redactor_assets/pictures/#{model.id}"
17
+ end
18
+
19
+ # Provide a default URL as a default if there hasn't been a file uploaded:
20
+ # def default_url
21
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
22
+ # end
23
+
24
+ # Process files as they are uploaded:
25
+ # process :scale => [200, 300]
26
+ #
27
+ # def scale(width, height)
28
+ # # do something
29
+ # end
30
+
31
+ process :read_dimensions
32
+
33
+ # Create different versions of your uploaded files:
34
+ version :thumb do
35
+ process resize_to_fill: [118, 100]
36
+ end
37
+
38
+ version :content do
39
+ process resize_to_limit: [800, 800]
40
+ end
41
+
42
+ # Add a white list of extensions which are allowed to be uploaded.
43
+ # For images you might use something like this:
44
+ def extension_white_list
45
+ RedactorRails.image_file_types
46
+ end
47
+
48
+ # Override the filename of the uploaded files:
49
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
50
+ # def filename
51
+ # "something.jpg" if original_filename
52
+ # end
53
+ end
@@ -0,0 +1,17 @@
1
+ $(document).ready(
2
+ function(){
3
+ var csrf_token = $('meta[name=csrf-token]').attr('content');
4
+ var csrf_param = $('meta[name=csrf-param]').attr('content');
5
+ var params;
6
+ if (csrf_param !== undefined && csrf_token !== undefined) {
7
+ params = csrf_param + "=" + encodeURIComponent(csrf_token);
8
+ }
9
+ $('.redactor').redactor(
10
+ { "imageUpload":"/redactor_rails/pictures?" + params,
11
+ "imageGetJson":"/redactor_rails/pictures",
12
+ "fileUpload":"/redactor_rails/documents?" + params,
13
+ "fileGetJson":"/redactor_rails/documents",
14
+ "path":"/assets/redactor-rails",
15
+ "css":"style.css"}
16
+ );
17
+ });
@@ -0,0 +1,9 @@
1
+ class RedactorRails::Asset
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ include RedactorRails::Orm::Mongoid::AssetBase
6
+
7
+ delegate :url, :current_path, :size, :content_type, :filename, to: :data
8
+ validates_presence_of :data
9
+ end
@@ -0,0 +1,14 @@
1
+ class RedactorRails::Document < RedactorRails::Asset
2
+ mount_uploader :data, RedactorRailsDocumentUploader, mount_on: :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+
8
+ def thumb
9
+ # Could theoretically provide an icon set here
10
+ # to match against the extensions
11
+ # but for now it's nil to address the bug
12
+ nil
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ class RedactorRails::Picture < RedactorRails::Asset
2
+ mount_uploader :data, RedactorRailsPictureUploader, mount_on: :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require "orm_adapter"
2
+ require "redactor-rails/version"
3
+ require 'pathname'
4
+
5
+ module RedactorRails
6
+ IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/pjpeg', 'image/tiff', 'image/x-png']
7
+
8
+ FILE_TYPES = ['application/msword', 'application/pdf', 'text/plain', 'text/rtf', 'application/vnd.ms-excel']
9
+
10
+ autoload :Http, 'redactor-rails/http'
11
+
12
+ module Backend
13
+ autoload :CarrierWave, 'redactor-rails/backend/carrierwave'
14
+ autoload :Paperclip, 'redactor-rails/backend/paperclip'
15
+ end
16
+ require 'redactor-rails/orm/base'
17
+ require 'redactor-rails/orm/active_record'
18
+ require 'redactor-rails/orm/mongoid'
19
+ require 'redactor-rails/engine'
20
+ require 'redactor-rails/helper'
21
+
22
+ mattr_accessor :image_file_types, :document_file_types
23
+ @@image_file_types = ["jpg", "jpeg", "png", "gif", "tiff"]
24
+ @@document_file_types = ["pdf", "doc", "docx", "xls", "xlsx", "rtf", "txt"]
25
+
26
+ # Ckeditor files destination path
27
+ mattr_accessor :relative_path
28
+ @@relative_path = '/assets/redactor_rails'
29
+
30
+ def self.root_path
31
+ @root_path ||= Pathname.new(File.dirname(File.expand_path('../', __FILE__)))
32
+ end
33
+
34
+ end
@@ -0,0 +1,64 @@
1
+ require 'mime/types'
2
+ require 'mini_magick'
3
+
4
+ module RedactorRails
5
+ module Backend
6
+ module CarrierWave
7
+ def self.included(base)
8
+ base.send(:include, InstanceMethods)
9
+ base.send(:extend, ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def self.extended(base)
14
+ base.class_eval do
15
+ process :extract_content_type
16
+ process :set_size
17
+ end
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ # process :strip
23
+ def strip
24
+ manipulate! do |img|
25
+ img.strip
26
+ img = yield(img) if block_given?
27
+ img
28
+ end
29
+ end
30
+
31
+ # process :quality => 85
32
+ def quality(percentage)
33
+ manipulate! do |img|
34
+ img.quality(percentage)
35
+ img = yield(img) if block_given?
36
+ img
37
+ end
38
+ end
39
+
40
+ def extract_content_type
41
+ if file.content_type == 'application/octet-stream' || file.content_type.blank?
42
+ content_type = MIME::Types.type_for(original_filename).first
43
+ else
44
+ content_type = file.content_type
45
+ end
46
+
47
+ model.data_content_type = content_type.to_s
48
+ end
49
+
50
+ def set_size
51
+ model.data_file_size = file.size
52
+ end
53
+
54
+ def read_dimensions
55
+ if model.image? && model.has_dimensions?
56
+ magick = ::MiniMagick::Image.new(current_path)
57
+ model.width, model.height = magick[:width], magick[:height]
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,56 @@
1
+ require 'mime/types'
2
+
3
+ module RedactorRails
4
+ module Backend
5
+ module Paperclip
6
+ def self.included(base)
7
+ base.send(:include, InstanceMethods)
8
+ base.send(:extend, ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def self.extended(base)
13
+ base.class_eval do
14
+ before_validation :extract_content_type
15
+ before_create :read_dimensions, :parameterize_filename
16
+
17
+ delegate :url, :path, :styles, :size, :content_type, to: :data
18
+ end
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+ def geometry
24
+ @geometry ||= begin
25
+ file = data.respond_to?(:queued_for_write) ? data.queued_for_write[:original] : data.to_file
26
+ ::Paperclip::Geometry.from_file(file)
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ def parameterize_filename
33
+ unless data_file_name.blank?
34
+ filename = RedactorRails::Helpers.parameterize_filename(data_file_name)
35
+ self.data.instance_write(:file_name, filename)
36
+ end
37
+ end
38
+
39
+ def read_dimensions
40
+ if image? && has_dimensions?
41
+ self.width = geometry.width
42
+ self.height = geometry.height
43
+ end
44
+ end
45
+
46
+ # Extract content_type from filename using mime/types gem
47
+ def extract_content_type
48
+ if data_content_type == "application/octet-stream" && !data_file_name.blank?
49
+ content_types = MIME::Types.type_for(data_file_name)
50
+ self.data_content_type = content_types.first.to_s unless content_types.empty?
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,10 @@
1
+ module RedactorRails
2
+ class Engine < Rails::Engine
3
+ isolate_namespace RedactorRails
4
+ initializer "helper" do |app|
5
+ ActiveSupport.on_load(:action_view) do
6
+ include RedactorRails::Helpers
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module RedactorRails
2
+ module Helpers
3
+ # Setting Redactor Language
4
+ def redactor_lang(lang = 'en')
5
+ javascript_include_tag "redactor-rails/langs/#{lang}.js"
6
+ end
7
+
8
+ class << self
9
+ def parameterize_filename(filename)
10
+ # return filename unless Ckeditor.parameterize_filenames
11
+
12
+ extension = File.extname(filename)
13
+ basename = filename.gsub(/#{extension}$/, "")
14
+
15
+ [basename.parameterize('_'), extension].join.downcase
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ require 'digest/sha1'
3
+ require 'mime/types'
4
+
5
+ module RedactorRails
6
+ module Http
7
+ # Create tempfile from hash
8
+ class UploadedFile
9
+ attr_accessor :original_filename, :content_type, :tempfile, :headers
10
+
11
+ def initialize(hash)
12
+ @original_filename = hash[:filename]
13
+ @content_type = hash[:type]
14
+ @headers = hash[:head]
15
+ @tempfile = hash[:tempfile]
16
+ raise(ArgumentError, ':tempfile is required') unless @tempfile
17
+ end
18
+
19
+ def open
20
+ @tempfile.open
21
+ end
22
+
23
+ def path
24
+ @tempfile.path
25
+ end
26
+
27
+ def read(*args)
28
+ @tempfile.read(*args)
29
+ end
30
+
31
+ def rewind
32
+ @tempfile.rewind
33
+ end
34
+
35
+ def size
36
+ @tempfile.size
37
+ end
38
+ end
39
+
40
+ # Usage (paperclip example)
41
+ # @asset.data = QqFile.new(params[:qqfile], request)
42
+ class QqFile < ::Tempfile
43
+
44
+ def initialize(filename, request, tmpdir = Dir::tmpdir)
45
+ @original_filename = filename
46
+ @request = request
47
+
48
+ super Digest::SHA1.hexdigest(filename), tmpdir
49
+ fetch
50
+ end
51
+
52
+ def fetch
53
+ self.write(body)
54
+ self.rewind
55
+ self
56
+ end
57
+
58
+ def original_filename
59
+ @original_filename
60
+ end
61
+
62
+ def content_type
63
+ types = MIME::Types.type_for(original_filename)
64
+ types.empty? ? @request.content_type : types.first.to_s
65
+ end
66
+
67
+ def body
68
+ if @request.raw_post.respond_to?(:force_encoding)
69
+ @request.raw_post.force_encoding("UTF-8")
70
+ else
71
+ @request.raw_post
72
+ end
73
+ end
74
+ end
75
+
76
+ # Convert nested Hash to HashWithIndifferentAccess and replace
77
+ # file upload hash with UploadedFile objects
78
+ def self.normalize_param(*args)
79
+ value = args.first
80
+ if Hash === value && value.has_key?(:tempfile)
81
+ UploadedFile.new(value)
82
+ elsif value.is_a?(String)
83
+ QqFile.new(*args)
84
+ else
85
+ value
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,24 @@
1
+ require 'redactor-rails/orm/base'
2
+
3
+ module RedactorRails
4
+ module Orm
5
+ module ActiveRecord
6
+ module AssetBase
7
+ def self.included(base)
8
+ base.send(:include, Base::AssetBase::InstanceMethods)
9
+ base.send(:extend, ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def self.extended(base)
14
+ base.class_eval do
15
+ self.table_name = "redactor_assets"
16
+
17
+ belongs_to :assetable, polymorphic: true
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module RedactorRails
2
+ module Orm
3
+ module Base
4
+ module AssetBase
5
+ module InstanceMethods
6
+ def has_dimensions?
7
+ respond_to?(:width) && respond_to?(:height)
8
+ end
9
+
10
+ def image?
11
+ RedactorRails::IMAGE_TYPES.include?(data_content_type)
12
+ end
13
+
14
+ def image
15
+ url
16
+ end
17
+
18
+ def thumb
19
+ url(:thumb)
20
+ end
21
+
22
+ def as_json_methods
23
+ [:image, :thumb]
24
+ end
25
+
26
+ def as_json(options = nil)
27
+ options = {
28
+ methods: as_json_methods,
29
+ root: false
30
+ }
31
+
32
+ super options
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end