samlown-carrierwave 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Generators +4 -0
- data/History.txt +125 -0
- data/Manifest.txt +110 -0
- data/README.rdoc +524 -0
- data/Rakefile +39 -0
- data/carrierwave.gemspec +85 -0
- data/cucumber.yml +2 -0
- data/features/caching.feature +28 -0
- data/features/download.feature +20 -0
- data/features/file_storage.feature +37 -0
- data/features/file_storage_overridden_filename.feature +38 -0
- data/features/file_storage_overridden_store_dir.feature +38 -0
- data/features/file_storage_reversing_processor.feature +43 -0
- data/features/fixtures/bork.txt +1 -0
- data/features/fixtures/monkey.txt +1 -0
- data/features/grid_fs_storage.feature +32 -0
- data/features/mount_activerecord.feature +46 -0
- data/features/mount_datamapper.feature +46 -0
- data/features/step_definitions/activerecord_steps.rb +22 -0
- data/features/step_definitions/caching_steps.rb +14 -0
- data/features/step_definitions/datamapper_steps.rb +29 -0
- data/features/step_definitions/download_steps.rb +4 -0
- data/features/step_definitions/file_steps.rb +53 -0
- data/features/step_definitions/general_steps.rb +85 -0
- data/features/step_definitions/mount_steps.rb +19 -0
- data/features/step_definitions/store_steps.rb +18 -0
- data/features/support/activerecord.rb +30 -0
- data/features/support/datamapper.rb +7 -0
- data/features/support/env.rb +22 -0
- data/features/versions_basics.feature +50 -0
- data/features/versions_nested_versions.feature +70 -0
- data/features/versions_overridden_filename.feature +51 -0
- data/features/versions_overriden_store_dir.feature +41 -0
- data/lib/carrierwave.rb +98 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/core_ext/blank.rb +46 -0
- data/lib/carrierwave/core_ext/file.rb +11 -0
- data/lib/carrierwave/core_ext/inheritable_attributes.rb +108 -0
- data/lib/carrierwave/core_ext/module_setup.rb +51 -0
- data/lib/carrierwave/mount.rb +359 -0
- data/lib/carrierwave/orm/activerecord.rb +73 -0
- data/lib/carrierwave/orm/datamapper.rb +27 -0
- data/lib/carrierwave/orm/mongoid.rb +23 -0
- data/lib/carrierwave/orm/mongomapper.rb +27 -0
- data/lib/carrierwave/orm/sequel.rb +45 -0
- data/lib/carrierwave/processing/image_science.rb +101 -0
- data/lib/carrierwave/processing/mini_magick.rb +265 -0
- data/lib/carrierwave/processing/rmagick.rb +282 -0
- data/lib/carrierwave/sanitized_file.rb +273 -0
- data/lib/carrierwave/storage/abstract.rb +30 -0
- data/lib/carrierwave/storage/cloud_files.rb +169 -0
- data/lib/carrierwave/storage/file.rb +48 -0
- data/lib/carrierwave/storage/grid_fs.rb +97 -0
- data/lib/carrierwave/storage/right_s3.rb +3 -0
- data/lib/carrierwave/storage/s3.rb +206 -0
- data/lib/carrierwave/test/matchers.rb +128 -0
- data/lib/carrierwave/uploader.rb +44 -0
- data/lib/carrierwave/uploader/cache.rb +145 -0
- data/lib/carrierwave/uploader/callbacks.rb +42 -0
- data/lib/carrierwave/uploader/configuration.rb +132 -0
- data/lib/carrierwave/uploader/default_url.rb +19 -0
- data/lib/carrierwave/uploader/download.rb +59 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/processing.rb +83 -0
- data/lib/carrierwave/uploader/proxy.rb +62 -0
- data/lib/carrierwave/uploader/remove.rb +22 -0
- data/lib/carrierwave/uploader/store.rb +89 -0
- data/lib/carrierwave/uploader/url.rb +33 -0
- data/lib/carrierwave/uploader/versions.rb +146 -0
- data/merb_generators/uploader_generator.rb +22 -0
- data/rails_generators/uploader/USAGE +2 -0
- data/rails_generators/uploader/templates/uploader.rb +47 -0
- data/rails_generators/uploader/uploader_generator.rb +21 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/compatibility/paperclip_spec.rb +52 -0
- data/spec/fixtures/bork.txt +1 -0
- data/spec/fixtures/landscape.jpg +0 -0
- data/spec/fixtures/portrait.jpg +0 -0
- data/spec/fixtures/test.jpeg +1 -0
- data/spec/fixtures/test.jpg +1 -0
- data/spec/mount_spec.rb +538 -0
- data/spec/orm/activerecord_spec.rb +271 -0
- data/spec/orm/datamapper_spec.rb +168 -0
- data/spec/orm/mongoid_spec.rb +202 -0
- data/spec/orm/mongomapper_spec.rb +202 -0
- data/spec/orm/sequel_spec.rb +183 -0
- data/spec/processing/image_science_spec.rb +56 -0
- data/spec/processing/mini_magick_spec.rb +76 -0
- data/spec/processing/rmagick_spec.rb +75 -0
- data/spec/sanitized_file_spec.rb +623 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/storage/cloudfiles_spec.rb +78 -0
- data/spec/storage/grid_fs_spec.rb +83 -0
- data/spec/storage/s3_spec.rb +118 -0
- data/spec/uploader/cache_spec.rb +209 -0
- data/spec/uploader/configuration_spec.rb +105 -0
- data/spec/uploader/default_url_spec.rb +85 -0
- data/spec/uploader/download_spec.rb +75 -0
- data/spec/uploader/extension_whitelist_spec.rb +44 -0
- data/spec/uploader/mountable_spec.rb +33 -0
- data/spec/uploader/paths_spec.rb +22 -0
- data/spec/uploader/processing_spec.rb +73 -0
- data/spec/uploader/proxy_spec.rb +54 -0
- data/spec/uploader/remove_spec.rb +70 -0
- data/spec/uploader/store_spec.rb +264 -0
- data/spec/uploader/url_spec.rb +102 -0
- data/spec/uploader/versions_spec.rb +298 -0
- metadata +433 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module DefaultUrl
|
6
|
+
|
7
|
+
def url(*args)
|
8
|
+
super || default_url
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# Override this method in your uploader to provide a default url
|
13
|
+
# in case no file has been cached/stored yet.
|
14
|
+
#
|
15
|
+
def default_url; end
|
16
|
+
|
17
|
+
end # DefaultPath
|
18
|
+
end # Uploader
|
19
|
+
end # CarrierWave
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module CarrierWave
|
6
|
+
module Uploader
|
7
|
+
module Download
|
8
|
+
|
9
|
+
depends_on CarrierWave::Uploader::Callbacks
|
10
|
+
depends_on CarrierWave::Uploader::Configuration
|
11
|
+
depends_on CarrierWave::Uploader::Cache
|
12
|
+
|
13
|
+
class RemoteFile
|
14
|
+
def initialize(uri)
|
15
|
+
@uri = URI.parse(uri)
|
16
|
+
end
|
17
|
+
|
18
|
+
def original_filename
|
19
|
+
File.basename(@uri.path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def respond_to?(*args)
|
23
|
+
super or file.respond_to?(*args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def http?
|
27
|
+
@uri.scheme =~ /^https?$/
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def file
|
33
|
+
@file ||= StringIO.new(Net::HTTP.get_response(@uri).body)
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_missing(*args, &block)
|
37
|
+
file.send(*args, &block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Caches the file by downloading it from the given URL.
|
43
|
+
#
|
44
|
+
# === Parameters
|
45
|
+
#
|
46
|
+
# [url (String)] The URL where the remote file is stored
|
47
|
+
#
|
48
|
+
def download!(uri)
|
49
|
+
unless uri.blank?
|
50
|
+
file = RemoteFile.new(uri)
|
51
|
+
raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
|
52
|
+
cache!(file)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end # Download
|
57
|
+
end # Uploader
|
58
|
+
end # CarrierWave
|
59
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module ExtensionWhitelist
|
6
|
+
|
7
|
+
setup do
|
8
|
+
before :cache, :check_whitelist!
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# Override this method in your uploader to provide a white list of extensions which
|
13
|
+
# are allowed to be uploaded.
|
14
|
+
#
|
15
|
+
# === Returns
|
16
|
+
#
|
17
|
+
# [NilClass, Array[String]] a white list of extensions which are allowed to be uploaded
|
18
|
+
#
|
19
|
+
# === Examples
|
20
|
+
#
|
21
|
+
# def extension_white_list
|
22
|
+
# %w(jpg jpeg gif png)
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
def extension_white_list; end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def check_whitelist!(new_file)
|
30
|
+
if extension_white_list and not extension_white_list.include?(new_file.extension.to_s)
|
31
|
+
raise CarrierWave::IntegrityError, "You are not allowed to upload #{new_file.extension.inspect} files, allowed types: #{extension_white_list.inspect}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end # ExtensionWhitelist
|
36
|
+
end # Uploader
|
37
|
+
end # CarrierWave
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Mountable
|
6
|
+
|
7
|
+
attr_reader :model, :mounted_as
|
8
|
+
|
9
|
+
##
|
10
|
+
# If a model is given as the first parameter, it will stored in the uploader, and
|
11
|
+
# available throught +#model+. Likewise, mounted_as stores the name of the column
|
12
|
+
# where this instance of the uploader is mounted. These values can then be used inside
|
13
|
+
# your uploader.
|
14
|
+
#
|
15
|
+
# If you do not wish to mount your uploaders with the ORM extensions in -more then you
|
16
|
+
# can override this method inside your uploader. Just be sure to call +super+
|
17
|
+
#
|
18
|
+
# === Parameters
|
19
|
+
#
|
20
|
+
# [model (Object)] Any kind of model object
|
21
|
+
# [mounted_as (Symbol)] The name of the column where this uploader is mounted
|
22
|
+
#
|
23
|
+
# === Examples
|
24
|
+
#
|
25
|
+
# class MyUploader < CarrierWave::Uploader::Base
|
26
|
+
#
|
27
|
+
# def store_dir
|
28
|
+
# File.join('public', 'files', mounted_as, model.permalink)
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
def initialize(model=nil, mounted_as=nil)
|
33
|
+
@model = model
|
34
|
+
@mounted_as = mounted_as
|
35
|
+
end
|
36
|
+
|
37
|
+
end # Mountable
|
38
|
+
end # Uploader
|
39
|
+
end # CarrierWave
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Processing
|
6
|
+
|
7
|
+
depends_on CarrierWave::Uploader::Callbacks
|
8
|
+
|
9
|
+
setup do
|
10
|
+
after :cache, :process!
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
##
|
16
|
+
# Lists processor callbacks declared
|
17
|
+
#
|
18
|
+
# === Returns
|
19
|
+
#
|
20
|
+
# [Array[Array[Symbol, Array]]] a list of processor callbacks which have been declared for this uploader
|
21
|
+
#
|
22
|
+
def processors
|
23
|
+
@processors ||= []
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Adds a processor callback which applies operations as a file is uploaded.
|
28
|
+
# The argument may be the name of any method of the uploader, expressed as a symbol,
|
29
|
+
# or a list of such methods, or a hash where the key is a method and the value is
|
30
|
+
# an array of arguments to call the method with
|
31
|
+
#
|
32
|
+
# === Parameters
|
33
|
+
#
|
34
|
+
# args (*Symbol, Hash{Symbol => Array[]})
|
35
|
+
#
|
36
|
+
# === Examples
|
37
|
+
#
|
38
|
+
# class MyUploader < CarrierWave::Uploader::Base
|
39
|
+
#
|
40
|
+
# process :sepiatone, :vignette
|
41
|
+
# process :scale => [200, 200]
|
42
|
+
#
|
43
|
+
# def sepiatone
|
44
|
+
# ...
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# def vignette
|
48
|
+
# ...
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# def scale(height, width)
|
52
|
+
# ...
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
def process(*args)
|
57
|
+
args.each do |arg|
|
58
|
+
if arg.is_a?(Hash)
|
59
|
+
arg.each do |method, args|
|
60
|
+
processors.push([method, args])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
processors.push([arg, []])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end # ClassMethods
|
69
|
+
|
70
|
+
##
|
71
|
+
# Apply all process callbacks added through CarrierWave.process
|
72
|
+
#
|
73
|
+
def process!(new_file=nil)
|
74
|
+
if enable_processing
|
75
|
+
self.class.processors.each do |method, args|
|
76
|
+
self.send(method, *args)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end # Processing
|
82
|
+
end # Uploader
|
83
|
+
end # CarrierWave
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Proxy
|
6
|
+
|
7
|
+
##
|
8
|
+
# === Returns
|
9
|
+
#
|
10
|
+
# [Boolean] Whether the uploaded file is blank
|
11
|
+
#
|
12
|
+
def blank?
|
13
|
+
file.blank?
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# === Returns
|
18
|
+
#
|
19
|
+
# [String] the path where the file is currently located.
|
20
|
+
#
|
21
|
+
def current_path
|
22
|
+
file.path if file.respond_to?(:path)
|
23
|
+
end
|
24
|
+
|
25
|
+
alias_method :path, :current_path
|
26
|
+
|
27
|
+
##
|
28
|
+
# Returns a string that uniquely identifies the last stored file
|
29
|
+
#
|
30
|
+
# === Returns
|
31
|
+
#
|
32
|
+
# [String] uniquely identifies a file
|
33
|
+
#
|
34
|
+
def identifier
|
35
|
+
storage.identifier if storage.respond_to?(:identifier)
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Read the contents of the file
|
40
|
+
#
|
41
|
+
# === Returns
|
42
|
+
#
|
43
|
+
# [String] contents of the file
|
44
|
+
#
|
45
|
+
def read
|
46
|
+
file.read if file.respond_to?(:read)
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Fetches the size of the currently stored/cached file
|
51
|
+
#
|
52
|
+
# === Returns
|
53
|
+
#
|
54
|
+
# [Integer] size of the file
|
55
|
+
#
|
56
|
+
def size
|
57
|
+
file.respond_to?(:size) ? file.size : 0
|
58
|
+
end
|
59
|
+
|
60
|
+
end # Proxy
|
61
|
+
end # Uploader
|
62
|
+
end # CarrierWave
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Remove
|
6
|
+
|
7
|
+
depends_on CarrierWave::Uploader::Callbacks
|
8
|
+
|
9
|
+
##
|
10
|
+
# Removes the file and reset it
|
11
|
+
#
|
12
|
+
def remove!
|
13
|
+
with_callbacks(:remove) do
|
14
|
+
@file.delete if @file
|
15
|
+
@file = nil
|
16
|
+
@cache_id = nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end # Remove
|
21
|
+
end # Uploader
|
22
|
+
end # CarrierWave
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Store
|
6
|
+
|
7
|
+
depends_on CarrierWave::Uploader::Callbacks
|
8
|
+
depends_on CarrierWave::Uploader::Configuration
|
9
|
+
depends_on CarrierWave::Uploader::Cache
|
10
|
+
|
11
|
+
##
|
12
|
+
# Override this in your Uploader to change the filename.
|
13
|
+
#
|
14
|
+
# Be careful using record ids as filenames. If the filename is stored in the database
|
15
|
+
# the record id will be nil when the filename is set. Don't use record ids unless you
|
16
|
+
# understand this limitation.
|
17
|
+
#
|
18
|
+
# Do not use the version_name in the filename, as it will prevent versions from being
|
19
|
+
# loaded correctly.
|
20
|
+
#
|
21
|
+
# === Returns
|
22
|
+
#
|
23
|
+
# [String] a filename
|
24
|
+
#
|
25
|
+
def filename
|
26
|
+
@filename
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Calculates the path where the file should be stored. If +for_file+ is given, it will be
|
31
|
+
# used as the filename, otherwise +CarrierWave::Uploader#filename+ is assumed.
|
32
|
+
#
|
33
|
+
# === Parameters
|
34
|
+
#
|
35
|
+
# [for_file (String)] name of the file <optional>
|
36
|
+
#
|
37
|
+
# === Returns
|
38
|
+
#
|
39
|
+
# [String] the store path
|
40
|
+
#
|
41
|
+
def store_path(for_file=filename)
|
42
|
+
File.join([store_dir, full_filename(for_file)].compact)
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Stores the file by passing it to this Uploader's storage engine.
|
47
|
+
#
|
48
|
+
# If new_file is omitted, a previously cached file will be stored.
|
49
|
+
#
|
50
|
+
# === Parameters
|
51
|
+
#
|
52
|
+
# [new_file (File, IOString, Tempfile)] any kind of file object
|
53
|
+
#
|
54
|
+
def store!(new_file=nil)
|
55
|
+
cache!(new_file) if new_file
|
56
|
+
if @file and @cache_id
|
57
|
+
with_callbacks(:store, new_file) do
|
58
|
+
@file = storage.store!(@file)
|
59
|
+
@cache_id = nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Retrieves the file from the storage.
|
66
|
+
#
|
67
|
+
# === Parameters
|
68
|
+
#
|
69
|
+
# [identifier (String)] uniquely identifies the file to retrieve
|
70
|
+
#
|
71
|
+
def retrieve_from_store!(identifier)
|
72
|
+
with_callbacks(:retrieve_from_store, identifier) do
|
73
|
+
@file = storage.retrieve!(identifier)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def full_filename(for_file)
|
80
|
+
for_file
|
81
|
+
end
|
82
|
+
|
83
|
+
def storage
|
84
|
+
@storage ||= self.class.storage.new(self)
|
85
|
+
end
|
86
|
+
|
87
|
+
end # Store
|
88
|
+
end # Uploader
|
89
|
+
end # CarrierWave
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Url
|
6
|
+
|
7
|
+
##
|
8
|
+
# === Returns
|
9
|
+
#
|
10
|
+
# [String] the location where this file is accessible via a url
|
11
|
+
#
|
12
|
+
def url
|
13
|
+
if file.respond_to?(:url) and not file.url.blank?
|
14
|
+
file.url
|
15
|
+
elsif current_path
|
16
|
+
File.expand_path(current_path).gsub(File.expand_path(root), '')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
alias_method :to_s, :url
|
21
|
+
|
22
|
+
##
|
23
|
+
# === Returns
|
24
|
+
#
|
25
|
+
# [String] A JSON serializtion containing this uploader's URL
|
26
|
+
#
|
27
|
+
def to_json(*args)
|
28
|
+
{ 'url' => url }.to_json(*args)
|
29
|
+
end
|
30
|
+
|
31
|
+
end # Url
|
32
|
+
end # Uploader
|
33
|
+
end # CarrierWave
|