balloon 1.0.4 → 1.1.2
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/README.md +94 -40
- 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/processing.rb +111 -45
- 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/rails/generators/balloon/config/config_generator.rb +1 -1
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89bc6b2f3af3d0f77235b2ca38bc17851f0a3850a4742d14dfb462d5d32fb11d
|
4
|
+
data.tar.gz: f110eedc9a0e2ba8dde12785401755c0b4f5e264c707a4184dd35ca1a0e88aee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1228ab4477df6a100db7046cffb4436775e8f851db17009fdb18b90a645b4eeb2e7cdfc48cb6cdfaff32af44dc73f4c6ae33a4968b8cd0f583a622d715662097
|
7
|
+
data.tar.gz: 60473bbaf1ab5804eb39e23d6f2577fd475c445d82486605696e34b43026a67c5f5c7ab7cd05c04e1c646c51b9594daf28576b94d2336006bcda4c90d9477fd4
|
data/.gitignore
CHANGED
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
|
-
|
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/processing.rb
CHANGED
@@ -4,71 +4,137 @@ 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
|
+
total_size = handle_resize(image, ext, data)
|
19
|
+
data[:total_size] = processed_img.size.to_i + total_size.to_i
|
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, data)
|
53
|
+
return unless self.respond_to?(:uploader_size)
|
54
|
+
return if store_storage.to_s == "upyun" && upyun_is_image
|
55
|
+
total_size = 0
|
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)
|
74
|
+
total_size += processed_img.size
|
20
75
|
end
|
21
|
-
|
76
|
+
|
77
|
+
return total_size
|
22
78
|
end
|
23
79
|
|
24
|
-
def resize(image, size)
|
25
|
-
width, height, symbol = size[:width], size[:height], size[:symbol]
|
80
|
+
def resize(convert, image, size)
|
81
|
+
width, height, symbol = size[:width], size[:height], size[:symbol]
|
82
|
+
|
26
83
|
if !symbol.empty? || width.match(/\%/) || height.match(/\%/)
|
27
84
|
if width == height
|
28
|
-
|
29
|
-
|
85
|
+
shave(convert, image)
|
86
|
+
convert.resize "#{width}"
|
30
87
|
else
|
31
|
-
|
88
|
+
convert.resize "#{width}x#{height}#{symbol}"
|
32
89
|
end
|
90
|
+
return
|
91
|
+
end
|
92
|
+
|
93
|
+
if width == height
|
94
|
+
shave(convert, image)
|
95
|
+
value = (width.to_f / image[:width].to_f) * 100
|
96
|
+
convert.resize "#{value}%"
|
97
|
+
return
|
98
|
+
end
|
99
|
+
|
100
|
+
if width.empty?
|
101
|
+
value = (height.to_f / image[:height].to_f) * 100
|
102
|
+
convert.resize "#{value}%"
|
103
|
+
elsif height.empty?
|
104
|
+
value = (width.to_f / image[:width].to_f) * 100
|
105
|
+
convert.resize "#{value}%"
|
33
106
|
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
|
107
|
+
convert.resize "#{width}x#{height}"
|
49
108
|
end
|
50
|
-
return image
|
51
109
|
end
|
52
110
|
|
53
|
-
def shave(image)
|
111
|
+
def shave(convert, image)
|
54
112
|
w, h = image[:width], image[:height]
|
113
|
+
|
55
114
|
if w > h
|
56
115
|
shave_off = ((w - h) / 2).round
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
116
|
+
convert.shave "#{shave_off}x0"
|
117
|
+
return
|
118
|
+
end
|
119
|
+
|
120
|
+
shave_off = ((h - w) / 2).round
|
121
|
+
convert.shave "0x#{shave_off}"
|
63
122
|
end
|
64
123
|
|
65
|
-
def auto_orient!(img,
|
124
|
+
def auto_orient!(img, covert)
|
66
125
|
if img.exif["Orientation"].to_i > 1
|
67
|
-
|
68
|
-
img.write file
|
126
|
+
convert.auto_orient
|
69
127
|
end
|
70
128
|
end
|
71
129
|
|
130
|
+
def get_image_data(img)
|
131
|
+
return {
|
132
|
+
width: img.width,
|
133
|
+
height: img.height,
|
134
|
+
size: img.size
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
72
138
|
# parseing the size string
|
73
139
|
#
|
74
140
|
# @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
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yeeli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -179,6 +179,11 @@ files:
|
|
179
179
|
- README_EN.md
|
180
180
|
- Rakefile
|
181
181
|
- balloon.gemspec
|
182
|
+
- examples/balloon.yml
|
183
|
+
- examples/input.gif
|
184
|
+
- examples/input.jpg
|
185
|
+
- examples/upload.rb
|
186
|
+
- examples/upload_gif.rb
|
182
187
|
- lib/balloon.rb
|
183
188
|
- lib/balloon/base.rb
|
184
189
|
- lib/balloon/cache.rb
|
@@ -221,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
221
226
|
- !ruby/object:Gem::Version
|
222
227
|
version: '0'
|
223
228
|
requirements: []
|
224
|
-
rubygems_version: 3.3.
|
229
|
+
rubygems_version: 3.3.7
|
225
230
|
signing_key:
|
226
231
|
specification_version: 4
|
227
232
|
summary: Ruby image upload libary
|