├── .document ├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── Rakefile ├── VERSION ├── carrierwave-postgresql.gemspec ├── lib ├── carrierwave-postgresql.rb └── carrierwave │ ├── postgresql.rb │ └── storage │ ├── adapters │ ├── jdbc_connection.rb │ └── pg_connection.rb │ └── postgresql_lo.rb └── spec ├── fixtures ├── another_test.jpg └── test.jpg ├── spec_helper.rb ├── storage ├── file_spec.rb └── postgresql_lo_spec.rb └── support ├── active_record.rb └── mock_files.rb /.document: -------------------------------------------------------------------------------- 1 | lib/**/*.rb 2 | bin/* 3 | - 4 | features/**/*.feature 5 | LICENSE.txt 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # rcov generated 2 | coverage 3 | coverage.data 4 | 5 | # rdoc generated 6 | rdoc 7 | 8 | # yard generated 9 | doc 10 | .yardoc 11 | 12 | # bundler 13 | .bundle 14 | 15 | # jeweler generated 16 | pkg 17 | 18 | # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: 19 | # 20 | # * Create a file at ~/.gitignore 21 | # * Include files you want ignored 22 | # * Run: git config --global core.excludesfile ~/.gitignore 23 | # 24 | # After doing this, these files will be ignored in all your git projects, 25 | # saving you from having to 'pollute' every project you touch with them 26 | # 27 | # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line) 28 | # 29 | # For MacOS: 30 | # 31 | #.DS_Store 32 | 33 | # For TextMate 34 | #*.tmproj 35 | #tmtags 36 | 37 | # For emacs: 38 | #*~ 39 | #\#* 40 | #.\#* 41 | 42 | # For vim: 43 | #*.swp 44 | 45 | # For redcar: 46 | #.redcar 47 | 48 | # For rubinius: 49 | #*.rbc 50 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format d 2 | --colour 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | services: postgresql 3 | rvm: 4 | - "2.3.0" 5 | - "jruby-9.0.5.0" 6 | before_script: 7 | - psql -c 'create database carrierwave_test;' -U postgres 8 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gemspec 4 | 5 | gem 'pg', platform: :ruby 6 | gem 'activerecord-jdbcpostgresql-adapter', platform: :jruby 7 | gem 'pry' 8 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | carrierwave-postgresql (0.3.0) 5 | carrierwave (>= 0.10.0) 6 | 7 | GEM 8 | remote: http://rubygems.org/ 9 | specs: 10 | activemodel (4.0.13) 11 | activesupport (= 4.0.13) 12 | builder (~> 3.1.0) 13 | activerecord (4.0.13) 14 | activemodel (= 4.0.13) 15 | activerecord-deprecated_finders (~> 1.0.2) 16 | activesupport (= 4.0.13) 17 | arel (~> 4.0.0) 18 | activerecord-deprecated_finders (1.0.4) 19 | activerecord-jdbc-adapter (1.3.20) 20 | activerecord (>= 2.2) 21 | activerecord-jdbcpostgresql-adapter (1.3.20) 22 | activerecord-jdbc-adapter (~> 1.3.20) 23 | jdbc-postgres (>= 9.1) 24 | activesupport (4.0.13) 25 | i18n (~> 0.6, >= 0.6.9) 26 | minitest (~> 4.2) 27 | multi_json (~> 1.3) 28 | thread_safe (~> 0.1) 29 | tzinfo (~> 0.3.37) 30 | arel (4.0.2) 31 | builder (3.1.4) 32 | carrierwave (1.2.2) 33 | activemodel (>= 4.0.0) 34 | activesupport (>= 4.0.0) 35 | mime-types (>= 1.16) 36 | coderay (1.1.1) 37 | diff-lcs (1.2.5) 38 | ffi (1.9.18) 39 | ffi (1.9.18-java) 40 | i18n (0.7.0) 41 | jdbc-postgres (9.4.1206) 42 | method_source (0.8.2) 43 | mime-types (3.1) 44 | mime-types-data (~> 3.2015) 45 | mime-types-data (3.2016.0521) 46 | minitest (4.7.5) 47 | multi_json (1.12.1) 48 | pg (0.18.4) 49 | pry (0.10.4) 50 | coderay (~> 1.1.0) 51 | method_source (~> 0.8.1) 52 | slop (~> 3.4) 53 | pry (0.10.4-java) 54 | coderay (~> 1.1.0) 55 | method_source (~> 0.8.1) 56 | slop (~> 3.4) 57 | spoon (~> 0.0) 58 | rake (11.2.2) 59 | rspec (3.5.0) 60 | rspec-core (~> 3.5.0) 61 | rspec-expectations (~> 3.5.0) 62 | rspec-mocks (~> 3.5.0) 63 | rspec-core (3.5.2) 64 | rspec-support (~> 3.5.0) 65 | rspec-expectations (3.5.0) 66 | diff-lcs (>= 1.2.0, < 2.0) 67 | rspec-support (~> 3.5.0) 68 | rspec-its (1.2.0) 69 | rspec-core (>= 3.0.0) 70 | rspec-expectations (>= 3.0.0) 71 | rspec-mocks (3.5.0) 72 | diff-lcs (>= 1.2.0, < 2.0) 73 | rspec-support (~> 3.5.0) 74 | rspec-support (3.5.0) 75 | slop (3.6.0) 76 | spoon (0.0.6) 77 | ffi 78 | thread_safe (0.3.5) 79 | thread_safe (0.3.5-java) 80 | tzinfo (0.3.51) 81 | 82 | PLATFORMS 83 | java 84 | ruby 85 | 86 | DEPENDENCIES 87 | activerecord (>= 4.0.1) 88 | activerecord-jdbcpostgresql-adapter 89 | bundler 90 | carrierwave-postgresql! 91 | pg 92 | pry 93 | rake 94 | rspec 95 | rspec-its 96 | 97 | BUNDLED WITH 98 | 1.16.1 99 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # carrierwave-postgresql [![Build Status](https://secure.travis-ci.org/diogob/carrierwave-postgresql.svg)](http://travis-ci.org/diogob/carrierwave-postgresql)[![Code Climate](https://codeclimate.com/github/diogob/carrierwave-postgresql.svg)](https://codeclimate.com/github/diogob/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 [official docs](http://www.postgresql.org/docs/9.2/static/largeobjects.html) 6 | 7 | ## Note about 0.2.0 8 | 9 | This version drops support for ruby 1.9. If you use this version you should stick to 0.1.5. 10 | The new adition to 0.2.0 is support to JRuby thanks to [@TheKidCoder](https://github.com/TheKidCoder). 11 | Due to the JRuby support the gem pg is no longer a dependency in our gemspec. 12 | This means that **you need either pg or activerecord-jdbcpostgresql-adapter** as a dependency of your project. 13 | 14 | ## Installation 15 | 16 | Install the latest release: 17 | 18 | gem install carrierwave-postgresql 19 | 20 | Require it in your code: 21 | 22 | require 'carrierwave/postgresql' 23 | 24 | Or, in Rails you can add it to your Gemfile: 25 | 26 | gem 'carrierwave-postgresql' 27 | 28 | ## Getting Started 29 | 30 | First, this extension assumes 2 important things: 31 | 32 | * You are using CarrierWave with ActiveRecord 33 | * Your database is PostgreSQL 34 | 35 | If you fill the above requirements then you can proceed to the wonderful land of database stored files! 36 | 37 | Start off by generating an uploader: 38 | 39 | rails generate uploader Avatar 40 | 41 | this should give you a file in: 42 | 43 | app/uploaders/avatar_uploader.rb 44 | 45 | You can configure Carrierwave to use PostgreSQL Large Objects instead of the filesystem. 46 | Just change your uploader storage to `:postgresql_lo` as in: 47 | 48 | ```ruby 49 | class AvatarUploader < CarrierWave::Uploader::Base 50 | storage :postgresql_lo 51 | end 52 | ``` 53 | 54 | Add an oid column to the (existing) model you want to mount the uploader on: 55 | 56 | ```ruby 57 | add_column :users, :avatar, :oid 58 | ``` 59 | 60 | *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. 61 | 62 | *NOTE:* If the model does not exist, and you are going to write a new model to store the oid's, the equivalent create_table migration is 63 | ```ruby 64 | def change 65 | create_table :users do |t| 66 | t.column :avatar, :oid, :null => false 67 | ``` 68 | 69 | Open your model file and mount the uploader: 70 | 71 | ```ruby 72 | class User < ActiveRecord::Base 73 | mount_uploader :avatar, AvatarUploader 74 | end 75 | ``` 76 | 77 | Now you can cache files by assigning them to the attribute, they will 78 | automatically be stored sinside the database using the Large Object facility when the record is saved. 79 | 80 | ```ruby 81 | u = User.new 82 | u.avatar = params[:file] 83 | u.avatar = File.open('somewhere') 84 | u.save! 85 | u.avatar.url # => '/url/to/file.png' 86 | u.avatar.current_path # => 'path/to/file.png' 87 | u.avatar.identifier # => 'file.png' 88 | ``` 89 | 90 | For more info on CarrierWave take a look at the main [Carrierwave repository](https://raw.github.com/jnicklas/carrierwave/). 91 | 92 | 93 | ## How to stream the files from the web server 94 | 95 | Since carrierwave-postgresql doesn't make the files available via HTTP. 96 | You can do this using the Rails engine [postgresql_lo_streamer](http://diogob.github.com/postgresql_lo_streamer/). 97 | 98 | Alternatively (in case you are not using Rails) you can stream them yourself. 99 | 100 | 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. 101 | 102 | For instance, in the user's avatar example the url for an image with 1 as it's oid would be `/user_avatar/1` 103 | 104 | You can retrieve the file contents using the following code: 105 | 106 | ```ruby 107 | u = User.new 108 | u.avatar = params[:file] 109 | u.avatar = File.open('somewhere') 110 | u.save! 111 | u.avatar.file.read 112 | ``` 113 | 114 | ## Contributing to carrierwave-postgresql 115 | 116 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. 117 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. 118 | * Fork the project. 119 | * Start a feature/bugfix branch. 120 | * Commit and push until you are happy with your contribution. 121 | * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. 122 | * 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. 123 | 124 | ## Acknowledgments 125 | 126 | This code is largely based on [carrierwave-mongoid](https://github.com/jnicklas/carrierwave-mongoid) 127 | I wanted to thank [jnicklas](https://github.com/jnicklas) for all the open source code and for the great (and extensible) architecture of CarrierWave. 128 | 129 | ## Copyright 130 | 131 | Copyright (c) 2012 Diogo Biazus. See LICENSE.txt for 132 | further details. 133 | 134 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | begin 3 | require 'bundler/setup' 4 | rescue LoadError 5 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 6 | end 7 | begin 8 | require 'rdoc/task' 9 | rescue LoadError 10 | require 'rdoc/rdoc' 11 | require 'rake/rdoctask' 12 | RDoc::Task = Rake::RDocTask 13 | end 14 | 15 | RDoc::Task.new(:rdoc) do |rdoc| 16 | rdoc.rdoc_dir = 'rdoc' 17 | rdoc.title = 'PostgresqlLoStreamer' 18 | rdoc.options << '--line-numbers' 19 | rdoc.rdoc_files.include('README.rdoc') 20 | rdoc.rdoc_files.include('lib/**/*.rb') 21 | end 22 | 23 | 24 | 25 | 26 | Bundler::GemHelper.install_tasks 27 | 28 | require 'rspec/core' 29 | require 'rspec/core/rake_task' 30 | RSpec::Core::RakeTask.new(:spec) do |spec| 31 | spec.pattern = FileList['spec/**/*_spec.rb'] 32 | end 33 | 34 | RSpec::Core::RakeTask.new(:rcov) do |spec| 35 | spec.pattern = 'spec/**/*_spec.rb' 36 | spec.rcov = true 37 | end 38 | 39 | task :default => :spec 40 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.1.2 -------------------------------------------------------------------------------- /carrierwave-postgresql.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | # Describe your gem and declare its dependencies: 4 | Gem::Specification.new do |s| 5 | s.name = "carrierwave-postgresql" 6 | s.version = "0.3.0" 7 | s.date = "2013-11-19" 8 | s.authors = ["Diogo Biazus"] 9 | s.email = ["diogo@biazus.me"] 10 | s.homepage = "https://github.com/diogob/carrierwave-postgresql" 11 | s.summary = "Carrierwave storing files in a PostgreSQL database" 12 | 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." 13 | 14 | s.files = Dir["{lib}/**/*"] + ["LICENSE.txt", "Rakefile", "README.md"] 15 | 16 | s.add_dependency "carrierwave", ">= 0.10.0" 17 | 18 | s.add_development_dependency "activerecord", ">= 4.0.1" 19 | s.add_development_dependency "rspec" 20 | s.add_development_dependency "rspec-its" 21 | s.add_development_dependency "bundler" 22 | s.add_development_dependency "rake" 23 | end 24 | -------------------------------------------------------------------------------- /lib/carrierwave-postgresql.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require 'carrierwave/postgresql' 3 | -------------------------------------------------------------------------------- /lib/carrierwave/postgresql.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'carrierwave' 4 | if defined?(JRUBY_VERSION) 5 | require 'carrierwave/storage/adapters/jdbc_connection' 6 | else 7 | require 'carrierwave/storage/adapters/pg_connection' 8 | end 9 | require 'carrierwave/storage/postgresql_lo' 10 | 11 | module CarrierWave 12 | module Postgresql 13 | end 14 | end 15 | 16 | CarrierWave::Storage.autoload :PostgresqlLo, 'carrierwave/storage/postgresql_lo' 17 | CarrierWave::Uploader::Base.storage_engines[:postgresql_lo] = "CarrierWave::Storage::PostgresqlLo" 18 | -------------------------------------------------------------------------------- /lib/carrierwave/storage/adapters/jdbc_connection.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | module CarrierWave 3 | module Storage 4 | module Adapters 5 | module JDBCConnection 6 | def identifier 7 | @oid ||= connection.connection.getLargeObjectAPI.createLO 8 | end 9 | 10 | def read 11 | @uploader.model.transaction do 12 | lo = lo_manager.java_send :open, [Java::long], identifier 13 | bytes = lo.read(lo.size) 14 | lo.close 15 | String.from_java_bytes(bytes) 16 | end 17 | end 18 | 19 | def write(file) 20 | array_buf = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(file.path)) 21 | @uploader.model.transaction do 22 | lo = lo_manager.java_send :open, [Java::long, Java::int], identifier, Java::OrgPostgresqlLargeobject::LargeObjectManager::WRITE 23 | lo.truncate(0) 24 | lo.write(array_buf) 25 | size = lo.size 26 | lo.close 27 | size 28 | end 29 | end 30 | 31 | def delete 32 | lo_manager.java_send :unlink, [Java::long], identifier 33 | end 34 | 35 | def file_length 36 | @uploader.model.transaction do 37 | lo = lo_manager.java_send :open, [Java::long], identifier 38 | size = lo.size 39 | lo.close 40 | size 41 | end 42 | end 43 | 44 | private 45 | def lo_manager 46 | @lo_manager ||= connection.connection.getLargeObjectAPI 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/carrierwave/storage/adapters/pg_connection.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | module CarrierWave 3 | module Storage 4 | module Adapters 5 | module PGConnection 6 | def identifier 7 | @oid ||= connection.lo_creat 8 | end 9 | 10 | def read 11 | @uploader.model.transaction do 12 | lo = connection.lo_open(identifier) 13 | content = connection.lo_read(lo, file_length) 14 | connection.lo_close(lo) 15 | content 16 | end 17 | end 18 | 19 | def write(file) 20 | @uploader.model.transaction do 21 | lo = connection.lo_open(identifier, ::PG::INV_WRITE) 22 | connection.lo_truncate(lo, 0) 23 | size = connection.lo_write(lo, file.read) 24 | connection.lo_close(lo) 25 | size 26 | end 27 | end 28 | 29 | def delete 30 | connection.lo_unlink(identifier) 31 | end 32 | 33 | def file_length 34 | @uploader.model.transaction do 35 | lo = connection.lo_open(identifier) 36 | size = connection.lo_lseek(lo, 0, 2) 37 | connection.lo_close(lo) 38 | size 39 | end 40 | end 41 | end 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/carrierwave/storage/postgresql_lo.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | module CarrierWave 3 | module Storage 4 | class PostgresqlLo < Abstract 5 | class File 6 | if defined?(JRUBY_VERSION) 7 | include CarrierWave::Storage::Adapters::JDBCConnection 8 | else 9 | include CarrierWave::Storage::Adapters::PGConnection 10 | end 11 | 12 | def initialize(uploader) 13 | @uploader = uploader 14 | end 15 | 16 | def url 17 | "/#{@uploader.model.class.name.underscore.gsub('/', '_')}_#{@uploader.mounted_as.to_s.underscore}/#{identifier}" 18 | end 19 | 20 | def content_type 21 | end 22 | 23 | alias :size :file_length 24 | 25 | def connection 26 | @connection ||= @uploader.model.class.connection.raw_connection 27 | end 28 | 29 | def identifier 30 | @oid ||= @uploader.identifier.to_i 31 | end 32 | 33 | def original_filename 34 | identifier.to_s 35 | end 36 | 37 | end 38 | 39 | def store!(file) 40 | raise "This uploader must be mounted in an ActiveRecord model to work" unless uploader.model 41 | stored = CarrierWave::Storage::PostgresqlLo::File.new(uploader) 42 | stored.write(file) 43 | stored 44 | end 45 | 46 | def retrieve!(identifier) 47 | raise "This uploader must be mounted in an ActiveRecord model to work" unless uploader.model 48 | @oid = identifier 49 | CarrierWave::Storage::PostgresqlLo::File.new(uploader) 50 | end 51 | 52 | def identifier 53 | @oid ||= create_large_object 54 | end 55 | 56 | def connection 57 | @connection ||= uploader.model.class.connection.raw_connection 58 | end 59 | 60 | private 61 | def create_large_object 62 | if defined?(JRUBY_VERSION) 63 | connection.connection.getLargeObjectAPI.createLO 64 | else 65 | connection.lo_creat 66 | end 67 | end 68 | end 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /spec/fixtures/another_test.jpg: -------------------------------------------------------------------------------- 1 | this is another longer stuff 2 | -------------------------------------------------------------------------------- /spec/fixtures/test.jpg: -------------------------------------------------------------------------------- 1 | this is stuff -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 2 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 3 | require 'rspec' 4 | require 'rspec/its' 5 | require 'carrierwave-postgresql' 6 | require 'tempfile' 7 | 8 | require 'carrierwave' 9 | 10 | # Requires supporting files with custom matchers and macros, etc, 11 | # in ./support/ and its subdirectories. 12 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} 13 | 14 | -------------------------------------------------------------------------------- /spec/storage/file_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe CarrierWave::Storage::PostgresqlLo::File do 4 | let(:test_model){ Test.new } 5 | let(:uploader){ double('an uploader', model: test_model, identifier: identifier, mounted_as: :file) } 6 | let(:file){ CarrierWave::Storage::PostgresqlLo::File.new(uploader) } 7 | let(:tempfile){ stub_tempfile('test.jpg', 'application/xml') } 8 | let(:file_content){ 'this is stuff' } 9 | let(:identifier) { 10 | if defined?(JRUBY_VERSION) 11 | Test.connection.raw_connection.connection.getLargeObjectAPI.createLO 12 | else 13 | Test.connection.raw_connection.lo_creat 14 | end 15 | } 16 | 17 | describe "#delete" do 18 | it("should delete the file using the lo interface") do 19 | ActiveRecord::Base.transaction { 20 | file.write(tempfile) 21 | file.delete 22 | } 23 | expect { file.read }.to raise_error(Exception) 24 | end 25 | end 26 | 27 | describe "#url" do 28 | subject{ file.url } 29 | it{ ActiveRecord::Base.transaction {should == "/test_file/#{identifier}"} } 30 | 31 | context "on a namespaced model" do 32 | let(:test_model){ Namespace::Test.new } 33 | it { 34 | ActiveRecord::Base.transaction { 35 | should == "/namespace_test_file/#{identifier}" 36 | } 37 | } 38 | end 39 | end 40 | 41 | describe "#write" do 42 | it("should write the file using the lo interface") do 43 | ActiveRecord::Base.transaction { 44 | expect(file.write(tempfile)).to eq file_content.length 45 | } 46 | end 47 | 48 | it("should change file size after a write is called twice on the same identifier") do 49 | ActiveRecord::Base.transaction { 50 | file.write(stub_tempfile('another_test.jpg', 'application/xml')) 51 | file.write(tempfile) 52 | } 53 | expect(ActiveRecord::Base.transaction {file.read}).to eq file_content 54 | end 55 | end 56 | 57 | describe "#file_length" do 58 | it("should return the file size") do 59 | ActiveRecord::Base.transaction {file.write(tempfile)} 60 | expect(ActiveRecord::Base.transaction {file.file_length}).to eq file_content.length 61 | end 62 | end 63 | 64 | describe "#read" do 65 | it("should read the file using the lo interface") do 66 | ActiveRecord::Base.transaction {file.write(tempfile)} 67 | expect(ActiveRecord::Base.transaction {file.read}).to eq file_content 68 | end 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /spec/storage/postgresql_lo_spec.rb: -------------------------------------------------------------------------------- 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){ double('an uploader', model: nil) } 11 | it("should raise error if not mounted"){ expect{ storage.store!(file) }.to 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){ double('an uploader', model: test_model, mounted_as: :file) } 16 | let(:lo){ storage.store!(file) } 17 | before do 18 | allow(uploader).to receive(:identifier) do 19 | storage.identifier 20 | end 21 | end 22 | subject{ storage.retrieve! lo.identifier } 23 | its(:read){ is_expected.to eq '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) { double('an uploader', model: nil) } 30 | it("should raise error if not mounted") { expect{ storage.store!(file) }.to 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) { double('an uploader', model: test_model) } 35 | let(:lo) { storage.store!(file) } 36 | before do 37 | allow(uploader).to receive(:identifier) do 38 | storage.identifier 39 | end 40 | end 41 | subject { lo } 42 | its(:read) { is_expected.to eq 'this is stuff' } 43 | end 44 | end 45 | 46 | it 'test with mounted as' do 47 | test = Test.create!(my_file: file) 48 | expect(Test.find(test.id).my_file.read).to eq 'this is stuff' 49 | test.destroy 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /spec/support/active_record.rb: -------------------------------------------------------------------------------- 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.create_table :tests, force: true do |t| 12 | t.column :file, :oid 13 | end 14 | end 15 | end 16 | require 'carrierwave' 17 | require 'carrierwave/orm/activerecord' 18 | 19 | class LoUploader < CarrierWave::Uploader::Base 20 | storage :postgresql_lo 21 | end 22 | 23 | class Test < ActiveRecord::Base 24 | include CarrierWave::ActiveRecord 25 | mount_uploader :my_file, LoUploader, mount_on: :file 26 | end 27 | 28 | module Namespace 29 | class Test < ActiveRecord::Base 30 | end 31 | end 32 | 33 | 34 | class Arel::Visitors::ToSql 35 | def visit_Integer(value, a) 36 | quoted(value.to_s, a) 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /spec/support/mock_files.rb: -------------------------------------------------------------------------------- 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 | File.open(file_path(filename)) 10 | end 11 | 12 | def stub_tempfile(filename, mime_type=nil, fake_name=nil) 13 | raise "#{file_path(filename)} file does not exist" unless File.exist?(file_path(filename)) 14 | 15 | t = Tempfile.new(filename) 16 | FileUtils.copy_file(file_path(filename), t.path) 17 | 18 | allow(t).to receive(:local_path).and_return('') 19 | allow(t).to receive(:original_filename).and_return(filename || fake_name) 20 | allow(t).to receive(:content_type).and_return(mime_type) 21 | t 22 | end 23 | end 24 | end 25 | end 26 | 27 | RSpec.configure do |config| 28 | config.include CarrierWave::Test::MockFiles 29 | end 30 | 31 | --------------------------------------------------------------------------------