balloon 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45b480476e4a3b0960af2ee96c45574e058f7169fa2edfd18d0f1d3972122d91
4
- data.tar.gz: d5f3cd96658c766738257b9c69101643059412bb01dcf45c518b24871192ad17
3
+ metadata.gz: cc46fceeceeb3953a4fb862908220119cc1948b056abc8cbe1e8ad4fa8092780
4
+ data.tar.gz: 15eb52b4210ccb0498ecdaa1711cf6b5b4ae0198245d4cf45a2fe63d36c00ea0
5
5
  SHA512:
6
- metadata.gz: 694689019e188d7994f986121d7ee2c6e87ab4102675d809235150eb2382e0b81501855d25c04018c00c10a71b73ed5d284ee786dbb98c4bbdac0b652ac54868
7
- data.tar.gz: d01efb4244b3003e0255d68d45a9c11ddca489aedf86b35e4cabd3032257baad2973318f48a4a018683de14dd7432f502232ce52d38ae9cb5fa0ed5d642cf10a
6
+ metadata.gz: 9d72137b38be30a8258aff896766de9e24aac3462cb3d760975a1eab78ba4c6251776416619201915238d9f0185439432a2fd6de124d8314a508d96505f29374
7
+ data.tar.gz: f9046e55e3387ecd448b0b5caaf3f38e41decf4e41f50d9f5d70545e93c46f89c62f7295353bbb362b45c16cf0e2eb9b4013162a5c3ff86f23cefbc7dae949f1
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ spec/fixtures/tmp
8
8
  spec/fixtures/public
9
9
  spec/fixtures/cache
10
10
  spec/config/balloon.yml
11
+ examples/output
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # Balloon
2
2
 
3
3
 
4
- Balloon是基于Ruby的图片上传插件, 并且可以完美配合Rails使用。
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 :avatar
39
- uploader_size t: "100x100"
40
- uploader_name_format
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
- file = File.new("file.jpg")
55
+ ````ruby
56
+ file = File.new("input.jpg")
57
+ upload = Upload.new(file)
44
58
 
45
- upload = Upload.new("file")
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
- ActiveRecord
129
+
130
+ #### ActiveRecord
127
131
 
128
132
  先生成 model文件
129
133
 
@@ -131,12 +135,15 @@ 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
148
  t.integer :file_size
142
149
  t.string :storage
@@ -148,29 +155,73 @@ end
148
155
 
149
156
  ```
150
157
 
151
- model 文件
158
+ model 文件
152
159
 
153
- ```
154
- class Image < ActiveRecord::Base
160
+ ```ruby
161
+ class Picture < ActiveRecord::Base
155
162
  include Balloon::Up
156
163
 
157
- uploader :image
164
+ uploader :image, :db
165
+ uploader_dir 'uploads/images'
166
+ uploader_mimetype_white %w[image/jpeg image/png image/gif image/webp]
167
+ uploader_name_format name: proc { |img| img.file_id }
168
+ uploader_type_format 'webp'
169
+ uploader_size thumb: '150x', small: '450x>'
170
+
171
+ before_validation :create_file_id, on: :create
172
+
173
+ def create_file_id
174
+ self.file_id = generate_file_id
175
+ end
176
+
177
+ def generate_file_id
178
+ loop do
179
+ token = SecureRandom.hex
180
+ break token unless Picture.exists?(file_id: token)
181
+ end
182
+ end
158
183
  end
159
184
 
160
185
  ```
161
186
 
162
- ###### rails实现图片上传
187
+
188
+
189
+ #### Mongoid
190
+
191
+ ```
192
+ class Image
193
+ include MongoMapper::Document
194
+ include Balloon::Up
195
+
196
+ uploader :image, :db
197
+ uploader_size t: "45", s: "450", m: "770"
198
+ uploader_dir "uploads/product"
199
+ uploader_mimetype_white %w{image/jpeg image/png image/gif}
200
+ uploader_name_format name: Proc.new{|p| p.id.to_s }
201
+ end
202
+ ```
203
+
204
+
205
+
206
+ #### rails 实现图片上传
163
207
 
164
208
  直接试用model原生操作, 用uploader设置的参数作为上传参数
165
-
166
- @image = Images.new(image: file)
167
- @image.save
168
209
 
169
- ###### 又拍云支持
210
+ ```ruby
211
+ @picture = Picture.new(image: params[:image])
212
+ @picture.save
213
+
214
+ @picture.url #获取原图
215
+ @picture.url(size) #获得图片地址
216
+ ```
217
+
218
+
219
+
220
+ ##### 又拍云支持
170
221
 
171
222
  将store_storage 修改为 ‘upyun’, 在config/balloon.yml内添加下列内容
172
223
 
173
- ```
224
+ ```yaml
174
225
  upyun_domain: ""
175
226
  upyun_bucket: ""
176
227
  upyun_username: ""
@@ -181,5 +232,3 @@ end
181
232
 
182
233
 
183
234
 
184
-
185
-
data/balloon.gemspec CHANGED
@@ -1,42 +1,39 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
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 = "balloon"
5
+ spec.name = 'balloon'
7
6
  spec.version = Balloon::VERSION
8
- spec.authors = ["yeeli"]
9
- spec.email = ["yeeli@outlook.com"]
7
+ spec.authors = ['yeeli']
8
+ spec.email = ['yeeli@outlook.com']
10
9
 
11
- spec.homepage = "https://github.com/yeeli/balloon"
12
- spec.summary = %q{Ruby image upload libary}
13
- spec.description = %q{Ruby image upload libary}
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["homepage_uri"] = spec.homepage
16
- spec.metadata["source_code_uri"] = spec.homepage
17
- spec.metadata["changelog_uri"] = spec.homepage
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 = "exe"
21
+ spec.bindir = 'exe'
24
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
- spec.require_paths = ["lib"]
26
-
23
+ spec.require_paths = ['lib']
27
24
 
28
- spec.add_dependency "activesupport"
29
- spec.add_dependency "mini_magick"
30
- spec.add_dependency "faraday"
31
- spec.add_dependency "mime-types"
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 "rails"
36
- spec.add_development_dependency "rspec"
37
- spec.add_development_dependency "mysql2"
38
- spec.add_development_dependency "mongo_mapper"
39
- spec.add_development_dependency "mongoid"
40
- spec.add_development_dependency "bson_ext"
41
- spec.add_development_dependency "activerecord"
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
Binary file
Binary file
@@ -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
- @info[:filename] = store_info[:filename]
32
- @info[:basename] = store_info[:basename]
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 = { file: "Balloon::Storage::File", upyun: "Balloon::Storage::Upyun" }
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)
@@ -6,7 +6,7 @@ module Balloon
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  def down_url(uri)
9
- connection = ::Faraday.new
9
+ connection = ::Faraday.new({:ssl => {:verify => false}})
10
10
  response = connection.get(uri)
11
11
  generate_down_cache_directory
12
12
  path = File.join down_cache_path, generate_down_id
@@ -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
- config = YAML.load(ERB.new(config_file.read).result)
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
- config = YAML.load(ERB.new(File.read(config_file)).result)
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
@@ -4,68 +4,118 @@ module Balloon
4
4
  module Processing
5
5
  extend ActiveSupport::Concern
6
6
 
7
- def resize_with_string(file)
8
- width, height = "", ""
9
- oranginl_img = MiniMagick::Image.open(file.path)
10
- auto_orient!(oranginl_img, file.path)
11
- if self.respond_to?(:uploader_size) && !(store_storage.to_s == "upyun" && upyun_is_image)
12
- uploader_size.each do |s, o|
13
- img = MiniMagick::Image.open(file.path)
14
- raise ProcessError, "process error" unless img
15
- width = img[:width]
16
- height = img[:height]
17
- new_img = resize(img, o)
18
- new_img.write File.join(cache_path, "#{file.basename}_#{s}.#{file.extension}")
19
- end
7
+ def image_processing(image)
8
+ ext = image.extension
9
+
10
+ if respond_to?(:uploader_type_format)
11
+ ext = uploader_type_format
20
12
  end
21
- return {width: oranginl_img[:width], height: oranginl_img[:height]}
13
+
14
+ processed_img = handle_original(image, ext)
15
+
16
+ handle_resize(image, ext)
17
+
18
+ mime_type = processed_img.mime_type
19
+ extension = FileExtension.get_extension(mime_type)
20
+ filename = "#{image.basename}.#{extension}"
21
+
22
+ return {
23
+ basename: image.basename,
24
+ width: processed_img.width,
25
+ height: processed_img.height,
26
+ size: processed_img.size,
27
+ filename: filename,
28
+ mime_type: mime_type,
29
+ extension: extension
30
+ }
31
+ end
32
+
33
+ def handle_original(file, ext)
34
+ original_image = MiniMagick::Image.open(file.path)
35
+ convert = MiniMagick::Tool::Convert.new
36
+ convert << file.path
37
+
38
+ auto_orient!(original_image, convert)
39
+ convert.format ext
40
+
41
+ cache_file = File.join(cache_path, "#{file.basename}.#{ext}")
42
+ convert << cache_file
43
+ convert.call
44
+
45
+ return MiniMagick::Image.open(cache_file)
22
46
  end
23
47
 
24
- def resize(image, size)
48
+ def handle_resize(file, ext)
49
+ return unless self.respond_to?(:uploader_size)
50
+ return if store_storage.to_s == "upyun" && upyun_is_image
51
+
52
+ uploader_size.each do |size, o|
53
+ img = MiniMagick::Image.open(file.path)
54
+ raise ProcessError, "process error" unless img
55
+
56
+ convert = MiniMagick::Tool::Convert.new
57
+ convert << file.path
58
+
59
+ auto_orient!(img, convert)
60
+ convert.format ext
61
+
62
+ resize(convert, img, o)
63
+ cache_file = File.join(cache_path, "#{file.basename}_#{size}.#{ext}")
64
+ convert << cache_file
65
+ convert.call
66
+
67
+ # img.write File.join(cache_path, "#{file.basename}_#{size}.#{ext}")
68
+ end
69
+ end
70
+
71
+ def resize(convert, image, size)
25
72
  width, height, symbol = size[:width], size[:height], size[:symbol]
73
+
26
74
  if !symbol.empty? || width.match(/\%/) || height.match(/\%/)
27
75
  if width == height
28
- image = shave(image)
29
- image.resize "#{width}"
76
+ shave(convert, image)
77
+ convert.resize "#{width}"
30
78
  else
31
- image.resize "#{width}x#{height}#{symbol}"
79
+ convert.resize "#{width}x#{height}#{symbol}"
32
80
  end
81
+
82
+ return
83
+ end
84
+
85
+ if width == height
86
+ shave(convert, image)
87
+ value = (width.to_f / image[:width].to_f) * 100
88
+ convert.resize "#{value}%"
33
89
  else
34
- if width == height
35
- image = shave(image)
36
- value = (width.to_f / image[:width].to_f) * 100
37
- image.resize "#{value}%"
90
+ if width.empty?
91
+ value = (height.to_f / image[:height].to_f) * 100
92
+ convert.resize "#{value}%"
93
+ elsif height.empty?
94
+ value = (width.to_f / image[:width].to_f) * 100
95
+ convert.resize "#{value}%"
38
96
  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
97
+ convert.resize "#{width}x#{height}"
48
98
  end
49
99
  end
50
- return image
100
+
101
+ return
51
102
  end
52
103
 
53
- def shave(image)
104
+ def shave(convert, image)
54
105
  w, h = image[:width], image[:height]
106
+
55
107
  if w > h
56
108
  shave_off = ((w - h) / 2).round
57
- image.shave "#{shave_off}x0"
109
+ convert.shave "#{shave_off}x0"
58
110
  else
59
111
  shave_off = ((h - w) / 2).round
60
- image.shave "0x#{shave_off}"
112
+ convert.shave "0x#{shave_off}"
61
113
  end
62
- return image
63
114
  end
64
115
 
65
- def auto_orient!(img, file)
116
+ def auto_orient!(img, covert)
66
117
  if img.exif["Orientation"].to_i > 1
67
- img.auto_orient
68
- img.write file
118
+ convert.auto_orient
69
119
  end
70
120
  end
71
121
 
@@ -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, @uploader.info[:filename]
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, @uploader.info[:basename]+ "_#{s}"+"."+ @uploader.info[:extension]
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
- ::File.expand_path ::File.join(@uploader.root, @uploader.store_dir, upload_dir)
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.info
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
- return upload_file[:basename] if !@uploader.respond_to?(:uploader_name_format)
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
- if name_format[:format].to_s == "downcase"
29
- name = name.downcase
30
- elsif name_format[:format].to_s == "upcase"
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
- if size_name
39
- store_name + "_#{size_name.to_s}" + "." + upload_file[:extension]
40
- else
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, @uploader.info[:filename]
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, @uploader.info[:basename]+ "_#{s}"+"."+ @uploader.info[:extension]
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
- @info
25
+ @meta
26
26
  end
27
27
 
28
28
  def uploader_save
29
- return if info.nil?
29
+ return if cache_meta.nil?
30
30
  set_storage_engine
31
31
  store_info = storage_engine.store!
32
- @info[:filename] = store_info[:filename]
33
- @info[:basename] = store_info[:basename]
32
+ @meta[:filename] = store_info[:filename]
33
+ @meta[:basename] = store_info[:basename]
34
34
  end
35
35
 
36
36
  def uploader_name
@@ -67,20 +67,20 @@ module Balloon
67
67
 
68
68
  class_eval <<-RUBY
69
69
  def save_db
70
- return if info.nil?
71
- self.file_name = info[:filename]
72
- self.content_type = info[:mime_type]
73
- self.file_size = info[:size]
70
+ return if meta.nil?
71
+ self.file_name = meta[:filename]
72
+ self.content_type = meta[:mime_type]
73
+ self.file_size = meta[:size]
74
74
  self.storage = store_storage.to_s
75
- self.width = info[:width]
76
- self.height = info[:height]
75
+ self.width = meta[:width]
76
+ self.height = meta[:height]
77
77
  end
78
78
 
79
79
  def url(size_name = nil)
80
80
  return "" if !respond_to?(:file_name) || file_name.nil?
81
81
  extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
82
82
  basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
83
- @info = { basename: basename, extension: extension.to_s }
83
+ @meta = { basename: basename, extension: extension.to_s }
84
84
  set_storage_engine
85
85
  storage_engine.retrieve!(size_name)
86
86
  end
@@ -89,7 +89,7 @@ module Balloon
89
89
  return if !respond_to?(:file_name) || file_name.nil?
90
90
  extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
91
91
  basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
92
- @info = { basename: basename, extension: extension.to_s }
92
+ @meta = { basename: basename, extension: extension.to_s }
93
93
  set_storage_engine
94
94
  storage_engine.delete!
95
95
  end
@@ -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 :info
12
+ attr_reader :cache_meta, :meta
13
13
  attr_accessor :download_error, :process_error
14
14
  end
15
15
 
@@ -27,7 +27,9 @@ module Balloon
27
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
- elsif self.respond_to?(:uploader_mimetype_black)
30
+ end
31
+
32
+ if self.respond_to?(:uploader_mimetype_black)
31
33
  if !uploader_mimetype_black.include?(file_mime_type)
32
34
  raise Balloon::DownloadError, I18n.translate(:"errors.messages.down_mime_error")
33
35
  end
@@ -35,16 +37,13 @@ module Balloon
35
37
 
36
38
  generate_cache_directory
37
39
  up_file = uploader_file_ext.save_to cache_path, permissions
38
- uploader_file = up_file
39
- img = resize_with_string up_file
40
- @info = {
41
- width: img[:width],
42
- height: img[:height],
43
- size: up_file.size,
44
- mime_type: up_file.mime_type,
45
- filename: up_file.filename,
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]
48
47
  }
49
48
  end
50
49
 
@@ -76,11 +75,16 @@ module Balloon
76
75
  end
77
76
  end
78
77
 
78
+ def uploader_type_format(ext)
79
+ define_method "uploader_type_format" do; ext; end
80
+ end
81
+
79
82
  def uploader_mimetype_white(list)
80
83
  raise "just choise one method" if respond_to?(:uploader_mime_type_black)
81
84
  define_method "uploader_mimetype_white" do; list; end
82
85
  end
83
86
 
87
+
84
88
  def uploader_mimetype_black(list)
85
89
  raise "just choise one method" if respond_to?(:uploader_mime_type_black)
86
90
  define_method "uploader_mimetype_black" do; list; end
@@ -1,3 +1,3 @@
1
1
  module Balloon
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/balloon.rb CHANGED
@@ -1,13 +1,13 @@
1
- #coding:utf-8
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.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yeeli
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-26 00:00:00.000000000 Z
11
+ date: 2022-05-07 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: mini_magick
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: faraday
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: mime-types
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: rails
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: rspec
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: mysql2
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: mongoid
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: bson_ext
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: activerecord
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
@@ -205,7 +211,7 @@ metadata:
205
211
  homepage_uri: https://github.com/yeeli/balloon
206
212
  source_code_uri: https://github.com/yeeli/balloon
207
213
  changelog_uri: https://github.com/yeeli/balloon
208
- post_install_message:
214
+ post_install_message:
209
215
  rdoc_options: []
210
216
  require_paths:
211
217
  - lib
@@ -220,8 +226,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
226
  - !ruby/object:Gem::Version
221
227
  version: '0'
222
228
  requirements: []
223
- rubygems_version: 3.1.3
224
- signing_key:
229
+ rubygems_version: 3.3.7
230
+ signing_key:
225
231
  specification_version: 4
226
232
  summary: Ruby image upload libary
227
233
  test_files: []