diff options
| author | mo khan <mo@mokhan.ca> | 2014-06-07 08:26:53 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-06-07 08:26:53 -0600 |
| commit | 532fa71eae4ecc096d30e2fbebcd77305889fcf6 (patch) | |
| tree | 5ff30d137dba8c96af0755b865fd04c543b15889 /app | |
| parent | b3d59861124ece048e34fcb58c1a3ec99ed3feba (diff) | |
add ability to generate urls for different versions of an image.
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/creation.rb | 4 | ||||
| -rw-r--r-- | app/models/photo.rb | 64 | ||||
| -rw-r--r-- | app/services/application/handlers/process_photo.rb | 2 | ||||
| -rw-r--r-- | app/services/application/photo_to_jq_json_mapper.rb | 3 |
4 files changed, 45 insertions, 28 deletions
diff --git a/app/models/creation.rb b/app/models/creation.rb index 51ca8c6f..d331c06e 100644 --- a/app/models/creation.rb +++ b/app/models/creation.rb @@ -26,10 +26,6 @@ class Creation < ActiveRecord::Base is_restricted == false end - def add_photo(photo) - photos.create(:image => photo) - end - def is_liked_by(user) favorites.where(user: user).any? end diff --git a/app/models/photo.rb b/app/models/photo.rb index 95ac5c12..4878005e 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,53 +1,75 @@ class Photo < ActiveRecord::Base belongs_to :creation, counter_cache: true, touch: true - #mount_uploader :image, PhotoUploader def watermark return '' if creation.nil? creation.watermark end + def url_for(version_key, asset_host = ENV['ASSET_HOST']) + versions.find { |version| version.for?(version_key) }.url_for(asset_host) + end + def is_processed? !self.image_processing end def upload(file, blob_storage) image = Image.new(file) - self.image = self.original_filename = image.filename + self.original_filename = File.basename(file) + self.image = image.filename self.content_type = image.content_type self.latitude, self.longitude = image.geolocation self.image_processing = nil versions.each do |version| version.adjust(image) - blob_storage.upload(create_key(version.prefix), image.path) + blob_storage.upload(version.create_key, image.path) end end private - def create_key(prefix = '') - "uploads/photo/image/#{id}/#{prefix}#{original_filename}" - end - def versions - @versions ||= [OriginalVersion.new, LargeVersion.new, ThumbnailVersion.new] + @versions ||= [OriginalVersion.new(self), LargeVersion.new(self), ThumbnailVersion.new(self)] end - class OriginalVersion - attr_reader :prefix + class Version + attr_reader :key, :prefix, :photo - def initialize - @prefix = '' + def initialize(photo, key, prefix) + @key = key + @prefix = prefix + @photo = photo end - def adjust(image); end + def adjust(image) + fail "Please override with version specific behaviours" + end + + def for?(other_key) + key == other_key + end + + def url_for(asset_host) + "#{asset_host}/#{create_key}" + end + + def create_key + "uploads/photo/image/#{photo.id}/#{prefix}#{photo.image}" + end end - class LargeVersion - attr_reader :prefix + class OriginalVersion < Version + def initialize(photo) + super(photo, :original, "") + end + + def adjust(image); end + end - def initialize - @prefix = 'large_' + class LargeVersion < Version + def initialize(photo) + super(photo, :large, "large_") end def adjust(image) @@ -55,11 +77,9 @@ class Photo < ActiveRecord::Base end end - class ThumbnailVersion - attr_reader :prefix - - def initialize - @prefix = 'thumb_' + class ThumbnailVersion < Version + def initialize(photo) + super(photo, :thumb, "thumb_") end def adjust(image) diff --git a/app/services/application/handlers/process_photo.rb b/app/services/application/handlers/process_photo.rb index 780c9aa3..57f9dc82 100644 --- a/app/services/application/handlers/process_photo.rb +++ b/app/services/application/handlers/process_photo.rb @@ -1,5 +1,5 @@ class ProcessPhoto - def initialize(photos, blob_storage = BlobStorage.new) + def initialize(photos, blob_storage) @photos = photos @blob_storage = blob_storage end diff --git a/app/services/application/photo_to_jq_json_mapper.rb b/app/services/application/photo_to_jq_json_mapper.rb index dc2d37e3..6b43a124 100644 --- a/app/services/application/photo_to_jq_json_mapper.rb +++ b/app/services/application/photo_to_jq_json_mapper.rb @@ -2,7 +2,8 @@ class PhotoToJQJsonMapper def map_from(photo) { name: photo.read_attribute(:image), - url: photo.image.url, + #url: photo.image.url, + url: photo.url_for(:large), thumbnail_url: photo.is_processed? ? photo.image.thumb.url : photo.image.thumb.default_url, delete_url: photo.id, delete_type: "DELETE" |
