Last active
February 27, 2018 20:08
-
-
Save octoberstorm/9531947 to your computer and use it in GitHub Desktop.
Rails upload using Carrierwave + Fog + Openstack Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # app/views/pictures/_form.html.erb | |
<%= form_for(@picture) do |f| %> | |
<% if @picture.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@picture.errors.count, "error") %> prohibited this picture from being saved:</h2> | |
<ul> | |
<% @picture.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :title %><br> | |
<%= f.text_field :title %> | |
</div> | |
<div class="field"> | |
<%= f.label :image %><br> | |
<%= f.file_field :image %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/carrierwave.rb | |
CarrierWave.configure do |config| | |
config.storage = :fog | |
config.fog_credentials = { | |
provider: 'openstack', | |
openstack_tenant: "Framgia Picture Show", | |
openstack_api_key: "password", | |
openstack_username: "username", | |
openstack_auth_url: "http://192.168.1.107:35357/v2.0/tokens" | |
} | |
config.fog_directory = "framgia-pictures" | |
config.fog_public = false | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' | |
gem 'rails', '4.0.3' | |
gem 'sqlite3' | |
gem "rmagick" | |
gem "carrierwave" | |
gem "fog" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/uploaders/image_uploaders.rb | |
class ImageUploader < CarrierWave::Uploader::Base | |
include CarrierWave::RMagick | |
version :thumb do | |
process :resize_to_limit => [200, 200] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # app/views/pictures/index.html.erb %> | |
<h1>Listing pictures</h1> | |
<table> | |
<tbody> | |
<% @pictures.each do |picture| %> | |
<tr> | |
<td><h2><%= picture.title %></h2></td> | |
</tr> | |
<tr> | |
<td><%= image_tag picture.public_url(:thumb) %></td> | |
<td><%= link_to 'Show', picture %></td> | |
<td><%= link_to 'Edit', edit_picture_path(picture) %></td> | |
<td><%= link_to 'Destroy', picture, method: :delete, data: { confirm: 'Are you sure?' } %></td> | |
</tr> | |
<tr><td><hr /></td></tr> | |
<% end %> | |
</tbody> | |
</table> | |
<br> | |
<%= link_to 'New Picture', new_picture_path %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/picture.rb | |
class Picture < ActiveRecord::Base | |
mount_uploader :image, ImageUploader | |
def public_url thumb = false | |
file_name = thumb ? self.image.url(:thumb) : self.image.url | |
"http://192.168.1.107:8080/v1/AUTH_8684c73a2e814bdeb314f40c5830a708/framgia-pictures/#{file_name}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # app/views/pictures/show.html.erb %> | |
<p id="notice"><%= notice %></p> | |
<p> | |
<h2><%= @picture.title %></h2> | |
</p> | |
<p> | |
<%= image_tag @picture.public_url %> | |
</p> | |
<%= link_to 'Edit', edit_picture_path(@picture) %> | | |
<%= link_to 'Back', pictures_path %> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment