Sha256: bf0696ac1ff7ef7740c5e63d995c3b5ec048ab9471ac84fab32e25dff4525e28

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

module RailsUploads
  module Storages
    class Local

      def initialize(default)
        @tmp = (Rails.env == 'test' and not default)
      end

      def exists?(path)
        ::File.exists? realpath(path)
      end

      def size(path)
        ::File.size realpath(path)
      end

      def url(path)
        ::File.join Rails.application.config.uploads.base_url, path
      end

      def store(upload, path)
        create_dir realpath(path)
        upload.rewind # Hack to avoid empty files
        ::File.open(realpath(path), 'wb') do |file|
          while chunk = upload.read(16 * 1024)
            file.write(chunk)
          end
        end
      end
      
      def delete(path)
        ::File.delete realpath(path)
      end

      def magick(source, output, upload)
        create_dir realpath(output)
        yield RailsUploads::Magick::Image.new(realpath(source), realpath(output))
      end

      protected

      def realpath(path)
        base_path.join path
      end

      def base_path
        Rails.root.join @tmp ? 'tmp' : 'public'
      end

      def create_dir(path)
        dir = base_path.join('uploads', path).dirname
        FileUtils.mkdir_p dir unless ::File.directory? dir
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rails_uploads-0.2.9 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.8 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.7 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.6 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.5 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.4 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.3 lib/rails_uploads/storages/local.rb
rails_uploads-0.2.2 lib/rails_uploads/storages/local.rb