carrierwave-postgresql 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +2 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +54 -0
- data/LICENSE.txt +20 -0
- data/README.md +117 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/carrierwave-postgresql.gemspec +75 -0
- data/lib/carrierwave-postgresql.rb +3 -0
- data/lib/carrierwave/postgresql.rb +12 -0
- data/lib/carrierwave/storage/postgresql_lo.rb +81 -0
- data/spec/fixtures/test.jpg +1 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/storage/file_spec.rb +49 -0
- data/spec/storage/postgresql_lo_spec.rb +45 -0
- data/spec/support/active_record.rb +18 -0
- data/spec/support/mock_files.rb +32 -0
- metadata +184 -0
data/.document
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
gem 'carrierwave', "~> 0.6.2"
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem "activerecord", "~> 3.2.8"
|
12
|
+
gem "pg"
|
13
|
+
gem "rspec", "~> 2.11.0"
|
14
|
+
gem "rdoc", "~> 3.12"
|
15
|
+
gem "bundler", "~> 1.2.1"
|
16
|
+
gem "jeweler", "~> 1.8.4"
|
17
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.2.8)
|
5
|
+
activesupport (= 3.2.8)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activerecord (3.2.8)
|
8
|
+
activemodel (= 3.2.8)
|
9
|
+
activesupport (= 3.2.8)
|
10
|
+
arel (~> 3.0.2)
|
11
|
+
tzinfo (~> 0.3.29)
|
12
|
+
activesupport (3.2.8)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
multi_json (~> 1.0)
|
15
|
+
arel (3.0.2)
|
16
|
+
builder (3.0.0)
|
17
|
+
carrierwave (0.6.2)
|
18
|
+
activemodel (>= 3.2.0)
|
19
|
+
activesupport (>= 3.2.0)
|
20
|
+
diff-lcs (1.1.3)
|
21
|
+
git (1.2.5)
|
22
|
+
i18n (0.6.1)
|
23
|
+
jeweler (1.8.4)
|
24
|
+
bundler (~> 1.0)
|
25
|
+
git (>= 1.2.5)
|
26
|
+
rake
|
27
|
+
rdoc
|
28
|
+
json (1.7.5)
|
29
|
+
multi_json (1.3.6)
|
30
|
+
pg (0.14.0)
|
31
|
+
rake (0.9.2.2)
|
32
|
+
rdoc (3.12)
|
33
|
+
json (~> 1.4)
|
34
|
+
rspec (2.11.0)
|
35
|
+
rspec-core (~> 2.11.0)
|
36
|
+
rspec-expectations (~> 2.11.0)
|
37
|
+
rspec-mocks (~> 2.11.0)
|
38
|
+
rspec-core (2.11.1)
|
39
|
+
rspec-expectations (2.11.3)
|
40
|
+
diff-lcs (~> 1.1.3)
|
41
|
+
rspec-mocks (2.11.3)
|
42
|
+
tzinfo (0.3.33)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
activerecord (~> 3.2.8)
|
49
|
+
bundler (~> 1.2.1)
|
50
|
+
carrierwave (~> 0.6.2)
|
51
|
+
jeweler (~> 1.8.4)
|
52
|
+
pg
|
53
|
+
rdoc (~> 3.12)
|
54
|
+
rspec (~> 2.11.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Diogo Biazus
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# carrierwave-postgresql
|
2
|
+
|
3
|
+
This gem adds to [CarrierWave](https://github.com/jnicklas/carrierwave/) a storage facility which will use the PostgreSQL's oid datatype to reference a large object residing in the databse. It supports up to 2GB files, though it's better suited for smaller ones. Makes life easier for fast prototyping and put all your data in the same place, allows one backup for all your data and file storage in heroku servers.
|
4
|
+
|
5
|
+
For more information on PostgreSQL Large Objects you can take a look at the [oficial docs](http://www.postgresql.org/docs/9.2/static/largeobjects.html)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the latest release:
|
10
|
+
|
11
|
+
gem install carrierwave-postgresql
|
12
|
+
|
13
|
+
Require it in your code:
|
14
|
+
|
15
|
+
require 'carrierwave/postgresql'
|
16
|
+
|
17
|
+
Or, in Rails you can add it to your Gemfile:
|
18
|
+
|
19
|
+
gem 'carrierwave-postgresql'
|
20
|
+
|
21
|
+
## Getting Started
|
22
|
+
|
23
|
+
First, this extension assumes 2 important things:
|
24
|
+
|
25
|
+
* You are using CarrierWave with ActiveRecord
|
26
|
+
* Your database is PostgreSQL
|
27
|
+
|
28
|
+
If you fill the above requirements then you can proceed to the wonderful land of database stored files!
|
29
|
+
|
30
|
+
Start off by generating an uploader:
|
31
|
+
|
32
|
+
rails generate uploader Avatar
|
33
|
+
|
34
|
+
this should give you a file in:
|
35
|
+
|
36
|
+
app/uploaders/avatar_uploader.rb
|
37
|
+
|
38
|
+
You can configure Carrierwave to use PostgreSQL Large Objects instead of the filesystem.
|
39
|
+
Just change your uploader storage to `:postgresql_lo` as in:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
43
|
+
storage :postgresql_lo
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Add an oid column to the model you want to mount the uploader on:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
add_column :users, :avatar, :oid
|
51
|
+
```
|
52
|
+
|
53
|
+
*IMPORTANT:* The table contains only an oid, which is a number referencing the large object. The file content is stored in a separate table. This is nice because it does not genrate any problem with SELECT * statements and it gives us a nice file-like interface to the large object.
|
54
|
+
|
55
|
+
Open your model file and mount the uploader:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class User < ActiveRecord::Base
|
59
|
+
mount_uploader :avatar, AvatarUploader
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Now you can cache files by assigning them to the attribute, they will
|
64
|
+
automatically be stored sinside the database using the Large Object facility when the record is saved.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
u = User.new
|
68
|
+
u.avatar = params[:file]
|
69
|
+
u.avatar = File.open('somewhere')
|
70
|
+
u.save!
|
71
|
+
u.avatar.url # => '/url/to/file.png'
|
72
|
+
u.avatar.current_path # => 'path/to/file.png'
|
73
|
+
u.avatar.identifier # => 'file.png'
|
74
|
+
```
|
75
|
+
|
76
|
+
For more info on CarrierWave take a look at the main [Carrierwave repository](https://raw.github.com/jnicklas/carrierwave/).
|
77
|
+
|
78
|
+
|
79
|
+
## How to stream the files from the web server
|
80
|
+
|
81
|
+
Since carrierwave-postgresql doesn't make the files available via HTTP, you'll need to stream
|
82
|
+
them yourself. In Rails for example, you could use the `send_data` method.
|
83
|
+
|
84
|
+
The url that will be available from the field where you mounted the uploader will be the model name followed by the attribute name and with the oid as the resource id.
|
85
|
+
|
86
|
+
For instance, in the user's avatar example the url for an image with 1 as it's oid would be `/user_avatar/1`
|
87
|
+
|
88
|
+
You can retrieve the file contents using the following code:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
u = User.new
|
92
|
+
u.avatar = params[:file]
|
93
|
+
u.avatar = File.open('somewhere')
|
94
|
+
u.save!
|
95
|
+
u.avatar.file.read
|
96
|
+
```
|
97
|
+
|
98
|
+
## Contributing to carrierwave-postgresql
|
99
|
+
|
100
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
101
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
102
|
+
* Fork the project.
|
103
|
+
* Start a feature/bugfix branch.
|
104
|
+
* Commit and push until you are happy with your contribution.
|
105
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
106
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
107
|
+
|
108
|
+
## Acknowledgments
|
109
|
+
|
110
|
+
This code is largely based on [carrierwave-mongoid](https://github.com/jnicklas/carrierwave-mongoid)
|
111
|
+
I wanted to thank (jnicklas)[https://github.com/jnicklas] for all the open source code and for the great (and extensible) architecture of CarrierWave.
|
112
|
+
|
113
|
+
## Copyright
|
114
|
+
|
115
|
+
Copyright (c) 2012 Diogo Biazus. See LICENSE.txt for
|
116
|
+
further details.
|
117
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "carrierwave-postgresql"
|
18
|
+
gem.homepage = "http://diogob.github.com/carrierwave-postgresql/"
|
19
|
+
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = %Q{Use PostgreSQL large objects (AKA BLOBs) to store your files inside the database}
|
22
|
+
gem.description = %Q{This gem adds to carrierwave a storage facility which will use the PostgreSQL's oid datatype to reference a large object residing in the databse. It supports up to 2GB files, though it's better suited for smaller ones. Makes life easier for fast prototyping and put all your data in the same place, allows one backup for all your data and file storage in heroku servers.}
|
23
|
+
gem.email = "diogo@biazus.me"
|
24
|
+
gem.authors = ["Diogo Biazus"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rdoc/task'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "carrierwave-postgresql #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "carrierwave-postgresql"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Diogo Biazus"]
|
12
|
+
s.date = "2012-09-28"
|
13
|
+
s.description = "This gem adds to carrierwave a storage facility which will use the PostgreSQL's oid datatype to reference a large object residing in the databse. It supports up to 2GB files, though it's better suited for smaller ones. Makes life easier for fast prototyping and put all your data in the same place, allows one backup for all your data and file storage in heroku servers."
|
14
|
+
s.email = "diogo@biazus.me"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"carrierwave-postgresql.gemspec",
|
29
|
+
"lib/carrierwave-postgresql.rb",
|
30
|
+
"lib/carrierwave/postgresql.rb",
|
31
|
+
"lib/carrierwave/storage/postgresql_lo.rb",
|
32
|
+
"spec/fixtures/test.jpg",
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/storage/file_spec.rb",
|
35
|
+
"spec/storage/postgresql_lo_spec.rb",
|
36
|
+
"spec/support/active_record.rb",
|
37
|
+
"spec/support/mock_files.rb"
|
38
|
+
]
|
39
|
+
s.homepage = "http://diogob.github.com/carrierwave-postgresql/"
|
40
|
+
s.licenses = ["MIT"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = "1.8.24"
|
43
|
+
s.summary = "Use PostgreSQL large objects (AKA BLOBs) to store your files inside the database"
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_runtime_dependency(%q<carrierwave>, ["~> 0.6.2"])
|
50
|
+
s.add_development_dependency(%q<activerecord>, ["~> 3.2.8"])
|
51
|
+
s.add_development_dependency(%q<pg>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.11.0"])
|
53
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
54
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
|
55
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<carrierwave>, ["~> 0.6.2"])
|
58
|
+
s.add_dependency(%q<activerecord>, ["~> 3.2.8"])
|
59
|
+
s.add_dependency(%q<pg>, [">= 0"])
|
60
|
+
s.add_dependency(%q<rspec>, ["~> 2.11.0"])
|
61
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.2.1"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
64
|
+
end
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<carrierwave>, ["~> 0.6.2"])
|
67
|
+
s.add_dependency(%q<activerecord>, ["~> 3.2.8"])
|
68
|
+
s.add_dependency(%q<pg>, [">= 0"])
|
69
|
+
s.add_dependency(%q<rspec>, ["~> 2.11.0"])
|
70
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.2.1"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'carrierwave'
|
4
|
+
require 'carrierwave/storage/postgresql_lo'
|
5
|
+
|
6
|
+
module CarrierWave
|
7
|
+
module Postgresql
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
CarrierWave::Storage.autoload :PostgresqlLo, 'carrierwave/storage/postgresql_lo'
|
12
|
+
CarrierWave::Uploader::Base.storage_engines[:postgresql_lo] = "CarrierWave::Storage::PostgresqlLo"
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module CarrierWave
|
3
|
+
module Storage
|
4
|
+
class PostgresqlLo < Abstract
|
5
|
+
class File
|
6
|
+
def initialize(uploader)
|
7
|
+
@uploader = uploader
|
8
|
+
end
|
9
|
+
|
10
|
+
def url
|
11
|
+
"/#{@uploader.model.class.name.downcase}_#{@uploader.mounted_as.downcase}/#{identifier}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def read
|
15
|
+
@uploader.model.transaction do
|
16
|
+
lo = connection.lo_open(identifier)
|
17
|
+
content = connection.lo_read(lo, file_length)
|
18
|
+
connection.lo_close(lo)
|
19
|
+
content
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def write(file)
|
24
|
+
@uploader.model.transaction do
|
25
|
+
lo = connection.lo_open(identifier, ::PG::INV_WRITE)
|
26
|
+
size = connection.lo_write(lo, file.read)
|
27
|
+
connection.lo_close(lo)
|
28
|
+
size
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete
|
33
|
+
connection.lo_unlink(identifier)
|
34
|
+
end
|
35
|
+
|
36
|
+
def content_type
|
37
|
+
end
|
38
|
+
|
39
|
+
def file_length
|
40
|
+
@uploader.model.transaction do
|
41
|
+
lo = connection.lo_open(identifier)
|
42
|
+
size = connection.lo_lseek(lo, 0, 2)
|
43
|
+
connection.lo_close(lo)
|
44
|
+
size
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
alias :size :file_length
|
49
|
+
|
50
|
+
def connection
|
51
|
+
@connection ||= @uploader.model.connection.raw_connection
|
52
|
+
end
|
53
|
+
|
54
|
+
def identifier
|
55
|
+
@oid ||= @uploader.identifier
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def store!(file)
|
61
|
+
raise "This uploader must be mounted in an ActiveRecord model to work" unless uploader.model
|
62
|
+
stored = CarrierWave::Storage::PostgresqlLo::File.new(uploader)
|
63
|
+
stored.write(file)
|
64
|
+
stored
|
65
|
+
end
|
66
|
+
|
67
|
+
def retrieve!(identifier)
|
68
|
+
raise "This uploader must be mounted in an ActiveRecord model to work" unless uploader.model
|
69
|
+
CarrierWave::Storage::PostgresqlLo::File.new(uploader)
|
70
|
+
end
|
71
|
+
|
72
|
+
def identifier
|
73
|
+
@oid ||= uploader.model.read_attribute(uploader.mounted_as) || connection.lo_creat
|
74
|
+
end
|
75
|
+
|
76
|
+
def connection
|
77
|
+
@connection ||= uploader.model.connection.raw_connection
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
this is stuff
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'carrierwave-postgresql'
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
require 'carrierwave'
|
8
|
+
|
9
|
+
# Requires supporting files with custom matchers and macros, etc,
|
10
|
+
# in ./support/ and its subdirectories.
|
11
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
12
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Storage::PostgresqlLo::File do
|
4
|
+
let(:test_model){ Test.new }
|
5
|
+
let(:uploader){ mock('an uploader', :model => test_model, :identifier => 0, :mounted_as => :file) }
|
6
|
+
let(:file){ CarrierWave::Storage::PostgresqlLo::File.new(uploader) }
|
7
|
+
let(:tempfile){ stub_tempfile('test.jpg', 'application/xml') }
|
8
|
+
|
9
|
+
describe "#delete" do
|
10
|
+
before do
|
11
|
+
file.connection.should_receive(:lo_unlink).with(0)
|
12
|
+
end
|
13
|
+
it("should delete the file using the lo interface"){ file.delete }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#url" do
|
17
|
+
subject{ file.url }
|
18
|
+
it{ should == "/test_file/0" }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#write" do
|
22
|
+
before do
|
23
|
+
file.connection.should_receive(:lo_open).with(0, ::PG::INV_WRITE).and_return(2)
|
24
|
+
file.connection.should_receive(:lo_close).with(2)
|
25
|
+
file.connection.should_receive(:lo_write).with(2, "this is stuff").and_return(42)
|
26
|
+
end
|
27
|
+
it("should write the file using the lo interface"){ file.write(tempfile).should == 42 }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#file_length" do
|
31
|
+
before do
|
32
|
+
file.connection.should_receive(:lo_open).with(0).and_return(1)
|
33
|
+
file.connection.should_receive(:lo_close).with(1)
|
34
|
+
file.connection.should_receive(:lo_lseek).with(1, 0, 2).and_return(42)
|
35
|
+
end
|
36
|
+
it("should return the file size"){ file.file_length.should == 42 }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#read" do
|
40
|
+
before do
|
41
|
+
file.connection.should_receive(:lo_open).with(0).and_return(1)
|
42
|
+
file.connection.should_receive(:lo_close).with(1)
|
43
|
+
file.should_receive(:file_length).and_return(42)
|
44
|
+
file.connection.should_receive(:lo_read).with(1, 42).and_return('file content')
|
45
|
+
end
|
46
|
+
|
47
|
+
it("should read the file using the lo interface"){ file.read.should == 'file content' }
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Storage::PostgresqlLo do
|
4
|
+
let(:storage){ CarrierWave::Storage::PostgresqlLo.new(uploader) }
|
5
|
+
let(:file){ stub_tempfile('test.jpg', 'application/xml') }
|
6
|
+
let(:test_model){ Test.new }
|
7
|
+
|
8
|
+
describe "#retrieve!" do
|
9
|
+
context "when we do not have it mounted" do
|
10
|
+
let(:uploader){ mock('an uploader', :model => nil) }
|
11
|
+
it("should raise error if not mounted"){ ->{ storage.store!(file) }.should raise_error("This uploader must be mounted in an ActiveRecord model to work") }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when we have it mounted" do
|
15
|
+
let(:uploader){ mock('an uploader', :model => test_model, :mounted_as => :file) }
|
16
|
+
let(:lo){ storage.store!(file) }
|
17
|
+
before do
|
18
|
+
uploader.stub(:identifier) do
|
19
|
+
storage.identifier
|
20
|
+
end
|
21
|
+
end
|
22
|
+
subject{ storage.retrieve! lo.identifier }
|
23
|
+
its(:read){ should == "this is stuff" }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#store!" do
|
28
|
+
context "when we do not have it mounted" do
|
29
|
+
let(:uploader){ mock('an uploader', :model => nil) }
|
30
|
+
it("should raise error if not mounted"){ ->{ storage.store!(file) }.should raise_error("This uploader must be mounted in an ActiveRecord model to work") }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when we have it mounted" do
|
34
|
+
let(:uploader){ mock('an uploader', :model => test_model, :mounted_as => :file) }
|
35
|
+
let(:lo){ storage.store!(file) }
|
36
|
+
before do
|
37
|
+
uploader.stub(:identifier) do
|
38
|
+
storage.identifier
|
39
|
+
end
|
40
|
+
end
|
41
|
+
subject{ lo }
|
42
|
+
its(:read){ should == "this is stuff" }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.before(:suite) do
|
5
|
+
ActiveRecord::Base.establish_connection(
|
6
|
+
:adapter => 'postgresql',
|
7
|
+
:database => 'carrierwave_test',
|
8
|
+
:username => 'postgres',
|
9
|
+
:password => 'password',
|
10
|
+
:host => 'localhost')
|
11
|
+
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS tests;"
|
12
|
+
ActiveRecord::Base.connection.execute "CREATE TABLE tests (file oid);"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Test < ActiveRecord::Base
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Test
|
3
|
+
module MockFiles
|
4
|
+
def file_path( *paths )
|
5
|
+
File.expand_path(File.join(File.dirname(__FILE__), '../fixtures', *paths))
|
6
|
+
end
|
7
|
+
|
8
|
+
def stub_file(filename, mime_type=nil, fake_name=nil)
|
9
|
+
f = File.open(file_path(filename))
|
10
|
+
return f
|
11
|
+
end
|
12
|
+
|
13
|
+
def stub_tempfile(filename, mime_type=nil, fake_name=nil)
|
14
|
+
raise "#{file_path(filename)} file does not exist" unless File.exist?(file_path(filename))
|
15
|
+
|
16
|
+
t = Tempfile.new(filename)
|
17
|
+
FileUtils.copy_file(file_path(filename), t.path)
|
18
|
+
|
19
|
+
t.stub!(:local_path => "",
|
20
|
+
:original_filename => filename || fake_name,
|
21
|
+
:content_type => mime_type)
|
22
|
+
|
23
|
+
return t
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
RSpec.configure do |config|
|
30
|
+
config.include CarrierWave::Test::MockFiles
|
31
|
+
end
|
32
|
+
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-postgresql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Diogo Biazus
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: carrierwave
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.6.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.6.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.8
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.8
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pg
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.11.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.11.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rdoc
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.12'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '3.12'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: bundler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.2.1
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.2.1
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: jeweler
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.8.4
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.8.4
|
126
|
+
description: This gem adds to carrierwave a storage facility which will use the PostgreSQL's
|
127
|
+
oid datatype to reference a large object residing in the databse. It supports up
|
128
|
+
to 2GB files, though it's better suited for smaller ones. Makes life easier for
|
129
|
+
fast prototyping and put all your data in the same place, allows one backup for
|
130
|
+
all your data and file storage in heroku servers.
|
131
|
+
email: diogo@biazus.me
|
132
|
+
executables: []
|
133
|
+
extensions: []
|
134
|
+
extra_rdoc_files:
|
135
|
+
- LICENSE.txt
|
136
|
+
- README.md
|
137
|
+
files:
|
138
|
+
- .document
|
139
|
+
- .rspec
|
140
|
+
- Gemfile
|
141
|
+
- Gemfile.lock
|
142
|
+
- LICENSE.txt
|
143
|
+
- README.md
|
144
|
+
- Rakefile
|
145
|
+
- VERSION
|
146
|
+
- carrierwave-postgresql.gemspec
|
147
|
+
- lib/carrierwave-postgresql.rb
|
148
|
+
- lib/carrierwave/postgresql.rb
|
149
|
+
- lib/carrierwave/storage/postgresql_lo.rb
|
150
|
+
- spec/fixtures/test.jpg
|
151
|
+
- spec/spec_helper.rb
|
152
|
+
- spec/storage/file_spec.rb
|
153
|
+
- spec/storage/postgresql_lo_spec.rb
|
154
|
+
- spec/support/active_record.rb
|
155
|
+
- spec/support/mock_files.rb
|
156
|
+
homepage: http://diogob.github.com/carrierwave-postgresql/
|
157
|
+
licenses:
|
158
|
+
- MIT
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
none: false
|
165
|
+
requirements:
|
166
|
+
- - ! '>='
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
segments:
|
170
|
+
- 0
|
171
|
+
hash: -2991919450044557960
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
none: false
|
174
|
+
requirements:
|
175
|
+
- - ! '>='
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 1.8.24
|
181
|
+
signing_key:
|
182
|
+
specification_version: 3
|
183
|
+
summary: Use PostgreSQL large objects (AKA BLOBs) to store your files inside the database
|
184
|
+
test_files: []
|