balloon 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9a840a4692178db52423213fcf6ead8d068f889
4
+ data.tar.gz: 514e31edc3e12d36b7622282e62bf726e80553d2
5
+ SHA512:
6
+ metadata.gz: 1747c58e04a86ab1a498686714535e271e6ad964f1451bb1e8c0f4050a0a14f44a6baf696163b00f11588ec347d7beb19be49e305001f87f525fcbe71a4c92e1
7
+ data.tar.gz: bd76ecc3b4244ca575d20f853b02e67575290464198543efcee59294484f770485174cea200e70368085bbdb3fb0e3286993e40cbe26607a0b6e033223447150
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .*.swp
6
+ .swp
7
+ spec/fixtures/tmp
8
+ spec/fixtures/public
9
+ spec/fixtures/cache
10
+ spec/config/balloon.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in balloon.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # Balloon
2
+
3
+
4
+ Balloon是基于Ruby的图片上传插件, 并且可以完美配合Rails使用。
5
+
6
+
7
+ ## 安装
8
+
9
+
10
+ 将下列文字添加到你程序中的Gemfile里
11
+
12
+ ```
13
+ gem 'balloon'
14
+
15
+ 或者
16
+
17
+ gem 'ballon', github: 'yeeli/balloon'
18
+ ```
19
+
20
+ 并执行:
21
+
22
+ $ bundle
23
+
24
+ 或者直接通过命令安装:
25
+
26
+ $ gem install balloon
27
+
28
+ ## 单独使用
29
+
30
+ ```
31
+ require 'balloon'
32
+
33
+ Balloon.configure do |config|
34
+ config.store_storage = :file
35
+ end
36
+
37
+ class Upload < Balloon::Base
38
+ uploader :avatar
39
+ uploader_size t: "100x100"
40
+ uploader_name_format
41
+ end
42
+
43
+ file = File.new("file.jpg")
44
+
45
+ upload = Upload.new("file")
46
+
47
+ upload.upload_store #上传图片
48
+
49
+ upload.from_store(:t) #获得图片Path
50
+
51
+ ```
52
+
53
+ ## 在Rails中使用
54
+
55
+
56
+ 在Rails应用程序中运行下述命令来完成Balloon插件的初始化
57
+
58
+ $ rails g balloon:config
59
+
60
+ 在运行命令后, 会在config目录中生成一个balloon.yml配置文件
61
+
62
+ ###### 默认生成balloon.yml文件
63
+
64
+ ````
65
+ defaults: &defaults
66
+ store_storage: 'file'
67
+
68
+ development:
69
+ <<: *defaults
70
+
71
+ test:
72
+ <<: *defaults
73
+
74
+ production:
75
+ <<: *defaults
76
+ ````
77
+
78
+ balloon.yml 配置介绍
79
+
80
+ ```
81
+ store_storage: 设置 文件储存位置, file: 文件储存, upyun: 又拍云存储
82
+
83
+ asset_host : 设置 asset
84
+
85
+ root: 设置主目录, 默认为当前应用程序的主目录
86
+
87
+ permissions: 设置生成文件目录权限, 默认为0777
88
+
89
+ store_dir: 设置存储目录, 默认为主目录下的"public"目录
90
+
91
+ cache_dir: "tmp" # 设置临时文件储存目录, 默认为主目录下的“tmp”目录
92
+ ```
93
+
94
+ ###### 在为model文件添加下列格式
95
+
96
+ Mongomapper, Mongoid
97
+
98
+ ```
99
+ class Image
100
+ include MongoMapper::Document
101
+ include Balloon::Up
102
+
103
+ uploader :image, :db
104
+ uploader_size t: "45", s: "450", m: "770"
105
+ uploader_dir "uploads/product"
106
+ uploader_mimetype_white %w{image/jpeg image/png image/gif}
107
+ uploader_name_format name: Proc.new{|p| p.id.to_s }
108
+ end
109
+ ```
110
+
111
+ model 配置介绍
112
+
113
+ ```
114
+ uploder 设置uploader名称, :db 后台生成数据key
115
+
116
+ uploader_size #设置文件裁切大小及文件名, 裁切格式参考
117
+
118
+ uploader_dir #设置上传目录, 未指定,默认为uploader设置的name为目录名
119
+
120
+ uploader_mimetype_white #设置上传白名单
121
+
122
+ uploader_name_format #对上传文件进行重命名,
123
+ ```
124
+
125
+
126
+ ActiveRecord
127
+
128
+ 先生成 model文件
129
+
130
+ $ rails g model image
131
+
132
+ 并修改migration文件, 为下列格式
133
+
134
+ ```
135
+
136
+ class CreateImages < ActiveRecord::Migration
137
+ def change
138
+ create_table :pictures do |t|
139
+ t.string :file_name
140
+ t.string :content_type
141
+ t.integer :file_size
142
+ t.string :storage
143
+ t.datetime :created_at
144
+ t.timestamps
145
+ end
146
+ end
147
+ end
148
+
149
+ ```
150
+
151
+ 在 model 文件
152
+
153
+ ```
154
+ class Image < ActiveRecord::Base
155
+ include Balloon::Up
156
+
157
+ uploader :image
158
+ end
159
+
160
+ ```
161
+
162
+ ###### rails实现图片上传
163
+
164
+ 直接试用model原生操作, 用uploader设置的参数作为上传参数
165
+
166
+ @image = Images.new(image: file)
167
+ @image.save
168
+
169
+ ###### 又拍云支持
170
+
171
+ 将store_storage 修改为 ‘upyun’, 在config/balloon.yml内添加下列内容
172
+
173
+ ```
174
+ upyun_domain: ""
175
+ upyun_bucket: ""
176
+ upyun_username: ""
177
+ upyun_password: ""
178
+ upyun_timeout: 600
179
+ upyun_is_image: true # true: 又拍云为图片空间Balloon将只上传原图, false: 又拍云为普通空间, 将会上传所有图片
180
+ ```
181
+
182
+
183
+
184
+
185
+
data/README_EN.md ADDED
@@ -0,0 +1,4 @@
1
+ Balloon
2
+ =======
3
+
4
+ This gem is a sample upload libary for ruby
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/balloon.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "balloon/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "balloon"
7
+ s.version = Balloon::VERSION
8
+ s.authors = ["yeeli"]
9
+ s.email = ["yeeli@outlook.com"]
10
+ s.homepage = ""
11
+ s.summary = "Ruby image upload libary"
12
+ s.description = %q{Ruby image upload libary}
13
+
14
+ s.rubyforge_project = "balloon"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+
22
+ s.add_dependency "activesupport"
23
+ s.add_dependency "mini_magick"
24
+ s.add_dependency "faraday"
25
+ s.add_dependency "mime-types"
26
+
27
+ #development
28
+ s.add_development_dependency "rails"
29
+ s.add_development_dependency "rspec"
30
+ s.add_development_dependency "mysql2"
31
+ s.add_development_dependency "mongo_mapper"
32
+ s.add_development_dependency "mongoid"
33
+ s.add_development_dependency "bson_ext"
34
+ s.add_development_dependency "activerecord"
35
+ end
data/lib/balloon.rb ADDED
@@ -0,0 +1,42 @@
1
+ #coding:utf-8
2
+ require "balloon/version"
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+
6
+ module Balloon
7
+ class << self
8
+ attr_accessor :root
9
+ def configure(&block)
10
+ Balloon::Base.configure &block
11
+ end
12
+
13
+ def configure_load(config, env)
14
+ Balloon::Base.setup config, env
15
+ end
16
+ end
17
+
18
+ autoload :Uploader, 'balloon/uploader'
19
+ autoload :Base, 'balloon/base'
20
+ autoload :Configuration, 'balloon/configuration'
21
+ autoload :Processing, 'balloon/processing'
22
+ autoload :Cache, 'balloon/cache'
23
+ autoload :FileExtension, 'balloon/file_extension'
24
+ autoload :Download, 'balloon/download'
25
+ autoload :Validate, 'balloon/validate'
26
+
27
+ module Http
28
+ autoload :Client, 'balloon/http/client'
29
+ autoload :Response, 'balloon/http/response'
30
+ end
31
+
32
+ module Storage
33
+ autoload :Store, 'balloon/storage/store'
34
+ autoload :File, 'balloon/storage/file'
35
+ autoload :Upyun, 'balloon/storage/upyun'
36
+ end
37
+
38
+ autoload :Up, 'balloon/up'
39
+ end
40
+
41
+ require 'balloon/load'
42
+ require 'balloon/error'
@@ -0,0 +1,43 @@
1
+ module Balloon
2
+ class Base
3
+ include Balloon::Uploader
4
+
5
+ def self.uploader(name)
6
+ class_eval <<-RUBY
7
+ def initialize(upload_file = nil)
8
+ set_storage_engine
9
+ @file = upload_file.is_a?(Hash) ? upload_file[:#{name}] : upload_file
10
+ end
11
+
12
+ def uploader_name
13
+ "#{name}".pluralize
14
+ end
15
+
16
+ def set_storage_engine
17
+ return if respond_to?(:@storage_engine)
18
+ @storage_engine = eval(STORAGE_EGINE[store_storage.to_sym]).new(self)
19
+ end
20
+ RUBY
21
+ end
22
+
23
+ # upload file save storage
24
+ #
25
+ # @param [File] upload_file the upload file
26
+ #
27
+ def upload_store(upload_file = nil)
28
+ uploader_file = upload_file.nil? ? @file : upload_file
29
+ save_to_cache(uploader_file)
30
+ store_info = storage_engine.store!
31
+ @info[:filename] = store_info[:filename]
32
+ @info[:basename] = store_info[:basename]
33
+ end
34
+
35
+ def from_store(size_name = nil)
36
+ storage_engine.retrieve!(size_name)
37
+ end
38
+
39
+ def upload_delete
40
+ storage_engine.delete!
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ module Balloon
2
+ module Cache
3
+ extend ActiveSupport::Concern
4
+
5
+ attr_reader :cache_cid
6
+
7
+ # generate cache directory in system
8
+ #
9
+ # @return
10
+ def generate_cache_directory
11
+ @cache_cid = generate_cid
12
+ FileUtils.mkdir_p(cache_path)
13
+ FileUtils.chmod dir_permissions.to_i, cache_path
14
+ end
15
+
16
+ def cache_path
17
+ File.expand_path cache_directory, root
18
+ end
19
+
20
+ def cache_directory
21
+ File.join cache_dir, "images", cache_cid
22
+ end
23
+
24
+ def generate_down_cache_directory
25
+ FileUtils.mkdir_p(down_cache_path)
26
+ FileUtils.chmod dir_permissions.to_i, down_cache_path
27
+ end
28
+
29
+ def down_cache_path
30
+ File.expand_path down_cache_directory, root
31
+ end
32
+
33
+ def down_cache_directory
34
+ File.join cache_dir, "net"
35
+ end
36
+
37
+ private
38
+
39
+ def generate_cid
40
+ now = Time.now
41
+ "#{now.to_i}.#{now.usec}.#{Process.pid}"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,85 @@
1
+ module Balloon
2
+ module Configuration
3
+
4
+ STORAGE_EGINE = { file: "Balloon::Storage::File", upyun: "Balloon::Storage::Upyun" }
5
+
6
+ class << self
7
+ def included(base)
8
+ base.extend ClassMethods
9
+ set_config :root
10
+ set_config :permissions
11
+ set_config :dir_permissions
12
+ set_config :store_storage
13
+ set_config :cache_dir
14
+ set_config :store_dir
15
+ set_config :asset_host
16
+
17
+ set_config :upyun_api_host
18
+ set_config :upyun_username
19
+ set_config :upyun_password
20
+ set_config :upyun_bucket
21
+ set_config :upyun_timeout
22
+ set_config :upyun_domain
23
+ set_config :upyun_thumb_symbol
24
+ set_config :upyun_is_image
25
+ set_config :upyun_headers
26
+
27
+ reset_config if base == Balloon::Base
28
+ end
29
+
30
+ def set_config(name)
31
+ ClassMethods.class_eval <<-RUBY
32
+ attr_writer :#{name}
33
+ alias :uploader_#{name} :#{name}=
34
+ def #{name}; @#{name}; end
35
+ RUBY
36
+
37
+ class_eval <<-RUBY
38
+ def #{name}
39
+ value = self.class.#{name}
40
+ value.nil? ? Balloon::Base.#{name} : value
41
+ end
42
+ RUBY
43
+ end
44
+
45
+ def reset_config
46
+ Balloon.configure do |config|
47
+ config.root = Balloon.root
48
+ config.permissions = 0644
49
+ config.dir_permissions = 0755
50
+ config.store_storage = :file
51
+ config.store_dir = "public"
52
+ config.cache_dir = "tmp"
53
+
54
+ config.upyun_headers = {}
55
+ config.upyun_thumb_symbol = '!'
56
+ config.upyun_is_image = false
57
+ end
58
+ end
59
+ end
60
+
61
+ module ClassMethods
62
+ def configure; yield self; end
63
+
64
+ def setup(configure, env)
65
+ conf = configure[env]
66
+ conf = configure['defaults'] if conf.nil?
67
+ Balloon.configure do |config|
68
+ conf.each do | n, v |
69
+ if !v.blank? || v ==true || v == false
70
+ if v ==true || v == false
71
+ class_eval <<-RUBY
72
+ config.#{n} = #{v}
73
+ RUBY
74
+ else
75
+ class_eval <<-RUBY
76
+ config.#{n} = '#{v}'
77
+ RUBY
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end