ksss/tori


Tori is a bird

License: MIT

Language: Ruby


Tori

test

"(\( ⁰⊖⁰)/)"

Tori is a very very simple file uploader.

Tori does nothing.

Only file upload to backend store.

You can upload file without alter database.

Quick start on Rails

Gemfile

gem 'tori', require: 'tori/rails'

app/models/photo.rb

class Photo < ActiveRecord::Base
  tori :image

  after_save do
    image.write if image.from?
  end

  after_destroy do
    image.delete
  end
end

app/controllers/photos_controller.rb

class PhotosController < ApplicationController
  def new
    @photo = Photo.new
  end

  def create
    Photo.create(photo_params)
    redirect_to root_path
  end

  private

    def photo_params
      params.require(:photo).permit(:image)
    end
end

app/views/photos/new.html.slim

= form_for @photo, multipart: true |f|
  = f.file_field 'image'
  = f.button 'Upload'

You can read file.

photo.image.read #=> image bin
photo.image.exist? #=> exist check
photo.image.name #=> filename

Attach example

Two image file upload to backend example. defined method by tori method can define a key name for each by block.

class Photo < ActiveRecord::Base
  tori :original_image do |model|
    "#{model.class}/original/#{model.original_filename}"
  end

  tori :striped_image do |model|
    "#{model.class}/striped/#{model.striped_filename}"
  end

  # customize backend each by `tori` method.
  tori :custom, to: Tori::Backend::FileSystem.new(Pathname("custom")) do |model|
    "#{__tori__}/#{id}"
  end
end

class PhotoController < ApplicationController
  def create
    original = params[:file]
    Tempfile.open("striped") { |striped|
      # image processing example
      MiniMagick::Tool::Convert.new { |c|
        c.strip
        c << original.path
        c << striped.path
      }

      # create record
      photo = Photo.create

      # set image file to model
      photo.original_image = original
      photo.striped_image = striped

      # write image file to backend
      photo.original_image.write
      photo.striped_image.write
    }
  end
end

Default configure

https://github.com/ksss/tori/blob/master/lib/tori.rb

You can change configure any time.

Project Statistics

Sourcerank 5
Repository Size 113 KB
Stars 15
Forks 4
Watchers 4
Open issues 0
Dependencies 7
Contributors 5
Tags 27
Created
Last updated
Last pushed

Top Contributors See all

Yuki Kurihara Yusuke Nakamura kirikiriyamama naofumi-fujii hshimoyama

Packages Referencing this Repo

tori
Simple file uploader
Latest release 0.8.0 - Updated - 15 stars

Recent Tags See all

v0.8.0 February 02, 2022
v0.7.2 September 06, 2017
v0.7.2 September 06, 2017
v0.7.1 June 30, 2016
v0.7.0 June 14, 2016
v0.6.6 March 29, 2016
v0.6.5 March 17, 2016
v0.6.4 February 29, 2016
v0.6.3 November 29, 2015
v0.6.2 November 19, 2015
v0.6.1 November 10, 2015
v0.6.0 November 09, 2015
v0.5.0 November 06, 2015
v0.4.1 October 08, 2015
v0.4.0 October 08, 2015

Something wrong with this page? Make a suggestion

Last synced: 2024-04-05 17:10:59 UTC

Login to resync this repository