balloon 1.1.0 → 1.1.3

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: cc46fceeceeb3953a4fb862908220119cc1948b056abc8cbe1e8ad4fa8092780
4
- data.tar.gz: 15eb52b4210ccb0498ecdaa1711cf6b5b4ae0198245d4cf45a2fe63d36c00ea0
3
+ metadata.gz: 7c867d95dbb5c4a0d87faf47ccaf9e893388b46abd615a8d9f98801241fb6bde
4
+ data.tar.gz: 6f950eec27013a0f257bae84e848269c5e0a2693d9795ac622f1be572d7c4d95
5
5
  SHA512:
6
- metadata.gz: 9d72137b38be30a8258aff896766de9e24aac3462cb3d760975a1eab78ba4c6251776416619201915238d9f0185439432a2fd6de124d8314a508d96505f29374
7
- data.tar.gz: f9046e55e3387ecd448b0b5caaf3f38e41decf4e41f50d9f5d70545e93c46f89c62f7295353bbb362b45c16cf0e2eb9b4013162a5c3ff86f23cefbc7dae949f1
6
+ metadata.gz: 82a9c414d16d7f2435e854568d1d6be4de522e4cda381a129d4ea5deba49c95ad6507098feb18528e369576e96bc13884bd694f688632c140b15d01a3c31510a
7
+ data.tar.gz: a6a4163b12edf1c43acd3bf42ea9646de72dd72644105a94a52b9cc3e4a912108519147aacd03fcb2239ee249d3d67795bdaeb674ca5d48093bffcb51a755376
data/README.md CHANGED
@@ -145,9 +145,14 @@ class CreateImages < ActiveRecord::Migration
145
145
  t.integer :width
146
146
  t.integer :height
147
147
  t.string :content_type
148
- t.integer :file_size
148
+ t.bigint :file_size
149
149
  t.string :storage
150
- t.datetime :created_at
150
+
151
+ # Postgresql 可以JSONB 作为metadata格式
152
+ t.jsonb :metadata
153
+ # Mysql和其它需要使用text
154
+ t.text :metadata
155
+
151
156
  t.timestamps
152
157
  end
153
158
  end
data/examples/upload.rb CHANGED
@@ -10,10 +10,13 @@ class Upload < Balloon::Base
10
10
  uploader :image
11
11
  uploader_dir 'uploads'
12
12
  uploader_mimetype_white %w[image/jpeg image/png image/gif image/webp]
13
- uploader_name_format name: "output", format: 'upcase'
13
+ uploader_name_format name: 'output', format: 'upcase'
14
14
  uploader_type_format 'webp'
15
15
  uploader_size thumb: '200x', small: '600x>', large: '800x>'
16
16
  end
17
17
 
18
18
  upload = Upload.new("input.jpg")
19
- upload.upload_store
19
+ p upload.upload_store
20
+ p upload.from_store
21
+ p upload.path
22
+ p upload.local_path
data/lib/balloon/base.rb CHANGED
@@ -40,6 +40,14 @@ module Balloon
40
40
  storage_engine.retrieve!(size_name)
41
41
  end
42
42
 
43
+ def path(size_name = nil)
44
+ storage_engine.path!(size_name)
45
+ end
46
+
47
+ def local_path(size_name = nil)
48
+ storage_engine.local_path!(size_name)
49
+ end
50
+
43
51
  def upload_delete
44
52
  storage_engine.delete!
45
53
  end
@@ -5,6 +5,7 @@ module Balloon
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  def image_processing(image)
8
+ data = {}
8
9
  ext = image.extension
9
10
 
10
11
  if respond_to?(:uploader_type_format)
@@ -12,8 +13,10 @@ module Balloon
12
13
  end
13
14
 
14
15
  processed_img = handle_original(image, ext)
16
+ data[:original] = get_image_data(processed_img)
15
17
 
16
- handle_resize(image, ext)
18
+ total_size = handle_resize(image, ext, data)
19
+ data[:total_size] = processed_img.size.to_i + total_size.to_i
17
20
 
18
21
  mime_type = processed_img.mime_type
19
22
  extension = FileExtension.get_extension(mime_type)
@@ -26,7 +29,8 @@ module Balloon
26
29
  size: processed_img.size,
27
30
  filename: filename,
28
31
  mime_type: mime_type,
29
- extension: extension
32
+ extension: extension,
33
+ data: data
30
34
  }
31
35
  end
32
36
 
@@ -41,13 +45,14 @@ module Balloon
41
45
  cache_file = File.join(cache_path, "#{file.basename}.#{ext}")
42
46
  convert << cache_file
43
47
  convert.call
44
-
48
+
45
49
  return MiniMagick::Image.open(cache_file)
46
50
  end
47
51
 
48
- def handle_resize(file, ext)
52
+ def handle_resize(file, ext, data)
49
53
  return unless self.respond_to?(:uploader_size)
50
54
  return if store_storage.to_s == "upyun" && upyun_is_image
55
+ total_size = 0
51
56
 
52
57
  uploader_size.each do |size, o|
53
58
  img = MiniMagick::Image.open(file.path)
@@ -64,12 +69,16 @@ module Balloon
64
69
  convert << cache_file
65
70
  convert.call
66
71
 
67
- # img.write File.join(cache_path, "#{file.basename}_#{size}.#{ext}")
72
+ processed_img = MiniMagick::Image.open(cache_file)
73
+ data[size] = get_image_data(processed_img)
74
+ total_size += processed_img.size
68
75
  end
76
+
77
+ return total_size
69
78
  end
70
79
 
71
80
  def resize(convert, image, size)
72
- width, height, symbol = size[:width], size[:height], size[:symbol]
81
+ width, height, symbol = size[:width], size[:height], size[:symbol]
73
82
 
74
83
  if !symbol.empty? || width.match(/\%/) || height.match(/\%/)
75
84
  if width == height
@@ -78,7 +87,6 @@ module Balloon
78
87
  else
79
88
  convert.resize "#{width}x#{height}#{symbol}"
80
89
  end
81
-
82
90
  return
83
91
  end
84
92
 
@@ -86,19 +94,18 @@ module Balloon
86
94
  shave(convert, image)
87
95
  value = (width.to_f / image[:width].to_f) * 100
88
96
  convert.resize "#{value}%"
89
- else
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}%"
96
- else
97
- convert.resize "#{width}x#{height}"
98
- end
97
+ return
99
98
  end
100
99
 
101
- return
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}%"
106
+ else
107
+ convert.resize "#{width}x#{height}"
108
+ end
102
109
  end
103
110
 
104
111
  def shave(convert, image)
@@ -107,10 +114,11 @@ module Balloon
107
114
  if w > h
108
115
  shave_off = ((w - h) / 2).round
109
116
  convert.shave "#{shave_off}x0"
110
- else
111
- shave_off = ((h - w) / 2).round
112
- convert.shave "0x#{shave_off}"
113
- end
117
+ return
118
+ end
119
+
120
+ shave_off = ((h - w) / 2).round
121
+ convert.shave "0x#{shave_off}"
114
122
  end
115
123
 
116
124
  def auto_orient!(img, covert)
@@ -119,6 +127,14 @@ module Balloon
119
127
  end
120
128
  end
121
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
+
122
138
  # parseing the size string
123
139
  #
124
140
  # @return [Hash] the options for string
@@ -31,11 +31,21 @@ module Balloon
31
31
  return { filename: original_file, basename: store_name}
32
32
  end
33
33
 
34
- def retrieve!(size_name = nil)
34
+ def path!(size_name = nil)
35
35
  return "" if !upload_file
36
36
  path = ::File.join upload_dir, store_filename(size_name)
37
- return "/" + path if @uploader.asset_host.nil?
38
- @uploader.asset_host + "/" + path
37
+ return "/#{path}"
38
+ end
39
+
40
+ def local_path!(size_name = nil)
41
+ path = ::File.join @uploader.store_dir, path!(size_name)
42
+ return ::File.expand_path(::File.join(@uploader.root, path))
43
+ end
44
+
45
+ def retrieve!(size_name = nil)
46
+ path = path!(size_name)
47
+ return path if @uploader.asset_host.nil?
48
+ return "#{@uploader.asset_host}#{path}"
39
49
  end
40
50
 
41
51
  def delete!
data/lib/balloon/up.rb CHANGED
@@ -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
 
@@ -74,6 +76,9 @@ module Balloon
74
76
  self.storage = store_storage.to_s
75
77
  self.width = meta[:width]
76
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)
@@ -85,6 +90,24 @@ module Balloon
85
90
  storage_engine.retrieve!(size_name)
86
91
  end
87
92
 
93
+ def path(size_name = nil)
94
+ return "" if !respond_to?(:file_name) || file_name.nil?
95
+ extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
96
+ basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
97
+ @meta = { basename: basename, extension: extension.to_s }
98
+ set_storage_engine
99
+ storage_engine.path!(size_name)
100
+ end
101
+
102
+ def local_path(size_name = nil)
103
+ return "" if !respond_to?(:file_name) || file_name.nil?
104
+ extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
105
+ basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
106
+ @meta = { basename: basename, extension: extension.to_s }
107
+ set_storage_engine
108
+ storage_engine.local_path!(size_name)
109
+ end
110
+
88
111
  def uploader_delete
89
112
  return if !respond_to?(:file_name) || file_name.nil?
90
113
  extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
@@ -24,13 +24,13 @@ 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
30
  end
31
-
31
+
32
32
  if self.respond_to?(:uploader_mimetype_black)
33
- if !uploader_mimetype_black.include?(file_mime_type)
33
+ if !uploader_mimetype_black.include?(file_mime_type)
34
34
  raise Balloon::DownloadError, I18n.translate(:"errors.messages.down_mime_error")
35
35
  end
36
36
  end
@@ -43,7 +43,8 @@ module Balloon
43
43
  height: @cache_meta[:height],
44
44
  size: @cache_meta[:size],
45
45
  mime_type: @cache_meta[:mime_type],
46
- extension: @cache_meta[:extension]
46
+ extension: @cache_meta[:extension],
47
+ data: @cache_meta[:data]
47
48
  }
48
49
  end
49
50
 
@@ -70,7 +71,7 @@ module Balloon
70
71
 
71
72
  def uploader_name_format(info)
72
73
  define_method "uploader_name_format" do
73
- name = info[:name].nil? ? info[:name] : info[:name].call(self)
74
+ name = info[:name].is_a?(Proc) ? info[:name].call(self) : info[:name].to_s
74
75
  { name: name, format: info[:format] }
75
76
  end
76
77
  end
@@ -1,3 +1,3 @@
1
1
  module Balloon
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.3"
3
3
  end
@@ -2,7 +2,7 @@ module Balloon
2
2
  module Generators
3
3
  class ConfigGenerator < Rails::Generators::Base
4
4
  desc "create the Balloon configuration at config/balloon.yml"
5
-
5
+
6
6
  def self.source_root
7
7
  @source_root ||= File.expand_path("../templates", __FILE__)
8
8
  end
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.1.0
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - yeeli
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-07 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport