Git Product home page Git Product logo

carrierwave-colore's Introduction

CarrierWave Colore

CarrierWave adapter for the Colore document storage system.

Setup

Add to your Gemfile:

gem 'colore-client',      github: 'ifad/colore-client'
gem 'carrierwave-colore', github: 'ifad/carrierwave-colore'

And configure CarrierWave, e.g. in config/initializers/carrierwave.rb:

CarrierWave.configure do |config|
  config.storage = :colore
  config.colore_config = {
    base_uri: 'https://my-colore-host/',
    app:      "MyApp",
    logger:   Rails.logger
  }
end

Usage

Colore requires a unique identifier for each file uploaded. This should be defined on your uploader with the store_path method.

For example, to use the record id:

class DocumentUploader < CarrierWave::Uploader::Base
  def store_path
    "#{model.class.name}.#{model.id}"
  end
end

Or to generate a unique token for each record:

class DocumentUploader < CarrierWave::Uploader::Base
  def store_path
    token
  end

  process :store_token

  def store_token
    model.token = token
  end

  def token
    @token ||= begin
      model.token || SecureRandom.uuid
    end
  end
end

Other than that, it just works like a regular CarrierWave adapter.

Converting a document with Colore

(These examples assumes that attachment is a mounted Uploader on the Document model.)

To convert a file to another format:

file = Document.find(1234).attachment.file
file.convert('txt')

Where txt is a Colore conversion action. Note that conversion happens asynchronously. You can pass a second parameter, which is an URL that will receive a POST callback once the conversion is complete.

To then get the converted version:

file = Document.find(1234).attachment.file
file.format('txt').read
=> "The quick brown fox..."

You can view all versions and formats as follows:

file = Document.find(1234).attachment.file
file.versions
=> {"v001" => ["docx", "txt"], "v002"=>["txt", "docx"]}

It defaults to reading the current version, you can also specify the version:

file = Document.find(1234).attachment.file
file.version('v001').format('txt').read
=> "The slow brown fox..."

Serving files

For best performance you should use the Colore Nginx module, so files are served by Nginx rather than your application or Colore itself.

You can get the URL of the file to use with this like this:

url = Document.find(1234).attachment.url
=> "/document/doccy/1e723575/current/Support.docx"

Here is an example of what you can use in a Rails controller to serve this:

def show
  @document = Document.find(params[:id])
  response['X-Accel-Redirect']    = @document.attachment.url
  response['Content-Disposition'] = "attachment; filename=#{@document.filename}"
  render nothing: true
end

carrierwave-colore's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.