image_server 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2c9451bb393818af965b833090b863d4576a331
4
+ data.tar.gz: d239ae1f1954a6f2325daad9ecad36e60db90f09
5
+ SHA512:
6
+ metadata.gz: 2609cc4f7c44877ee5207d3ca65447a4cbbb4c99c638e79a4ce1f0fc80da379b5edb03e95914768ed5d08084640c20c615ba2f18021f3ef1a56f6e654f4d4b24
7
+ data.tar.gz: 8bb96b9df5830adf36dd8384cf26ee301ea4cddd14e2a51536fdb2bd6deeac2544c6c9ee98fa4f1eda3651b6026899c8fea1be9f5d80a324231883f03499af4d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in image-server-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Richard Millan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # ImageServer Ruby Client
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/image/server/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'image-server'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install image-server
22
+
23
+ ## Usage
24
+
25
+ Call this method to modify defaults in your initializers.
26
+ ```ruby
27
+ ImageServer.configure do |config|
28
+ config.logger = Rails.logger
29
+ config.upload_host = '127.0.0.1'
30
+ config.cdn_protocol = '//:'
31
+ config.cdn_host = 'example.com'
32
+ config.sharded_cdn_host = 'img-%d.example.com'
33
+ config.sharded_host_count = 3
34
+ end
35
+ ```
36
+
37
+ To prepare a model to use image server
38
+ ```ruby
39
+ class AddImageHashToProducts < ActiveRecord::Migration
40
+ def change
41
+ add_column :products, :image_hash, :string, :limit => 32
42
+ end
43
+ end
44
+ ```
45
+
46
+ To install rake tasks, include the following line in `Rakefile`
47
+ ```
48
+ require 'image_server/tasks'
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/image-server/image-server-ruby.
60
+
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
65
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "image/server/ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'image_server/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'image_server'
8
+ spec.version = ImageServer::VERSION
9
+ spec.authors = ['Richard Millan']
10
+ spec.email = ['richardiux@gmail.com']
11
+
12
+ spec.summary = %q{ImageServer ruby client}
13
+ spec.homepage = 'https://github.com/image-server/ruby_image_server'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'bundler', '~> 1.11'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'webmock', '~> 1.22.3'
34
+ end
@@ -0,0 +1,42 @@
1
+ require 'image_server/version'
2
+ require_relative 'image_server/logger'
3
+ require_relative 'image_server/configuration'
4
+ require_relative 'image_server/uploader'
5
+ require_relative 'image_server/attachment_uploader'
6
+ require_relative 'image_server/image'
7
+ require_relative 'image_server/url'
8
+
9
+ module ImageServer
10
+ class PermanentFailure < StandardError; end
11
+ class TemporaryFailure < StandardError; end
12
+
13
+ class ImageServerUnavailable < TemporaryFailure; end
14
+ class StoreConcurrencyExceeded < TemporaryFailure; end
15
+
16
+ class UploadError < PermanentFailure; end
17
+ class SourceNotFound < PermanentFailure; end
18
+ class InvalidSource < PermanentFailure; end
19
+ class Blocked < PermanentFailure; end
20
+ class ConnectionFailure < PermanentFailure; end
21
+
22
+ class << self
23
+ def logger
24
+ @logger ||= Logger.new
25
+ end
26
+
27
+ def configuration
28
+ @configuration ||= Configuration.new
29
+ end
30
+
31
+ # Call this method to modify defaults in your initializers.
32
+ #
33
+ # @example
34
+ # ImageServer.configure do |config|
35
+ # config.logger = Rails.logger
36
+ # config.upload_host = '127.0.0.1'
37
+ # end
38
+ def configure
39
+ yield(configuration) if block_given?
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ module ImageServer
2
+ module ActiveRecord
3
+ def image_server(column, namespace: 'img', versions: nil, processing_formats: [:jpg], default_size: 'full_size', default_format: 'jpg', sharded_cdn_template: nil)
4
+ sizes = versions.values.sort.uniq
5
+ keys = versions.keys.sort.uniq
6
+ processing_formats = processing_formats.map(&:to_s).sort.uniq
7
+ outputs = sizes.flat_map { |s| processing_formats.map { |f| "#{s}.#{f}" } }
8
+ outputs_str = outputs.join(',')
9
+
10
+ namespace_constant = "#{column.to_s.upcase}_NAMESPACE"
11
+ outputs_constant = "#{column.to_s.upcase}_OUTPUTS_STR"
12
+ keys_constant = "#{column.to_s.upcase}_KEYS"
13
+
14
+ sharded_cdn_template_constant = "#{column.to_s.upcase}_SHARDED_CDN_TEMPLATE"
15
+ sharded_cdn_template = sharded_cdn_template ? "'#{sharded_cdn_template}'" : 'nil'
16
+
17
+ class_eval <<-RUBY, __FILE__, __LINE__+1
18
+ #{namespace_constant} = '#{namespace}'
19
+ #{outputs_constant} = '#{outputs_str}'
20
+ #{keys_constant} = #{keys}
21
+ #{sharded_cdn_template_constant} = #{sharded_cdn_template}
22
+
23
+ def remote_#{column}_url=(url)
24
+ uploader = ImageServer::Uploader.new(#{namespace_constant}, url, #{outputs_constant})
25
+ image_property = uploader.upload
26
+ self.#{column}_hash = image_property.image_hash
27
+ end
28
+
29
+ def #{column}(options={})
30
+ processing = #{column}_hash.blank?
31
+ default_options = { protocol: #{column}_cdn_protocol,
32
+ domain: #{column}_cdn_domain,
33
+ default_size: '#{default_size}',
34
+ format: '#{default_format}',
35
+ processing: processing,
36
+ object: self }
37
+ ImageServer::Image.new(#{namespace_constant}, #{column}_hash, default_options.merge(options))
38
+ end
39
+
40
+ def #{column}_url(*attrs)
41
+ #{column}.url(*attrs).to_s
42
+ end
43
+
44
+ def upload_#{column}_attachment(name, file)
45
+ uploader = ::ImageServer::AttachmentUploader.new(#{namespace_constant}, #{column}_hash)
46
+ uploader.upload(name, file)
47
+ end
48
+
49
+ private
50
+
51
+ def #{column}_cdn_protocol
52
+ ImageServer.configuration.cdn_protocol
53
+ end
54
+
55
+ def #{column}_cdn_domain
56
+ return #{column}_host if #{column}_hash.blank?
57
+ return #{column}_host unless #{sharded_cdn_template_constant}
58
+ shard = #{column}_hash[0].hex % #{column}_sharded_host_count
59
+ #{sharded_cdn_template_constant} % shard
60
+ end
61
+
62
+ def #{column}_host
63
+ ImageServer.configuration.cdn_host
64
+ end
65
+
66
+ def #{column}_sharded_host_count
67
+ ImageServer.configuration.sharded_host_count
68
+ end
69
+ RUBY
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,72 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require_relative '../logger'
4
+ require_relative 'http/error_handler'
5
+
6
+ module ImageServer
7
+ module Adapters
8
+ class Http
9
+ attr_reader :url
10
+
11
+ def initialize(namespace, source, configuration: ImageServer.configuration)
12
+ @namespace = namespace
13
+ @source = source
14
+ @configuration = configuration
15
+ end
16
+
17
+ def upload(upload_uri)
18
+ logger.info "ImageServer::Adapters::Http --> uploading to image server: [#{upload_uri}]"
19
+
20
+ response = Net::HTTP.start(upload_uri.host, upload_uri.port) do |http|
21
+
22
+ http.read_timeout = 60
23
+ body = if source_is_url?
24
+ '{}' # blank body
25
+ elsif @source.is_a?(File)
26
+ @source
27
+ elsif @source.respond_to?(:path)
28
+ File.open(@source.path).read
29
+ else
30
+ raise('Not supported')
31
+ end
32
+
33
+ http.post("#{upload_uri.path}?#{upload_uri.query}", body)
34
+ end
35
+
36
+ ErrorHandler.new(response).handle_errors!
37
+ @body = JSON.parse(response.body)
38
+ rescue Errno::ECONNREFUSED => e
39
+ raise ImageServerUnavailable
40
+ end
41
+
42
+ def valid?
43
+ @body && (image_hash && width && height)
44
+ end
45
+
46
+ def image_hash
47
+ @body['hash']
48
+ end
49
+
50
+ def width
51
+ @body['width'].to_i
52
+ end
53
+
54
+ def height
55
+ @body['height'].to_i
56
+ end
57
+
58
+ private
59
+
60
+ def logger
61
+ @@logger ||= ImageServer::Logger.new
62
+ end
63
+
64
+ def source_is_url?
65
+ return false unless @source.is_a?(String)
66
+
67
+ @source.start_with?('http') || @source.start_with?('//')
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,46 @@
1
+ module ImageServer
2
+ module Adapters
3
+ class Http
4
+ class ErrorHandler
5
+ attr_reader :response
6
+
7
+ def initialize(http_response)
8
+ @response = http_response
9
+ end
10
+
11
+ def handle_errors!
12
+ case response
13
+ when Net::HTTPOK
14
+ when Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut
15
+ raise ImageServerUnavailable
16
+ when Net::HTTPNotFound
17
+ error = JSON.parse(response.body)['error']
18
+ if error.start_with?('Unable to download image') || error.start_with?('File is empty')
19
+ raise SourceNotFound, error
20
+ elsif error.end_with?('i/o timeout')
21
+ raise Blocked, error
22
+ elsif error.start_with?('ImageMagick failed')
23
+ raise InvalidSource, error
24
+ elsif error.include?('dial tcp')
25
+ raise ConnectionFailure, error
26
+ else
27
+ # generic case, let's log the error so we can add it later
28
+ # but we consider this a permanent error so tht we don't block processing.
29
+ logger.error "error uploading, but error was not recognized: #{error.inspect}"
30
+ raise UploadError, error
31
+ end
32
+ else
33
+ raise UploadError, response
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def logger
40
+ @@logger ||= ImageServer::Logger.new
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,26 @@
1
+ require_relative 'adapters/http'
2
+
3
+ module ImageServer
4
+ class AttachmentUploader
5
+ def initialize(namespace, hash, configuration: ImageServer.configuration)
6
+ @namespace = namespace
7
+ @hash = hash
8
+ @configuration = configuration
9
+ end
10
+
11
+ def upload(name, source)
12
+ uploader = Adapters::Http.new(@namespace, source, configuration: @configuration)
13
+ properties_json = uploader.upload(uri(name))
14
+ end
15
+
16
+ private
17
+
18
+ def uri(name)
19
+ URI.parse("#{@configuration.upload_host}/#{directory_path}/#{name}")
20
+ end
21
+
22
+ def directory_path
23
+ Path.directory_path(@namespace, @hash)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ require 'uri'
2
+
3
+ module ImageServer
4
+ class Configuration
5
+ attr_accessor :logger
6
+ attr_accessor :log_directory
7
+ attr_writer :upload_host
8
+ attr_accessor :path_to_binary
9
+ attr_accessor :cdn_protocol
10
+ attr_accessor :cdn_host
11
+ attr_accessor :sharded_cdn_host
12
+ attr_accessor :sharded_host_count
13
+
14
+ def port
15
+ return unless @upload_host
16
+
17
+ URI(upload_host).port
18
+ end
19
+
20
+ # returns upload_host with a protocol included
21
+ # example: if @upload_host is `example.com` will return `http://example.com`
22
+ def upload_host
23
+ return unless @upload_host
24
+ return @upload_host if @upload_host.start_with?('http')
25
+
26
+ "http://#{@upload_host}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ module ImageServer
2
+ class Image
3
+
4
+ def initialize(namespace, image_hash, protocol: 'http://', domain: nil, size: nil, format: nil, processing: false, object: nil, default_size: nil)
5
+ @namespace = namespace
6
+ @image_hash = image_hash
7
+ @protocol = protocol
8
+ @domain = domain
9
+ @size = size
10
+ @format = format
11
+ @processing = processing
12
+ @object = object
13
+ @default_size = default_size
14
+ end
15
+
16
+ def url(options = {})
17
+ options = options.merge({ processing: @processing })
18
+ image_url_for_version(@default_size, options)
19
+ end
20
+
21
+ private
22
+
23
+ def image_url_for_version(version, options = {})
24
+ options = {
25
+ protocol: @protocol,
26
+ domain: @domain,
27
+ size: version,
28
+ format: 'jpg',
29
+ }.merge(options)
30
+
31
+ ImageServer::Url.from_hash(@namespace, @image_hash, options)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,61 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'open-uri'
4
+ require 'fileutils'
5
+
6
+ module ImageServer
7
+ class Installer
8
+ attr_reader :configuration
9
+
10
+ def self.install(configuration = ImageServer.configuration)
11
+ new(configuration).install
12
+ end
13
+
14
+ def initialize(configuration = ImageServer.configuration)
15
+ @configuration = configuration
16
+ end
17
+
18
+ def install
19
+ return unless valid_platform?
20
+ return if installed_latest?
21
+
22
+ FileUtils.mkdir_p 'bin'
23
+ File.open(bin_path, 'wb') do |saved_file|
24
+ open(remote_url, 'rb') do |read_file|
25
+ saved_file.write(read_file.read)
26
+ end
27
+ end
28
+ File.chmod(744, bin_path)
29
+ end
30
+
31
+ def valid_platform?
32
+ %w(darwin linux).include?(platform)
33
+ end
34
+
35
+ def platform
36
+ `uname -s`.strip.downcase
37
+ end
38
+
39
+ def current_version
40
+ '1.16.0'
41
+ end
42
+
43
+ def executable_name
44
+ "images-#{platform}-#{current_version}"
45
+ end
46
+
47
+ def remote_url
48
+ "https://github.com/image-server/image-server/releases/download/v#{current_version}/#{executable_name}"
49
+ end
50
+
51
+ def installed_latest?
52
+ File.exist?(bin_path) && `#{bin_path} --version`.split(' ').last.chomp == current_version
53
+ rescue
54
+ false
55
+ end
56
+
57
+ def bin_path
58
+ configuration.path_to_binary
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,23 @@
1
+ require 'logger'
2
+
3
+ module ImageServer
4
+ class Logger
5
+ LOG_PREFIX = '** [ImageServer]'
6
+
7
+ [
8
+ :fatal,
9
+ :error,
10
+ :warn,
11
+ :info,
12
+ :debug,
13
+ ].each do |level|
14
+ define_method level do |*args, &block|
15
+ msg = block.call if block
16
+ logger = ImageServer.configuration.logger
17
+ logger ||= ::Logger.new(STDOUT)
18
+
19
+ logger.send(level, "#{LOG_PREFIX} #{msg}") if logger
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module ImageServer
2
+ class Path
3
+ def self.directory_path(namespace, image_hash)
4
+ "#{namespace}/#{image_hash[0..2]}/#{image_hash[3..5]}/#{image_hash[6..8]}/#{image_hash[9..-1]}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,70 @@
1
+ require 'fileutils'
2
+ require 'net/http'
3
+
4
+ module ImageServer
5
+ class Spawner
6
+ attr_accessor :server, :started, :pid, :configuration
7
+
8
+ def initialize(configuration: ImageServer.configuration)
9
+ @configuration = configuration
10
+ end
11
+
12
+ def start
13
+ unless started?
14
+ self.started = Time.now
15
+ self.pid = fork do
16
+ $stderr.reopen('/dev/null')
17
+ $stdout.reopen('/dev/null')
18
+ server_exec.run
19
+ end
20
+ kill_at_exit
21
+ give_feedback
22
+ end
23
+ end
24
+
25
+ def started?
26
+ Net::HTTP.get_response(URI.parse("#{configuration.upload_host}:#{configuration.port}/status_check"))
27
+ rescue StandardError
28
+ nil
29
+ end
30
+
31
+ def kill_at_exit
32
+ at_exit do
33
+ if pid
34
+ logger.info 'image server is stopping'
35
+ `pkill -TERM -P #{pid}` # also stops child processes
36
+ end
37
+ end
38
+ end
39
+
40
+ def give_feedback
41
+ logger.info("image server is starting on port #{configuration.port}...") while starting
42
+ logger.info "image server took #{seconds} seconds to start"
43
+ end
44
+
45
+ def seconds
46
+ '%.3f' % (Time.now - started)
47
+ end
48
+
49
+ def starting
50
+ response = started?
51
+ return false unless response
52
+ raise 'Unable to start image server' unless response.kind_of?(Net::HTTPSuccess)
53
+ false
54
+ rescue Errno::ECONNREFUSED
55
+ true
56
+ end
57
+
58
+ private
59
+
60
+ def server_exec
61
+ log_file = File.join(configuration.log_directory, 'image_server.log')
62
+ error_file = File.join(configuration.log_directory, 'image_server_errors.log')
63
+ exec("#{configuration.path_to_binary} --port=#{configuration.port} --outputs='' server > #{log_file} 2> #{error_file}")
64
+ end
65
+
66
+ def logger
67
+ @@logger ||= ImageServer::Logger.new
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,2 @@
1
+ spec = Gem::Specification.find_by_name 'image-server'
2
+ load "#{spec.gem_dir}/lib/tasks/image_server.rake"
@@ -0,0 +1,58 @@
1
+ require_relative 'adapters/http'
2
+
3
+ module ImageServer
4
+ class Uploader
5
+ def initialize(namespace, source, outputs, configuration: ImageServer.configuration)
6
+ @namespace = namespace
7
+ @source = source
8
+ @outputs = outputs
9
+ @configuration = configuration
10
+ end
11
+
12
+ def upload(force: false)
13
+ return existing_image_property if existing_image_property && !force
14
+ uploader = Adapters::Http.new(@namespace, @source, configuration: @configuration)
15
+ properties_json = uploader.upload(uri)
16
+
17
+ find_or_create_image_property(properties_json)
18
+ rescue PermanentFailure => e
19
+ raise UploadError.new(e.message)
20
+ end
21
+
22
+ private
23
+
24
+ def uri
25
+ uri = URI.parse("#{@configuration.upload_host}/#{@namespace}")
26
+ params = {outputs: @outputs}
27
+ params[:source] = url if source_is_url?
28
+ uri.query = URI.encode_www_form(params)
29
+ uri
30
+ end
31
+
32
+ def find_or_create_image_property(properties)
33
+ attributes = {
34
+ width: properties['width'],
35
+ height: properties['height'],
36
+ image_url: url,
37
+ namespace: @namespace
38
+ }
39
+ ip = ImageProperty.where(image_hash: properties['hash'], namespace: @namespace).first || ImageProperty.new(image_hash: properties['hash'])
40
+ ip.assign_attributes(attributes)
41
+ ip.save!
42
+ ip
43
+ end
44
+
45
+ def url
46
+ return @source if source_is_url?
47
+ end
48
+
49
+ def existing_image_property
50
+ return unless url
51
+ @existing_image_property ||= ImageProperty.where(namespace: @namespace).where('lower(image_url) = lower(?)', url).first
52
+ end
53
+
54
+ def source_is_url?
55
+ @source.is_a?(String) && (@source.start_with?('http') || @source.start_with?('//'))
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'path'
2
+
3
+ module ImageServer
4
+ class Url
5
+ # Pregenerated images
6
+ # full_size.jpg, x100-q95.jpg
7
+ def self.from_hash(namespace, image_hash, size: 'full_size', format: 'jpg', protocol: ImageServer.configuration.cdn_protocol, domain: nil, processing: false)
8
+ return if image_hash.to_s.empty?
9
+ domain ||= self.domain(image_hash)
10
+ directory_path = Path.directory_path(namespace, image_hash)
11
+ url = "#{protocol}#{domain}/#{directory_path}/#{size}"
12
+ url += ".#{format}" unless size.to_s == 'original'
13
+ url += '?processing' if processing
14
+ url
15
+ end
16
+
17
+ def self.domain(image_hash)
18
+ host = ImageServer.configuration.cdn_host
19
+ sharded_cdn_template = ImageServer.configuration.sharded_cdn_host
20
+
21
+ return host if image_hash.to_s.empty?
22
+ return host unless sharded_cdn_template
23
+ shard = image_hash[0].hex % ImageServer.configuration.sharded_host_count
24
+ sharded_cdn_template % shard
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module ImageServer
2
+ VERSION = '0.3.0'
3
+ end
@@ -0,0 +1,8 @@
1
+ namespace :image_server do
2
+ desc 'Installs the image server if not available'
3
+ task :install => :environment do
4
+ require 'image_server/installer'
5
+
6
+ ImageServer::Installer.install
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Millan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.22.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.22.3
83
+ description:
84
+ email:
85
+ - richardiux@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - image-server-ruby.gemspec
100
+ - lib/image_server.rb
101
+ - lib/image_server/active_record.rb
102
+ - lib/image_server/adapters/http.rb
103
+ - lib/image_server/adapters/http/error_handler.rb
104
+ - lib/image_server/attachment_uploader.rb
105
+ - lib/image_server/configuration.rb
106
+ - lib/image_server/image.rb
107
+ - lib/image_server/installer.rb
108
+ - lib/image_server/logger.rb
109
+ - lib/image_server/path.rb
110
+ - lib/image_server/spawner.rb
111
+ - lib/image_server/tasks.rb
112
+ - lib/image_server/uploader.rb
113
+ - lib/image_server/url.rb
114
+ - lib/image_server/version.rb
115
+ - lib/tasks/image_server.rake
116
+ homepage: https://github.com/image-server/ruby_image_server
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.5.1
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: ImageServer ruby client
140
+ test_files: []