upload_image_retain_low_quality 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59c64b1ff41b3cb21a09116110e3f8437b37847d
4
+ data.tar.gz: 698f16985c323da5d15d1208506794c6a0abb1eb
5
+ SHA512:
6
+ metadata.gz: d1f648a399b674b724ee077afdcc1dfb02a7fddb87b0955bbb05877a6de92d4b0f84068dd238fbffaae2867e28438331058046c1f24c7197ab30e0edac00f8da
7
+ data.tar.gz: ff9deb3770d44966e903ff3661f765e4a702255bb6c2ef3409ac82bb749aa8f2fdce0f57dc43179b30c02e1cde3250cea9fc44238842a294158eed7b9cfcdc57
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .upload_image_retain_low_qualityrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in upload_image_retain_low_quality.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 y.fujii
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # UploadImageRetainLowQuality
2
+ upload image to s3(Glacier) bucket retain low quality image file
3
+
4
+ ## Requirements
5
+
6
+ ImageMagick or GraphicsMagick command-line tool has to be installed.
7
+ because to use [minimagick](https://github.com/minimagick/minimagick)
8
+
9
+ ## Installation
10
+
11
+ install
12
+
13
+ $ gem install upload_image_retain_low_quality
14
+
15
+ ## Usage
16
+
17
+ ####First setting
18
+
19
+ $ upload_image_retain_low_quality init
20
+
21
+ ####Upload Image
22
+ create .upload_image_retain_low_qualityrc in your current directory
23
+ rewrite your aws key and secret
24
+
25
+ access_key_id: 'YOUR ACCESS KEY'
26
+ secret_access_key: 'SECRET ACCESS KEY'
27
+
28
+ move to direcory. then just do this.
29
+
30
+ $ upload_image_retain_low_quality
31
+
32
+ current directory image files(.jpg .jpeg .gif .png) convert low quality and transfer your interactive selected buckets and folder.
33
+
34
+
35
+ help
36
+
37
+ $ upload_image_retain_low_quality --help
38
+
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'upload_image_retain_low_quality'
4
+
5
+ params = ARGV.getopts('q:f:hd', 'quality:50', 'file_or_dir:./', 'help', 'delete')
6
+ params["quality"] = params["q"].nil? ? params["quality"] : params["q"]
7
+ params["file_or_dir"] = params["f"].nil? ? params["file_or_dir"] : params["d"]
8
+ params["help"] = params["h"].nil? ? params["help"] : params["h"]
9
+ params["delete"] = params["d"].nil? ? params["delete"] : params["d"]
10
+
11
+ # show help
12
+ if ARGV == ["init"]
13
+ # aws_congigを生成する処理
14
+ UploadImageRetainLowQuality::S3.generate_config
15
+ else
16
+ if params["help"]
17
+
18
+ puts <<"HELP_MES"
19
+ upload_image_retain_low_quality version: #{UploadImageRetainLowQuality::VERSION}
20
+
21
+ First Setting:
22
+ $ upload_image_retain_low_quality ini
23
+
24
+ .upload_image_retain_low_qualityrc is created in your current directory
25
+ open the file with editor
26
+
27
+ rewrire your key and secret
28
+ access_key_id: 'YOUR ACCESS KEY'
29
+ secret_access_key: 'SECRET ACCESS KEY'
30
+
31
+ Usage:
32
+ $ upload_image_retain_low_quality [-q --quality] [-f --file_or_dir] [-h --help] [-d --delete]
33
+
34
+ Options:
35
+ -q : set down quality percent(default: 50)
36
+ -f : set target directory or file(default: current directory)
37
+ -d : no backup original image file
38
+ -h : this help
39
+
40
+ HELP_MES
41
+
42
+ else
43
+ UploadImageRetainLowQuality::CLI.run(params)
44
+ end
45
+
46
+ end
47
+
48
+
@@ -0,0 +1,56 @@
1
+ require "upload_image_retain_low_quality/version"
2
+ require 'upload_image_retain_low_quality'
3
+ require 'upload_image_retain_low_quality/console'
4
+ require 'upload_image_retain_low_quality/image'
5
+ require 'upload_image_retain_low_quality/s3'
6
+
7
+ require "aws-sdk"
8
+ require "mini_magick"
9
+ require 'optparse'
10
+ require "yaml"
11
+ require 'parallel'
12
+
13
+ module UploadImageRetainLowQuality
14
+
15
+ class CLI
16
+ def self.run(params)
17
+ # bucketの選択
18
+ s3 = S3.new
19
+ all_buckets = Console.show_item_with_pointer(s3.buckets)
20
+ idx = Console.gets_from_stdin("select number your bucket")
21
+ s3.bucket = all_buckets[idx]
22
+
23
+ # ディレクトリの選択
24
+ while !s3.directories.empty? && !Console.get_y_or_n_from_stdin("update here?")
25
+ directories = Console.show_item_with_pointer(s3.directories)
26
+ idx = Console.gets_from_stdin("select number upload folder")
27
+ s3.cd(directories[idx])
28
+ end
29
+
30
+ image_upload_proc = ->(image){
31
+ if image && image.valid_ext?
32
+ quality_down_file = image.quality_down(params["quality"])
33
+ s3.upload_file(image.file, s3.current_path)
34
+ if params["delete"]
35
+ image.destroy!
36
+ else
37
+ image.backup!
38
+ end
39
+ end
40
+ }
41
+
42
+ if File::ftype(params["file_or_dir"]) == "directory"
43
+ images = Dir::entries(params["file_or_dir"]).map{|file| Image.new(file)}.select(&:valid_ext?)
44
+ else
45
+ images = [Image.new(params["file_or_dir"])]
46
+ end
47
+
48
+ idx = 0
49
+ Parallel.each(images, in_threads: Parallel.processor_count) do |file|
50
+ idx += 1
51
+ Console.progress_bar(idx, images.size)
52
+ image_upload_proc.call(file)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ module UploadImageRetainLowQuality
2
+ class Console
3
+ # Class Methods
4
+ class << self
5
+ def gets_from_stdin(message = nil)
6
+ print "#{message}: " unless message.nil?
7
+ str = STDIN.gets.chomp
8
+ end
9
+
10
+ def get_y_or_n_from_stdin(message)
11
+ bol = ""
12
+ while !%w(Y N).include?(bol.upcase)
13
+ bol = gets_from_stdin("#{message}(y/n)")
14
+ end
15
+ bol.upcase == "Y"
16
+ end
17
+
18
+ def show_item_with_pointer(iterator, console_print = true)
19
+ rtn_hash = {}
20
+ iterator.each_with_index do |value, idx|
21
+ rtn_hash[idx.to_s] = value
22
+ if console_print
23
+ if value.is_a?(AWS::S3::Bucket)
24
+ p "[" + (idx).to_s + "] " + value.name.to_s
25
+ else
26
+ p "[" + (idx).to_s + "] " + value
27
+ end
28
+ end
29
+ end
30
+
31
+ rtn_hash
32
+ end
33
+
34
+ def progress_bar(i, max = 100)
35
+ i = i.to_f
36
+ max = max.to_f
37
+ i = max if i > max
38
+ percent = i / max * 100.0
39
+ rest_size = 1 + 5 + 1 # space + progress_num + %
40
+ bar_size = 79 - rest_size # (width - 1) - rest_size
41
+ bar_str = '%-*s' % [bar_size, ('#' * (percent * bar_size / 100).to_i)]
42
+ progress_num = '%3.1f' % percent
43
+ print "\r#{bar_str} #{'%5s' % progress_num}%"
44
+ end
45
+
46
+ def print_green(str)
47
+ puts "\e[32m#{str}\e[0m"
48
+ end
49
+
50
+ def print_red(str)
51
+ puts "\e[31m#{str}\e[0m"
52
+ end
53
+ end # Class Methods End
54
+ end
55
+ end
@@ -0,0 +1,44 @@
1
+ module UploadImageRetainLowQuality
2
+ class Image
3
+ # 対象の拡張子
4
+ TARGET_EXT = %w(.jpg .jpeg .gif .png)
5
+ ORIGINAL_IMAGE_DIR = "original_image/"
6
+
7
+ def initialize(file)
8
+ return nil unless FileTest.file?(file)
9
+ @file = file
10
+ @ext = File.extname(file)
11
+ @base_name = File.basename(file, @ext)
12
+ end
13
+
14
+ def valid_ext?
15
+ TARGET_EXT.include?(@ext)
16
+ end
17
+
18
+ def file
19
+ @file
20
+ end
21
+
22
+ def quality_down(quality)
23
+ #p "#{@file} change quality #{quality}"
24
+ down_filename = "#{@base_name}_#{quality}#{@ext}"
25
+
26
+ image = MiniMagick::Image.open(@file)
27
+ image.format(@ext[1..-1])
28
+ image.quality(quality)
29
+ image.write(down_filename)
30
+
31
+ return down_filename
32
+ end
33
+
34
+ def backup!
35
+ FileUtils.mkdir_p(ORIGINAL_IMAGE_DIR) unless File.exists?(ORIGINAL_IMAGE_DIR)
36
+ FileUtils.move(@file, ORIGINAL_IMAGE_DIR + @file)
37
+ end
38
+
39
+ def destroy!
40
+ File.delete(@file)
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,67 @@
1
+ module UploadImageRetainLowQuality
2
+ class S3
3
+ CONFIG_FILE = ".upload_image_retain_low_qualityrc"
4
+
5
+ def initialize
6
+ file = if FileTest.file?(CONFIG_FILE)
7
+ CONFIG_FILE
8
+ else
9
+ [Dir.home, CONFIG_FILE].join("/")
10
+ end
11
+
12
+ raise "no aws config file" unless FileTest.file?(file)
13
+ @s3 = ::AWS::S3.new(YAML.load_file(file))
14
+ end
15
+
16
+ def buckets
17
+ @s3.buckets
18
+ end
19
+
20
+ def bucket=(bucket)
21
+ @bucket = bucket
22
+ end
23
+
24
+ def directories
25
+ directories = @bucket.as_tree({prefix: current_path}).children.select(&:branch?).collect(&:prefix)
26
+ directories.map{|dir| dir.split("/").last}
27
+ end
28
+
29
+ def current_path
30
+ @prefix
31
+ end
32
+
33
+ def cd(prefix)
34
+ @prefix = [@prefix, prefix].compact.join("/")
35
+ end
36
+
37
+ def upload_file(file, to_path, opts = {})
38
+ raise "set bucket at first" unless @bucket
39
+ to_path = [to_path, file].compact.join("/")
40
+ acl = opts[:acl] || :private
41
+
42
+ #p puts "#{file} to #{to_path}"
43
+ obj = @bucket.objects[to_path] || @bucket.create(to_path)
44
+ obj.write(File.open(file), { :acl => acl })
45
+ end
46
+
47
+ # Class Methods
48
+ class << self
49
+ def generate_config
50
+ if FileTest.file?(CONFIG_FILE)
51
+ Console.print_red "#{CONFIG_FILE} is already exists"
52
+ else
53
+
54
+ config = <<"CONFIG"
55
+ access_key_id: 'YOUR ACCESS KEY'
56
+ secret_access_key: 'SECRET ACCESS KEY'
57
+ CONFIG
58
+
59
+ f = File.new(CONFIG_FILE, "w")
60
+ f << config
61
+ f.close
62
+ Console.print_green "#{CONFIG_FILE} was created"
63
+ end
64
+ end
65
+ end # Class Methods End
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module UploadImageRetainLowQuality
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'upload_image_retain_low_quality/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "upload_image_retain_low_quality"
8
+ spec.version = UploadImageRetainLowQuality::VERSION
9
+ spec.authors = ["y.fujii"]
10
+ spec.email = ["ishikurasakura@gmail.com"]
11
+ spec.description = %q{upload image to s3 bucket retain low quality image file}
12
+ spec.summary = %q{upload image to s3 bucket retain low quality image file}
13
+ spec.homepage = "http://d.hatena.ne.jp/gogo_sakura/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'aws-sdk'
22
+ spec.add_dependency 'mini_magick'
23
+ spec.add_dependency 'parallel'
24
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upload_image_retain_low_quality
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - y.fujii
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mini_magick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: parallel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: upload image to s3 bucket retain low quality image file
56
+ email:
57
+ - ishikurasakura@gmail.com
58
+ executables:
59
+ - upload_image_retain_low_quality
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/upload_image_retain_low_quality
69
+ - lib/upload_image_retain_low_quality.rb
70
+ - lib/upload_image_retain_low_quality/console.rb
71
+ - lib/upload_image_retain_low_quality/image.rb
72
+ - lib/upload_image_retain_low_quality/s3.rb
73
+ - lib/upload_image_retain_low_quality/version.rb
74
+ - upload_image_retain_low_quality.gemspec
75
+ homepage: http://d.hatena.ne.jp/gogo_sakura/
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: upload image to s3 bucket retain low quality image file
99
+ test_files: []