locomotive_carrierwave 0.5.0.1.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +532 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/locale/en.yml +5 -0
- data/lib/carrierwave/mount.rb +376 -0
- data/lib/carrierwave/orm/activerecord.rb +36 -0
- data/lib/carrierwave/orm/datamapper.rb +37 -0
- data/lib/carrierwave/orm/mongoid.rb +36 -0
- data/lib/carrierwave/orm/sequel.rb +45 -0
- data/lib/carrierwave/processing/image_science.rb +116 -0
- data/lib/carrierwave/processing/mini_magick.rb +261 -0
- data/lib/carrierwave/processing/rmagick.rb +278 -0
- data/lib/carrierwave/sanitized_file.rb +306 -0
- data/lib/carrierwave/storage/abstract.rb +33 -0
- data/lib/carrierwave/storage/cloud_files.rb +168 -0
- data/lib/carrierwave/storage/file.rb +54 -0
- data/lib/carrierwave/storage/grid_fs.rb +136 -0
- data/lib/carrierwave/storage/right_s3.rb +1 -0
- data/lib/carrierwave/storage/s3.rb +249 -0
- data/lib/carrierwave/test/matchers.rb +164 -0
- data/lib/carrierwave/uploader/cache.rb +148 -0
- data/lib/carrierwave/uploader/callbacks.rb +41 -0
- data/lib/carrierwave/uploader/configuration.rb +134 -0
- data/lib/carrierwave/uploader/default_url.rb +19 -0
- data/lib/carrierwave/uploader/download.rb +64 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/processing.rb +85 -0
- data/lib/carrierwave/uploader/proxy.rb +62 -0
- data/lib/carrierwave/uploader/remove.rb +23 -0
- data/lib/carrierwave/uploader/rename.rb +62 -0
- data/lib/carrierwave/uploader/store.rb +98 -0
- data/lib/carrierwave/uploader/url.rb +33 -0
- data/lib/carrierwave/uploader/versions.rb +157 -0
- data/lib/carrierwave/uploader.rb +45 -0
- data/lib/carrierwave/validations/active_model.rb +79 -0
- data/lib/carrierwave/version.rb +3 -0
- data/lib/carrierwave.rb +101 -0
- data/lib/generators/templates/uploader.rb +47 -0
- data/lib/generators/uploader_generator.rb +7 -0
- metadata +390 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
|
3
|
+
module Uploader
|
4
|
+
module Configuration
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
add_config :root
|
9
|
+
add_config :permissions
|
10
|
+
add_config :storage_engines
|
11
|
+
add_config :s3_access_policy
|
12
|
+
add_config :s3_bucket
|
13
|
+
add_config :s3_access_key_id
|
14
|
+
add_config :s3_secret_access_key
|
15
|
+
add_config :s3_cname
|
16
|
+
add_config :s3_headers
|
17
|
+
add_config :cloud_files_username
|
18
|
+
add_config :cloud_files_api_key
|
19
|
+
add_config :cloud_files_container
|
20
|
+
add_config :cloud_files_cdn_host
|
21
|
+
add_config :grid_fs_connection
|
22
|
+
add_config :grid_fs_database
|
23
|
+
add_config :grid_fs_host
|
24
|
+
add_config :grid_fs_port
|
25
|
+
add_config :grid_fs_username
|
26
|
+
add_config :grid_fs_password
|
27
|
+
add_config :grid_fs_access_url
|
28
|
+
add_config :store_dir
|
29
|
+
add_config :cache_dir
|
30
|
+
add_config :enable_processing
|
31
|
+
add_config :ensure_multipart_form
|
32
|
+
|
33
|
+
# Mounting
|
34
|
+
add_config :ignore_integrity_errors
|
35
|
+
add_config :ignore_processing_errors
|
36
|
+
add_config :validate_integrity
|
37
|
+
add_config :validate_processing
|
38
|
+
add_config :mount_on
|
39
|
+
|
40
|
+
configure do |config|
|
41
|
+
config.permissions = 0644
|
42
|
+
config.storage_engines = {
|
43
|
+
:file => "CarrierWave::Storage::File",
|
44
|
+
:s3 => "CarrierWave::Storage::S3",
|
45
|
+
:grid_fs => "CarrierWave::Storage::GridFS",
|
46
|
+
:right_s3 => "CarrierWave::Storage::RightS3",
|
47
|
+
:cloud_files => "CarrierWave::Storage::CloudFiles"
|
48
|
+
}
|
49
|
+
config.storage = :file
|
50
|
+
config.s3_headers = {}
|
51
|
+
config.s3_access_policy = :public_read
|
52
|
+
config.grid_fs_database = 'carrierwave'
|
53
|
+
config.grid_fs_host = 'localhost'
|
54
|
+
config.grid_fs_port = 27017
|
55
|
+
config.store_dir = 'uploads'
|
56
|
+
config.cache_dir = 'uploads/tmp'
|
57
|
+
config.ignore_integrity_errors = true
|
58
|
+
config.ignore_processing_errors = true
|
59
|
+
config.validate_integrity = true
|
60
|
+
config.validate_processing = true
|
61
|
+
config.root = CarrierWave.root
|
62
|
+
config.enable_processing = true
|
63
|
+
config.ensure_multipart_form = true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
module ClassMethods
|
68
|
+
|
69
|
+
##
|
70
|
+
# Sets the storage engine to be used when storing files with this uploader.
|
71
|
+
# Can be any class that implements a #store!(CarrierWave::SanitizedFile) and a #retrieve!
|
72
|
+
# method. See lib/carrierwave/storage/file.rb for an example. Storage engines should
|
73
|
+
# be added to CarrierWave::Uploader::Base.storage_engines so they can be referred
|
74
|
+
# to by a symbol, which should be more convenient
|
75
|
+
#
|
76
|
+
# If no argument is given, it will simply return the currently used storage engine.
|
77
|
+
#
|
78
|
+
# === Parameters
|
79
|
+
#
|
80
|
+
# [storage (Symbol, Class)] The storage engine to use for this uploader
|
81
|
+
#
|
82
|
+
# === Returns
|
83
|
+
#
|
84
|
+
# [Class] the storage engine to be used with this uploader
|
85
|
+
#
|
86
|
+
# === Examples
|
87
|
+
#
|
88
|
+
# storage :file
|
89
|
+
# storage CarrierWave::Storage::File
|
90
|
+
# storage MyCustomStorageEngine
|
91
|
+
#
|
92
|
+
def storage(storage = nil)
|
93
|
+
if storage.is_a?(Symbol)
|
94
|
+
@storage = eval(storage_engines[storage])
|
95
|
+
elsif storage
|
96
|
+
@storage = storage
|
97
|
+
elsif @storage.nil?
|
98
|
+
# Get the storage from the superclass if there is one
|
99
|
+
@storage = superclass.storage rescue nil
|
100
|
+
end
|
101
|
+
return @storage
|
102
|
+
end
|
103
|
+
alias_method :storage=, :storage
|
104
|
+
|
105
|
+
|
106
|
+
def add_config(name)
|
107
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
108
|
+
def self.#{name}(value=nil)
|
109
|
+
@#{name} = value if value
|
110
|
+
return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
|
111
|
+
name = superclass.#{name}
|
112
|
+
return nil if name.nil? && !instance_variable_defined?("@#{name}")
|
113
|
+
@#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.#{name}=(value)
|
117
|
+
@#{name} = value
|
118
|
+
end
|
119
|
+
|
120
|
+
def #{name}
|
121
|
+
self.class.#{name}
|
122
|
+
end
|
123
|
+
RUBY
|
124
|
+
end
|
125
|
+
|
126
|
+
def configure
|
127
|
+
yield self
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
@@ -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,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
module CarrierWave
|
6
|
+
module Uploader
|
7
|
+
module Download
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
include CarrierWave::Uploader::Callbacks
|
11
|
+
include CarrierWave::Uploader::Configuration
|
12
|
+
include CarrierWave::Uploader::Cache
|
13
|
+
|
14
|
+
class RemoteFile
|
15
|
+
def initialize(uri)
|
16
|
+
@uri = URI.parse(URI.escape(uri))
|
17
|
+
end
|
18
|
+
|
19
|
+
def original_filename
|
20
|
+
File.basename(@uri.path)
|
21
|
+
end
|
22
|
+
|
23
|
+
def respond_to?(*args)
|
24
|
+
super or file.respond_to?(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
def http?
|
28
|
+
@uri.scheme =~ /^https?$/
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def file
|
34
|
+
if @file.blank?
|
35
|
+
@file = Kernel.open(@uri.to_s)
|
36
|
+
@file = @file.is_a?(String) ? StringIO.new(@file) : @file
|
37
|
+
end
|
38
|
+
@file
|
39
|
+
end
|
40
|
+
|
41
|
+
def method_missing(*args, &block)
|
42
|
+
file.send(*args, &block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Caches the file by downloading it from the given URL.
|
48
|
+
#
|
49
|
+
# === Parameters
|
50
|
+
#
|
51
|
+
# [url (String)] The URL where the remote file is stored
|
52
|
+
#
|
53
|
+
def download!(uri)
|
54
|
+
unless uri.blank?
|
55
|
+
file = RemoteFile.new(uri)
|
56
|
+
raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
|
57
|
+
cache!(file)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end # Download
|
62
|
+
end # Uploader
|
63
|
+
end # CarrierWave
|
64
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module ExtensionWhitelist
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
before :cache, :check_whitelist!
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Override this method in your uploader to provide a white list of extensions which
|
14
|
+
# are allowed to be uploaded.
|
15
|
+
#
|
16
|
+
# === Returns
|
17
|
+
#
|
18
|
+
# [NilClass, Array[String]] a white list of extensions which are allowed to be uploaded
|
19
|
+
#
|
20
|
+
# === Examples
|
21
|
+
#
|
22
|
+
# def extension_white_list
|
23
|
+
# %w(jpg jpeg gif png)
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
def extension_white_list; end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def check_whitelist!(new_file)
|
31
|
+
if extension_white_list and not extension_white_list.include?(new_file.extension.to_s)
|
32
|
+
raise CarrierWave::IntegrityError, "You are not allowed to upload #{new_file.extension.inspect} files, allowed types: #{extension_white_list.inspect}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end # ExtensionWhitelist
|
37
|
+
end # Uploader
|
38
|
+
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, :original_file
|
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,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Processing
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
include CarrierWave::Uploader::Callbacks
|
9
|
+
|
10
|
+
included do
|
11
|
+
after :cache, :process!
|
12
|
+
after :recreate_versions, :process!
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
##
|
18
|
+
# Lists processor callbacks declared
|
19
|
+
#
|
20
|
+
# === Returns
|
21
|
+
#
|
22
|
+
# [Array[Array[Symbol, Array]]] a list of processor callbacks which have been declared for this uploader
|
23
|
+
#
|
24
|
+
def processors
|
25
|
+
@processors ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Adds a processor callback which applies operations as a file is uploaded.
|
30
|
+
# The argument may be the name of any method of the uploader, expressed as a symbol,
|
31
|
+
# or a list of such methods, or a hash where the key is a method and the value is
|
32
|
+
# an array of arguments to call the method with
|
33
|
+
#
|
34
|
+
# === Parameters
|
35
|
+
#
|
36
|
+
# args (*Symbol, Hash{Symbol => Array[]})
|
37
|
+
#
|
38
|
+
# === Examples
|
39
|
+
#
|
40
|
+
# class MyUploader < CarrierWave::Uploader::Base
|
41
|
+
#
|
42
|
+
# process :sepiatone, :vignette
|
43
|
+
# process :scale => [200, 200]
|
44
|
+
#
|
45
|
+
# def sepiatone
|
46
|
+
# ...
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# def vignette
|
50
|
+
# ...
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# def scale(height, width)
|
54
|
+
# ...
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
def process(*args)
|
59
|
+
args.each do |arg|
|
60
|
+
if arg.is_a?(Hash)
|
61
|
+
arg.each do |method, args|
|
62
|
+
processors.push([method, args])
|
63
|
+
end
|
64
|
+
else
|
65
|
+
processors.push([arg, []])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end # ClassMethods
|
71
|
+
|
72
|
+
##
|
73
|
+
# Apply all process callbacks added through CarrierWave.process
|
74
|
+
#
|
75
|
+
def process!(new_file=nil)
|
76
|
+
if enable_processing
|
77
|
+
self.class.processors.each do |method, args|
|
78
|
+
self.send(method, *args)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end # Processing
|
84
|
+
end # Uploader
|
85
|
+
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,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Remove
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
include CarrierWave::Uploader::Callbacks
|
9
|
+
|
10
|
+
##
|
11
|
+
# Removes the file and reset it
|
12
|
+
#
|
13
|
+
def remove!
|
14
|
+
with_callbacks(:remove) do
|
15
|
+
@file.delete if @file
|
16
|
+
@file = nil
|
17
|
+
@cache_id = nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end # Remove
|
22
|
+
end # Uploader
|
23
|
+
end # CarrierWave
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Rename
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
include CarrierWave::Uploader::Callbacks
|
9
|
+
|
10
|
+
included do
|
11
|
+
after :rename, :recreate_versions!
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# Override this method in your uploader to check if the model has been updated.
|
16
|
+
#
|
17
|
+
# === Returns
|
18
|
+
#
|
19
|
+
# [NilClass, Boolean] true if the model has been changed, false otherwise
|
20
|
+
#
|
21
|
+
# === Examples
|
22
|
+
#
|
23
|
+
# def stale_model?
|
24
|
+
# model.folder_changed? # because store_dir is based on the folder property of the model
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
def stale_model?
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def rename?
|
32
|
+
@rename || false
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Renames the file
|
37
|
+
#
|
38
|
+
def rename!
|
39
|
+
return true if !self.rename?
|
40
|
+
|
41
|
+
with_callbacks(:rename) do
|
42
|
+
@file = storage.rename!(@original_file)
|
43
|
+
@original_file = nil
|
44
|
+
@rename = false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def check_stale_model!
|
51
|
+
# the conditions below means: already an existing file, with model, model has been modified and not changing the file currently.
|
52
|
+
@rename = self.file && self.model && self.stale_model? && @cache_id.nil?
|
53
|
+
|
54
|
+
if self.rename?
|
55
|
+
@original_file = self.file.clone
|
56
|
+
@filename = self.model.send(:_mounter, self.mounted_as).identifier # default filename has to be the one from the model
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end # Rename
|
61
|
+
end # Uploader
|
62
|
+
end # CarrierWave
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module Store
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
include CarrierWave::Uploader::Callbacks
|
9
|
+
include CarrierWave::Uploader::Configuration
|
10
|
+
include CarrierWave::Uploader::Cache
|
11
|
+
|
12
|
+
##
|
13
|
+
# Override this in your Uploader to change the filename.
|
14
|
+
#
|
15
|
+
# Be careful using record ids as filenames. If the filename is stored in the database
|
16
|
+
# the record id will be nil when the filename is set. Don't use record ids unless you
|
17
|
+
# understand this limitation.
|
18
|
+
#
|
19
|
+
# Do not use the version_name in the filename, as it will prevent versions from being
|
20
|
+
# loaded correctly.
|
21
|
+
#
|
22
|
+
# === Returns
|
23
|
+
#
|
24
|
+
# [String] a filename
|
25
|
+
#
|
26
|
+
def filename
|
27
|
+
@filename
|
28
|
+
end
|
29
|
+
|
30
|
+
def delete_original_file?
|
31
|
+
@original_file && @original_file.path != @file.path # if the path hasn't changed, no need to delete it
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# Calculates the path where the file should be stored. If +for_file+ is given, it will be
|
36
|
+
# used as the filename, otherwise +CarrierWave::Uploader#filename+ is assumed.
|
37
|
+
#
|
38
|
+
# === Parameters
|
39
|
+
#
|
40
|
+
# [for_file (String)] name of the file <optional>
|
41
|
+
#
|
42
|
+
# === Returns
|
43
|
+
#
|
44
|
+
# [String] the store path
|
45
|
+
#
|
46
|
+
def store_path(for_file=filename)
|
47
|
+
File.join([store_dir, full_filename(for_file)].compact)
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Stores the file by passing it to this Uploader's storage engine.
|
52
|
+
#
|
53
|
+
# If new_file is omitted, a previously cached file will be stored.
|
54
|
+
#
|
55
|
+
# === Parameters
|
56
|
+
#
|
57
|
+
# [new_file (File, IOString, Tempfile)] any kind of file object
|
58
|
+
#
|
59
|
+
def store!(new_file=nil)
|
60
|
+
cache!(new_file) if new_file
|
61
|
+
if @file and @cache_id
|
62
|
+
with_callbacks(:store, new_file) do
|
63
|
+
@file = storage.store!(@file)
|
64
|
+
|
65
|
+
@original_file.delete if self.delete_original_file?
|
66
|
+
|
67
|
+
@cache_id = nil
|
68
|
+
@original_file = nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Retrieves the file from the storage.
|
75
|
+
#
|
76
|
+
# === Parameters
|
77
|
+
#
|
78
|
+
# [identifier (String)] uniquely identifies the file to retrieve
|
79
|
+
#
|
80
|
+
def retrieve_from_store!(identifier)
|
81
|
+
with_callbacks(:retrieve_from_store, identifier) do
|
82
|
+
@file = storage.retrieve!(identifier)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def full_filename(for_file)
|
89
|
+
for_file
|
90
|
+
end
|
91
|
+
|
92
|
+
def storage
|
93
|
+
@storage ||= self.class.storage.new(self)
|
94
|
+
end
|
95
|
+
|
96
|
+
end # Store
|
97
|
+
end # Uploader
|
98
|
+
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 as_json(options = nil)
|
28
|
+
{ :url => url }
|
29
|
+
end
|
30
|
+
|
31
|
+
end # Url
|
32
|
+
end # Uploader
|
33
|
+
end # CarrierWave
|