balloon 1.0.3 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +4 -0
- data/README.md +94 -40
- data/balloon.gemspec +25 -28
- data/examples/balloon.yml +0 -0
- data/examples/input.gif +0 -0
- data/examples/input.jpg +0 -0
- data/examples/upload.rb +19 -0
- data/examples/upload_gif.rb +24 -0
- data/lib/balloon/base.rb +6 -2
- data/lib/balloon/configuration.rb +4 -1
- data/lib/balloon/file_extension.rb +5 -1
- data/lib/balloon/load.rb +4 -2
- data/lib/balloon/processing.rb +108 -43
- data/lib/balloon/storage/file.rb +11 -3
- data/lib/balloon/storage/store.rb +20 -13
- data/lib/balloon/storage/upyun.rb +5 -2
- data/lib/balloon/up.rb +17 -12
- data/lib/balloon/uploader.rb +20 -15
- data/lib/balloon/version.rb +1 -1
- data/lib/balloon.rb +3 -3
- data/lib/rails/generators/balloon/config/config_generator.rb +1 -1
- metadata +18 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 909d8652d1b69823ed4a0a65e8802e5adf41fe37183111ad6d0f9e4035e7e882
|
4
|
+
data.tar.gz: 23e566f565e83f05d92740761ad7b9d1c6b851e73e038d23d9cac833ae09f483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99174822071a213f398c7fcec40c40f232852dce889f095241df9a7225099040cef5834104b878a1a41398231fad1624b29e286ca5b8063f5629cec017249459
|
7
|
+
data.tar.gz: 4c12e055ccc06b235e951b3edeb0f5adad4d04f58a2ff9a282827302199c1422bcb8f481884313143e9c32046e9599117e24fa6a9c4f6e6d8c92ceaf19d64b18
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# Balloon
|
2
2
|
|
3
3
|
|
4
|
-
Balloon是基于
|
4
|
+
Balloon是基于mini_magick的Rails项目图片上传插件。
|
5
|
+
|
5
6
|
|
6
7
|
|
7
8
|
## 安装
|
@@ -25,30 +26,47 @@ Balloon是基于Ruby的图片上传插件, 并且可以完美配合Rails使用
|
|
25
26
|
|
26
27
|
$ gem install balloon
|
27
28
|
|
29
|
+
|
30
|
+
|
28
31
|
## 单独使用
|
29
32
|
|
30
|
-
|
33
|
+
Class 例子
|
34
|
+
|
35
|
+
```ruby
|
31
36
|
require 'balloon'
|
32
37
|
|
33
38
|
Balloon.configure do |config|
|
34
39
|
config.store_storage = :file
|
40
|
+
config.root = "output"
|
35
41
|
end
|
36
42
|
|
37
43
|
class Upload < Balloon::Base
|
38
|
-
uploader :
|
39
|
-
|
40
|
-
|
44
|
+
uploader :image
|
45
|
+
uploader_dir 'uploads'
|
46
|
+
uploader_mimetype_white %w[image/jpeg image/png image/gif image/webp]
|
47
|
+
uploader_name_format name: "output", format: "upcase" # 输出文件名
|
48
|
+
uploader_type_format 'webp' # ImageMagick支持的类型
|
49
|
+
uploader_size thumb: "100x100", small: "200x", medium: '500x>' # https://legacy.imagemagick.org/Usage/resize/
|
41
50
|
end
|
51
|
+
```
|
52
|
+
|
53
|
+
文件上传
|
42
54
|
|
43
|
-
|
55
|
+
````ruby
|
56
|
+
file = File.new("input.jpg")
|
57
|
+
upload = Upload.new(file)
|
44
58
|
|
45
|
-
|
59
|
+
or
|
60
|
+
|
61
|
+
upload = Upload.new("input.jpg")
|
46
62
|
|
47
63
|
upload.upload_store #上传图片
|
64
|
+
upload.image #获取图片上传信息
|
48
65
|
|
49
66
|
upload.from_store(:t) #获得图片Path
|
67
|
+
````
|
68
|
+
|
50
69
|
|
51
|
-
```
|
52
70
|
|
53
71
|
## 在Rails中使用
|
54
72
|
|
@@ -56,7 +74,7 @@ upload.from_store(:t) #获得图片Path
|
|
56
74
|
在Rails应用程序中运行下述命令来完成Balloon插件的初始化
|
57
75
|
|
58
76
|
$ rails g balloon:config
|
59
|
-
|
77
|
+
|
60
78
|
在运行命令后, 会在config目录中生成一个balloon.yml配置文件
|
61
79
|
|
62
80
|
###### 默认生成balloon.yml文件
|
@@ -91,22 +109,7 @@ store_dir: 设置存储目录, 默认为主目录下的"public"目录
|
|
91
109
|
cache_dir: "tmp" # 设置临时文件储存目录, 默认为主目录下的“tmp”目录
|
92
110
|
```
|
93
111
|
|
94
|
-
###### 在为model文件添加下列格式
|
95
|
-
|
96
|
-
Mongomapper, Mongoid
|
97
112
|
|
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
113
|
|
111
114
|
model 配置介绍
|
112
115
|
|
@@ -123,7 +126,8 @@ uploader_name_format #对上传文件进行重命名,
|
|
123
126
|
```
|
124
127
|
|
125
128
|
|
126
|
-
|
129
|
+
|
130
|
+
#### ActiveRecord
|
127
131
|
|
128
132
|
先生成 model文件
|
129
133
|
|
@@ -131,16 +135,24 @@ ActiveRecord
|
|
131
135
|
|
132
136
|
并修改migration文件, 为下列格式
|
133
137
|
|
134
|
-
```
|
138
|
+
```ruby
|
135
139
|
|
136
140
|
class CreateImages < ActiveRecord::Migration
|
137
141
|
def change
|
138
142
|
create_table :pictures do |t|
|
143
|
+
t.string :file_id, null: false, index: true
|
139
144
|
t.string :file_name
|
145
|
+
t.integer :width
|
146
|
+
t.integer :height
|
140
147
|
t.string :content_type
|
141
|
-
t.
|
148
|
+
t.bigint :file_size
|
142
149
|
t.string :storage
|
143
|
-
|
150
|
+
|
151
|
+
# Postgresql 可以JSONB 作为metadata格式
|
152
|
+
t.jsonb :metadata
|
153
|
+
# Mysql和其它需要使用text
|
154
|
+
t.text :metadata
|
155
|
+
|
144
156
|
t.timestamps
|
145
157
|
end
|
146
158
|
end
|
@@ -148,29 +160,73 @@ end
|
|
148
160
|
|
149
161
|
```
|
150
162
|
|
151
|
-
|
163
|
+
model 文件
|
152
164
|
|
153
|
-
```
|
154
|
-
class
|
165
|
+
```ruby
|
166
|
+
class Picture < ActiveRecord::Base
|
155
167
|
include Balloon::Up
|
156
168
|
|
157
|
-
uploader :image
|
169
|
+
uploader :image, :db
|
170
|
+
uploader_dir 'uploads/images'
|
171
|
+
uploader_mimetype_white %w[image/jpeg image/png image/gif image/webp]
|
172
|
+
uploader_name_format name: proc { |img| img.file_id }
|
173
|
+
uploader_type_format 'webp'
|
174
|
+
uploader_size thumb: '150x', small: '450x>'
|
175
|
+
|
176
|
+
before_validation :create_file_id, on: :create
|
177
|
+
|
178
|
+
def create_file_id
|
179
|
+
self.file_id = generate_file_id
|
180
|
+
end
|
181
|
+
|
182
|
+
def generate_file_id
|
183
|
+
loop do
|
184
|
+
token = SecureRandom.hex
|
185
|
+
break token unless Picture.exists?(file_id: token)
|
186
|
+
end
|
187
|
+
end
|
158
188
|
end
|
159
189
|
|
160
190
|
```
|
161
191
|
|
162
|
-
|
192
|
+
|
193
|
+
|
194
|
+
#### Mongoid
|
195
|
+
|
196
|
+
```
|
197
|
+
class Image
|
198
|
+
include MongoMapper::Document
|
199
|
+
include Balloon::Up
|
200
|
+
|
201
|
+
uploader :image, :db
|
202
|
+
uploader_size t: "45", s: "450", m: "770"
|
203
|
+
uploader_dir "uploads/product"
|
204
|
+
uploader_mimetype_white %w{image/jpeg image/png image/gif}
|
205
|
+
uploader_name_format name: Proc.new{|p| p.id.to_s }
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
#### rails 实现图片上传
|
163
212
|
|
164
213
|
直接试用model原生操作, 用uploader设置的参数作为上传参数
|
165
|
-
|
166
|
-
@image = Images.new(image: file)
|
167
|
-
@image.save
|
168
214
|
|
169
|
-
|
215
|
+
```ruby
|
216
|
+
@picture = Picture.new(image: params[:image])
|
217
|
+
@picture.save
|
218
|
+
|
219
|
+
@picture.url #获取原图
|
220
|
+
@picture.url(size) #获得图片地址
|
221
|
+
```
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
##### 又拍云支持
|
170
226
|
|
171
227
|
将store_storage 修改为 ‘upyun’, 在config/balloon.yml内添加下列内容
|
172
228
|
|
173
|
-
```
|
229
|
+
```yaml
|
174
230
|
upyun_domain: ""
|
175
231
|
upyun_bucket: ""
|
176
232
|
upyun_username: ""
|
@@ -181,5 +237,3 @@ end
|
|
181
237
|
|
182
238
|
|
183
239
|
|
184
|
-
|
185
|
-
|
data/balloon.gemspec
CHANGED
@@ -1,42 +1,39 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require "balloon/version"
|
1
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
2
|
+
require 'balloon/version'
|
4
3
|
|
5
4
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
5
|
+
spec.name = 'balloon'
|
7
6
|
spec.version = Balloon::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
7
|
+
spec.authors = ['yeeli']
|
8
|
+
spec.email = ['yeeli@outlook.com']
|
10
9
|
|
11
|
-
spec.homepage =
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
10
|
+
spec.homepage = 'https://github.com/yeeli/balloon'
|
11
|
+
spec.summary = 'Ruby image upload libary'
|
12
|
+
spec.description = 'Ruby image upload libary'
|
14
13
|
|
15
|
-
spec.metadata[
|
16
|
-
spec.metadata[
|
17
|
-
spec.metadata[
|
14
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
15
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
16
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
18
17
|
|
19
|
-
|
20
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
19
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
20
|
end
|
23
|
-
spec.bindir =
|
21
|
+
spec.bindir = 'exe'
|
24
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
-
spec.require_paths = [
|
26
|
-
|
23
|
+
spec.require_paths = ['lib']
|
27
24
|
|
28
|
-
spec.add_dependency
|
29
|
-
spec.add_dependency
|
30
|
-
spec.add_dependency
|
31
|
-
spec.add_dependency
|
25
|
+
spec.add_dependency 'activesupport'
|
26
|
+
spec.add_dependency 'faraday'
|
27
|
+
spec.add_dependency 'mime-types'
|
28
|
+
spec.add_dependency 'mini_magick'
|
32
29
|
|
33
30
|
# Development dependency
|
34
31
|
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
spec.add_development_dependency
|
38
|
-
spec.add_development_dependency
|
39
|
-
spec.add_development_dependency
|
40
|
-
spec.add_development_dependency
|
41
|
-
spec.add_development_dependency
|
32
|
+
spec.add_development_dependency 'activerecord'
|
33
|
+
spec.add_development_dependency 'bson_ext'
|
34
|
+
spec.add_development_dependency 'mongoid'
|
35
|
+
spec.add_development_dependency 'mongo_mapper'
|
36
|
+
spec.add_development_dependency 'mysql2'
|
37
|
+
spec.add_development_dependency 'rails'
|
38
|
+
spec.add_development_dependency 'rspec'
|
42
39
|
end
|
File without changes
|
data/examples/input.gif
ADDED
Binary file
|
data/examples/input.jpg
ADDED
Binary file
|
data/examples/upload.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../lib'))
|
2
|
+
require 'balloon'
|
3
|
+
|
4
|
+
Balloon.configure do |config|
|
5
|
+
config.store_storage = :file
|
6
|
+
config.root = "output"
|
7
|
+
end
|
8
|
+
|
9
|
+
class Upload < Balloon::Base
|
10
|
+
uploader :image
|
11
|
+
uploader_dir 'uploads'
|
12
|
+
uploader_mimetype_white %w[image/jpeg image/png image/gif image/webp]
|
13
|
+
uploader_name_format name: 'output', format: 'upcase'
|
14
|
+
uploader_type_format 'webp'
|
15
|
+
uploader_size thumb: '200x', small: '600x>', large: '800x>'
|
16
|
+
end
|
17
|
+
|
18
|
+
upload = Upload.new("input.jpg")
|
19
|
+
upload.upload_store
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../lib'))
|
2
|
+
require 'balloon'
|
3
|
+
require "mini_magick"
|
4
|
+
|
5
|
+
Balloon.configure do |config|
|
6
|
+
config.store_storage = :file
|
7
|
+
config.root = "output"
|
8
|
+
end
|
9
|
+
|
10
|
+
class Upload < Balloon::Base
|
11
|
+
uploader :image
|
12
|
+
uploader_dir 'uploads/images'
|
13
|
+
uploader_mimetype_white %w[image/jpeg image/png image/gif image/webp]
|
14
|
+
uploader_name_format name: proc { |img| img.file_name }
|
15
|
+
uploader_type_format 'webp'
|
16
|
+
uploader_size thumb: '100x100', small: '200x>'
|
17
|
+
|
18
|
+
def file_name
|
19
|
+
"output_gif"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
upload = Upload.new("input.gif")
|
24
|
+
upload.upload_store
|
data/lib/balloon/base.rb
CHANGED
@@ -9,6 +9,10 @@ module Balloon
|
|
9
9
|
@file = upload_file.is_a?(Hash) ? upload_file[:#{name}] : upload_file
|
10
10
|
end
|
11
11
|
|
12
|
+
def #{name}
|
13
|
+
@meta
|
14
|
+
end
|
15
|
+
|
12
16
|
def uploader_name
|
13
17
|
"#{name}".pluralize
|
14
18
|
end
|
@@ -28,8 +32,8 @@ module Balloon
|
|
28
32
|
uploader_file = upload_file.nil? ? @file : upload_file
|
29
33
|
save_to_cache(uploader_file)
|
30
34
|
store_info = storage_engine.store!
|
31
|
-
@
|
32
|
-
@
|
35
|
+
@meta[:filename] = store_info[:filename]
|
36
|
+
@meta[:basename] = store_info[:basename]
|
33
37
|
end
|
34
38
|
|
35
39
|
def from_store(size_name = nil)
|
@@ -1,7 +1,10 @@
|
|
1
1
|
module Balloon
|
2
2
|
module Configuration
|
3
3
|
|
4
|
-
STORAGE_EGINE = {
|
4
|
+
STORAGE_EGINE = {
|
5
|
+
file: "Balloon::Storage::File",
|
6
|
+
upyun: "Balloon::Storage::Upyun"
|
7
|
+
}
|
5
8
|
|
6
9
|
class << self
|
7
10
|
def included(base)
|
@@ -32,7 +32,8 @@ module Balloon
|
|
32
32
|
IMAGE_EXT_LIST = {
|
33
33
|
"image/gif" => "gif",
|
34
34
|
"image/jpeg" => "jpg",
|
35
|
-
"image/png" => "png"
|
35
|
+
"image/png" => "png",
|
36
|
+
"image/webp" => "webp"
|
36
37
|
}
|
37
38
|
|
38
39
|
def initialize(file, mime_type = nil)
|
@@ -133,6 +134,9 @@ module Balloon
|
|
133
134
|
end
|
134
135
|
end
|
135
136
|
|
137
|
+
def self.get_extension(mime_type)
|
138
|
+
IMAGE_EXT_LIST[mime_type]
|
139
|
+
end
|
136
140
|
|
137
141
|
# @todo What Change basename add extension
|
138
142
|
def save_to(new_path, permissions = nil, directory_permission = nil)
|
data/lib/balloon/load.rb
CHANGED
@@ -8,7 +8,8 @@ if defined?(Rails)
|
|
8
8
|
initializer "Baloon.configure_rails_initializeation" do
|
9
9
|
config_file = Rails.root.join('config/balloon.yml')
|
10
10
|
if config_file.file?
|
11
|
-
|
11
|
+
config_result = config_file.read
|
12
|
+
config = Psych.load_stream(ERB.new(config_result).result)[0]
|
12
13
|
Balloon.configure_load(config, Rails.env)
|
13
14
|
end
|
14
15
|
end
|
@@ -25,7 +26,8 @@ elsif defined?(Sinatra)
|
|
25
26
|
Balloon.root = root
|
26
27
|
config_file = File.join(root, 'config/balloon.yml')
|
27
28
|
if File.exist?(config_file)
|
28
|
-
|
29
|
+
config_result = File.read(config_file)
|
30
|
+
config = Psych.load_stream(ERB.new(config_result).result)[0]
|
29
31
|
Balloon.configure_load(config, env)
|
30
32
|
end
|
31
33
|
end
|
data/lib/balloon/processing.rb
CHANGED
@@ -4,71 +4,136 @@ module Balloon
|
|
4
4
|
module Processing
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
if
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
7
|
+
def image_processing(image)
|
8
|
+
data = {}
|
9
|
+
ext = image.extension
|
10
|
+
|
11
|
+
if respond_to?(:uploader_type_format)
|
12
|
+
ext = uploader_type_format
|
13
|
+
end
|
14
|
+
|
15
|
+
processed_img = handle_original(image, ext)
|
16
|
+
data[:original] = get_image_data(processed_img)
|
17
|
+
|
18
|
+
resized_img = handle_resize(image, ext)
|
19
|
+
data.merge!(resized_img)
|
20
|
+
|
21
|
+
mime_type = processed_img.mime_type
|
22
|
+
extension = FileExtension.get_extension(mime_type)
|
23
|
+
filename = "#{image.basename}.#{extension}"
|
24
|
+
|
25
|
+
return {
|
26
|
+
basename: image.basename,
|
27
|
+
width: processed_img.width,
|
28
|
+
height: processed_img.height,
|
29
|
+
size: processed_img.size,
|
30
|
+
filename: filename,
|
31
|
+
mime_type: mime_type,
|
32
|
+
extension: extension,
|
33
|
+
data: data
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_original(file, ext)
|
38
|
+
original_image = MiniMagick::Image.open(file.path)
|
39
|
+
convert = MiniMagick::Tool::Convert.new
|
40
|
+
convert << file.path
|
41
|
+
|
42
|
+
auto_orient!(original_image, convert)
|
43
|
+
convert.format ext
|
44
|
+
|
45
|
+
cache_file = File.join(cache_path, "#{file.basename}.#{ext}")
|
46
|
+
convert << cache_file
|
47
|
+
convert.call
|
48
|
+
|
49
|
+
return MiniMagick::Image.open(cache_file)
|
50
|
+
end
|
51
|
+
|
52
|
+
def handle_resize(file, ext)
|
53
|
+
return {} unless self.respond_to?(:uploader_size)
|
54
|
+
return {} if store_storage.to_s == "upyun" && upyun_is_image
|
55
|
+
data = {}
|
56
|
+
|
57
|
+
uploader_size.each do |size, o|
|
58
|
+
img = MiniMagick::Image.open(file.path)
|
59
|
+
raise ProcessError, "process error" unless img
|
60
|
+
|
61
|
+
convert = MiniMagick::Tool::Convert.new
|
62
|
+
convert << file.path
|
63
|
+
|
64
|
+
auto_orient!(img, convert)
|
65
|
+
convert.format ext
|
66
|
+
|
67
|
+
resize(convert, img, o)
|
68
|
+
cache_file = File.join(cache_path, "#{file.basename}_#{size}.#{ext}")
|
69
|
+
convert << cache_file
|
70
|
+
convert.call
|
71
|
+
|
72
|
+
processed_img = MiniMagick::Image.open(cache_file)
|
73
|
+
data[size] = get_image_data(processed_img)
|
20
74
|
end
|
21
|
-
|
75
|
+
|
76
|
+
return data
|
22
77
|
end
|
23
78
|
|
24
|
-
def resize(image, size)
|
79
|
+
def resize(convert, image, size)
|
25
80
|
width, height, symbol = size[:width], size[:height], size[:symbol]
|
81
|
+
|
26
82
|
if !symbol.empty? || width.match(/\%/) || height.match(/\%/)
|
27
83
|
if width == height
|
28
|
-
|
29
|
-
|
84
|
+
shave(convert, image)
|
85
|
+
convert.resize "#{width}"
|
30
86
|
else
|
31
|
-
|
87
|
+
convert.resize "#{width}x#{height}#{symbol}"
|
32
88
|
end
|
89
|
+
return
|
90
|
+
end
|
91
|
+
|
92
|
+
if width == height
|
93
|
+
shave(convert, image)
|
94
|
+
value = (width.to_f / image[:width].to_f) * 100
|
95
|
+
convert.resize "#{value}%"
|
96
|
+
return
|
97
|
+
end
|
98
|
+
|
99
|
+
if width.empty?
|
100
|
+
value = (height.to_f / image[:height].to_f) * 100
|
101
|
+
convert.resize "#{value}%"
|
102
|
+
elsif height.empty?
|
103
|
+
value = (width.to_f / image[:width].to_f) * 100
|
104
|
+
convert.resize "#{value}%"
|
33
105
|
else
|
34
|
-
|
35
|
-
image = shave(image)
|
36
|
-
value = (width.to_f / image[:width].to_f) * 100
|
37
|
-
image.resize "#{value}%"
|
38
|
-
else
|
39
|
-
if width.empty?
|
40
|
-
value = (height.to_f / image[:height].to_f) * 100
|
41
|
-
image.resize "#{value}%"
|
42
|
-
elsif height.empty?
|
43
|
-
value = (width.to_f / image[:width].to_f) * 100
|
44
|
-
image.resize "#{value}%"
|
45
|
-
else
|
46
|
-
image.resize "#{width}x#{height}"
|
47
|
-
end
|
48
|
-
end
|
106
|
+
convert.resize "#{width}x#{height}"
|
49
107
|
end
|
50
|
-
return image
|
51
108
|
end
|
52
109
|
|
53
|
-
def shave(image)
|
110
|
+
def shave(convert, image)
|
54
111
|
w, h = image[:width], image[:height]
|
112
|
+
|
55
113
|
if w > h
|
56
114
|
shave_off = ((w - h) / 2).round
|
57
|
-
|
58
|
-
|
59
|
-
shave_off = ((h - w) / 2).round
|
60
|
-
image.shave "0x#{shave_off}"
|
115
|
+
convert.shave "#{shave_off}x0"
|
116
|
+
return
|
61
117
|
end
|
62
|
-
|
118
|
+
|
119
|
+
shave_off = ((h - w) / 2).round
|
120
|
+
convert.shave "0x#{shave_off}"
|
63
121
|
end
|
64
122
|
|
65
|
-
def auto_orient!(img,
|
123
|
+
def auto_orient!(img, covert)
|
66
124
|
if img.exif["Orientation"].to_i > 1
|
67
|
-
|
68
|
-
img.write file
|
125
|
+
convert.auto_orient
|
69
126
|
end
|
70
127
|
end
|
71
128
|
|
129
|
+
def get_image_data(img)
|
130
|
+
return {
|
131
|
+
width: img.width,
|
132
|
+
height: img.height,
|
133
|
+
size: img.size
|
134
|
+
}
|
135
|
+
end
|
136
|
+
|
72
137
|
# parseing the size string
|
73
138
|
#
|
74
139
|
# @return [Hash] the options for string
|
data/lib/balloon/storage/file.rb
CHANGED
@@ -3,6 +3,7 @@ module Balloon
|
|
3
3
|
class File < Balloon::Storage::Store
|
4
4
|
def store!
|
5
5
|
_store_path = store_path
|
6
|
+
cache_meta = @uploader.cache_meta
|
6
7
|
|
7
8
|
if !::File.exists? _store_path
|
8
9
|
FileUtils.mkdir_p _store_path
|
@@ -10,17 +11,23 @@ module Balloon
|
|
10
11
|
end
|
11
12
|
|
12
13
|
original_file = set_upload_name
|
14
|
+
|
13
15
|
store_original_file = ::File.join _store_path, original_file
|
14
|
-
cache_original_file = ::File.join @uploader.cache_path,
|
16
|
+
cache_original_file = ::File.join @uploader.cache_path, cache_meta[:filename]
|
17
|
+
|
15
18
|
FileUtils.mv cache_original_file, store_original_file
|
16
19
|
|
17
20
|
if @uploader.respond_to?(:uploader_size)
|
18
21
|
@uploader.uploader_size.each do |s, o|
|
19
22
|
store_file = ::File.join _store_path, set_upload_name(s)
|
20
|
-
cache_file = ::File.join @uploader.cache_path,
|
23
|
+
cache_file = ::File.join @uploader.cache_path, cache_meta[:basename]+ "_#{s}"+"."+ cache_meta[:extension]
|
21
24
|
FileUtils.mv cache_file, store_file
|
22
25
|
end
|
23
26
|
end
|
27
|
+
|
28
|
+
# Remove cache path
|
29
|
+
FileUtils.remove_dir(@uploader.cache_path)
|
30
|
+
|
24
31
|
return { filename: original_file, basename: store_name}
|
25
32
|
end
|
26
33
|
|
@@ -55,7 +62,8 @@ module Balloon
|
|
55
62
|
end
|
56
63
|
|
57
64
|
def store_path
|
58
|
-
|
65
|
+
root_path = @uploader.root || "."
|
66
|
+
::File.expand_path ::File.join(root_path, @uploader.store_dir, upload_dir)
|
59
67
|
end
|
60
68
|
end
|
61
69
|
end
|
@@ -10,8 +10,9 @@ module Balloon
|
|
10
10
|
def retrieve!(size_name = nil); end
|
11
11
|
|
12
12
|
def upload_file
|
13
|
-
file_info = @uploader.
|
13
|
+
file_info = @uploader.meta
|
14
14
|
return {} if file_info.nil?
|
15
|
+
|
15
16
|
basename = file_info[:basename] || ""
|
16
17
|
extension = file_info[:extension] || ""
|
17
18
|
{ basename: basename, extension: extension }
|
@@ -22,24 +23,30 @@ module Balloon
|
|
22
23
|
end
|
23
24
|
|
24
25
|
def store_name
|
25
|
-
|
26
|
+
if !@uploader.respond_to?(:uploader_name_format)
|
27
|
+
return @uploader.cache_meta[:basename]
|
28
|
+
end
|
29
|
+
|
26
30
|
name_format = @uploader.uploader_name_format
|
27
31
|
name = name_format[:name]
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
name = name.upcase
|
32
|
-
else
|
33
|
-
name
|
32
|
+
|
33
|
+
if name_format[:format].to_s == "downcase"
|
34
|
+
return name.downcase
|
34
35
|
end
|
36
|
+
|
37
|
+
if name_format[:format].to_s == "upcase"
|
38
|
+
return name.upcase
|
39
|
+
end
|
40
|
+
|
41
|
+
name
|
35
42
|
end
|
36
43
|
|
37
44
|
def set_upload_name(size_name = nil )
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
store_name + "." + upload_file[:extension]
|
45
|
+
cache_meta = @uploader.cache_meta
|
46
|
+
if size_name
|
47
|
+
return store_name + "_#{size_name.to_s}" + "." + cache_meta[:extension]
|
42
48
|
end
|
49
|
+
store_name + "." + cache_meta[:extension]
|
43
50
|
end
|
44
51
|
|
45
52
|
def connection
|
@@ -49,7 +56,7 @@ module Balloon
|
|
49
56
|
conn = Http::Client.new(options[:url]) do |builder|
|
50
57
|
builder.headers = options[:headers]
|
51
58
|
builder.basic_auth(basic[:user], basic[:password]) if !basic.nil?
|
52
|
-
builder.token_auth(self, token) if !token.nil?
|
59
|
+
builder.token_auth(self, token) if !token.nil?
|
53
60
|
end
|
54
61
|
return conn
|
55
62
|
end
|
@@ -16,9 +16,11 @@ module Balloon
|
|
16
16
|
|
17
17
|
def store!
|
18
18
|
_store_path = store_path
|
19
|
+
cache_meta = @uploader.cache_meta
|
19
20
|
original_file = set_upload_name
|
21
|
+
|
20
22
|
store_original_file = ::File.join _store_path, original_file
|
21
|
-
cache_original_file = ::File.join @uploader.cache_path,
|
23
|
+
cache_original_file = ::File.join @uploader.cache_path, cache_meta[:filename]
|
22
24
|
file = ::File.new cache_original_file
|
23
25
|
response = connection.put(store_original_file, file.read, file.size)
|
24
26
|
raise "Connection errors" if response.nil?
|
@@ -26,12 +28,13 @@ module Balloon
|
|
26
28
|
if !@uploader.upyun_is_image && @uploader.respond_to?(:uploader_size)
|
27
29
|
@uploader.uploader_size.each do |s, o|
|
28
30
|
store_file = ::File.join _store_path, set_upload_name(s)
|
29
|
-
cache_file = ::File.join @uploader.cache_path,
|
31
|
+
cache_file = ::File.join @uploader.cache_path, cache_meta[:basename]+ "_#{s}"+"."+ cache_meta[:extension]
|
30
32
|
file = ::File.new cache_file
|
31
33
|
connection.put(store_file, file.read, file.size)
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
37
|
+
FileUtils.remove_dir(@uploader.cache_path)
|
35
38
|
return { filename: original_file, basename: store_name }
|
36
39
|
end
|
37
40
|
|
data/lib/balloon/up.rb
CHANGED
@@ -22,15 +22,15 @@ module Balloon
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def #{name}
|
25
|
-
@
|
25
|
+
@meta
|
26
26
|
end
|
27
27
|
|
28
28
|
def uploader_save
|
29
|
-
return if
|
29
|
+
return if cache_meta.nil?
|
30
30
|
set_storage_engine
|
31
31
|
store_info = storage_engine.store!
|
32
|
-
@
|
33
|
-
@
|
32
|
+
@meta[:filename] = store_info[:filename]
|
33
|
+
@meta[:basename] = store_info[:basename]
|
34
34
|
end
|
35
35
|
|
36
36
|
def uploader_name
|
@@ -51,6 +51,7 @@ module Balloon
|
|
51
51
|
key :content_type, String
|
52
52
|
key :file_size, Integer
|
53
53
|
key :storage, String
|
54
|
+
key :metadata
|
54
55
|
key :created_at
|
55
56
|
elsif defined?(Mongoid)
|
56
57
|
field :file_name, type: String
|
@@ -59,6 +60,7 @@ module Balloon
|
|
59
60
|
field :content_type, type: String
|
60
61
|
field :file_size, type: String
|
61
62
|
field :storage, type: String
|
63
|
+
field :metadata
|
62
64
|
field :created_at
|
63
65
|
end
|
64
66
|
|
@@ -67,20 +69,23 @@ module Balloon
|
|
67
69
|
|
68
70
|
class_eval <<-RUBY
|
69
71
|
def save_db
|
70
|
-
return if
|
71
|
-
self.file_name =
|
72
|
-
self.content_type =
|
73
|
-
self.file_size =
|
72
|
+
return if meta.nil?
|
73
|
+
self.file_name = meta[:filename]
|
74
|
+
self.content_type = meta[:mime_type]
|
75
|
+
self.file_size = meta[:size]
|
74
76
|
self.storage = store_storage.to_s
|
75
|
-
self.width =
|
76
|
-
self.height =
|
77
|
+
self.width = meta[:width]
|
78
|
+
self.height = meta[:height]
|
79
|
+
if self.respond_to?(:metadata)
|
80
|
+
self.metadata = meta[:data]
|
81
|
+
end
|
77
82
|
end
|
78
83
|
|
79
84
|
def url(size_name = nil)
|
80
85
|
return "" if !respond_to?(:file_name) || file_name.nil?
|
81
86
|
extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
|
82
87
|
basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
|
83
|
-
@
|
88
|
+
@meta = { basename: basename, extension: extension.to_s }
|
84
89
|
set_storage_engine
|
85
90
|
storage_engine.retrieve!(size_name)
|
86
91
|
end
|
@@ -89,7 +94,7 @@ module Balloon
|
|
89
94
|
return if !respond_to?(:file_name) || file_name.nil?
|
90
95
|
extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
|
91
96
|
basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
|
92
|
-
@
|
97
|
+
@meta = { basename: basename, extension: extension.to_s }
|
93
98
|
set_storage_engine
|
94
99
|
storage_engine.delete!
|
95
100
|
end
|
data/lib/balloon/uploader.rb
CHANGED
@@ -9,7 +9,7 @@ module Balloon
|
|
9
9
|
include Balloon::Download
|
10
10
|
attr_accessor :file
|
11
11
|
attr_reader :storage_engine
|
12
|
-
attr_reader :
|
12
|
+
attr_reader :cache_meta, :meta
|
13
13
|
attr_accessor :download_error, :process_error
|
14
14
|
end
|
15
15
|
|
@@ -24,27 +24,27 @@ module Balloon
|
|
24
24
|
file_mime_type = uploader_file_ext.mime_type
|
25
25
|
|
26
26
|
if self.respond_to?(:uploader_mimetype_white)
|
27
|
-
if !uploader_mimetype_white.include?(file_mime_type)
|
27
|
+
if !uploader_mimetype_white.include?(file_mime_type)
|
28
28
|
raise Balloon::DownloadError, I18n.translate(:"errors.messages.down_mime_error")
|
29
29
|
end
|
30
|
-
|
31
|
-
|
30
|
+
end
|
31
|
+
|
32
|
+
if self.respond_to?(:uploader_mimetype_black)
|
33
|
+
if !uploader_mimetype_black.include?(file_mime_type)
|
32
34
|
raise Balloon::DownloadError, I18n.translate(:"errors.messages.down_mime_error")
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
38
|
generate_cache_directory
|
37
39
|
up_file = uploader_file_ext.save_to cache_path, permissions
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
basename: up_file.basename,
|
47
|
-
extension: up_file.extension
|
40
|
+
@cache_meta = image_processing up_file
|
41
|
+
@meta = {
|
42
|
+
width: @cache_meta[:width],
|
43
|
+
height: @cache_meta[:height],
|
44
|
+
size: @cache_meta[:size],
|
45
|
+
mime_type: @cache_meta[:mime_type],
|
46
|
+
extension: @cache_meta[:extension],
|
47
|
+
data: @cache_meta[:data]
|
48
48
|
}
|
49
49
|
end
|
50
50
|
|
@@ -71,16 +71,21 @@ module Balloon
|
|
71
71
|
|
72
72
|
def uploader_name_format(info)
|
73
73
|
define_method "uploader_name_format" do
|
74
|
-
name = info[:name].
|
74
|
+
name = info[:name].is_a?(Proc) ? info[:name].call(self) : info[:name].to_s
|
75
75
|
{ name: name, format: info[:format] }
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
+
def uploader_type_format(ext)
|
80
|
+
define_method "uploader_type_format" do; ext; end
|
81
|
+
end
|
82
|
+
|
79
83
|
def uploader_mimetype_white(list)
|
80
84
|
raise "just choise one method" if respond_to?(:uploader_mime_type_black)
|
81
85
|
define_method "uploader_mimetype_white" do; list; end
|
82
86
|
end
|
83
87
|
|
88
|
+
|
84
89
|
def uploader_mimetype_black(list)
|
85
90
|
raise "just choise one method" if respond_to?(:uploader_mime_type_black)
|
86
91
|
define_method "uploader_mimetype_black" do; list; end
|
data/lib/balloon/version.rb
CHANGED
data/lib/balloon.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
|
2
|
-
require "balloon/version"
|
1
|
+
require 'balloon/version'
|
3
2
|
require 'active_support'
|
4
3
|
require 'active_support/core_ext'
|
5
4
|
|
5
|
+
# Balloon
|
6
6
|
module Balloon
|
7
7
|
class << self
|
8
8
|
attr_accessor :root
|
9
9
|
def configure(&block)
|
10
|
-
Balloon::Base.configure &block
|
10
|
+
Balloon::Base.configure &block
|
11
11
|
end
|
12
12
|
|
13
13
|
def configure_load(config, env)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: balloon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yeeli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: faraday
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: mime-types
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: mini_magick
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: activerecord
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: bson_ext
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: mongoid
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
@@ -123,7 +123,7 @@ dependencies:
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
126
|
+
name: mysql2
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
129
|
- - ">="
|
@@ -137,7 +137,7 @@ dependencies:
|
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
|
-
name:
|
140
|
+
name: rails
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - ">="
|
@@ -151,7 +151,7 @@ dependencies:
|
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
|
-
name:
|
154
|
+
name: rspec
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
157
|
- - ">="
|
@@ -173,11 +173,17 @@ extra_rdoc_files: []
|
|
173
173
|
files:
|
174
174
|
- ".gitignore"
|
175
175
|
- ".rspec"
|
176
|
+
- ".rubocop.yml"
|
176
177
|
- Gemfile
|
177
178
|
- README.md
|
178
179
|
- README_EN.md
|
179
180
|
- Rakefile
|
180
181
|
- balloon.gemspec
|
182
|
+
- examples/balloon.yml
|
183
|
+
- examples/input.gif
|
184
|
+
- examples/input.jpg
|
185
|
+
- examples/upload.rb
|
186
|
+
- examples/upload_gif.rb
|
181
187
|
- lib/balloon.rb
|
182
188
|
- lib/balloon/base.rb
|
183
189
|
- lib/balloon/cache.rb
|
@@ -220,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
220
226
|
- !ruby/object:Gem::Version
|
221
227
|
version: '0'
|
222
228
|
requirements: []
|
223
|
-
rubygems_version: 3.
|
229
|
+
rubygems_version: 3.3.7
|
224
230
|
signing_key:
|
225
231
|
specification_version: 4
|
226
232
|
summary: Ruby image upload libary
|