2011-05-20 16:19:14 -07:00
|
|
|
require 'fileutils'
|
|
|
|
require 'uri'
|
2011-06-04 15:40:15 -07:00
|
|
|
require 'utf8'
|
2011-05-20 16:19:14 -07:00
|
|
|
|
2010-05-16 12:01:38 -07:00
|
|
|
class SwfAsset < ActiveRecord::Base
|
2010-10-09 07:53:58 -07:00
|
|
|
PUBLIC_ASSET_DIR = File.join('swfs', 'outfit')
|
|
|
|
LOCAL_ASSET_DIR = Rails.root.join('public', PUBLIC_ASSET_DIR)
|
2011-05-20 16:19:14 -07:00
|
|
|
IMAGE_BUCKET = IMPRESS_S3.bucket('impress-asset-images')
|
|
|
|
IMAGE_PERMISSION = 'public-read'
|
|
|
|
IMAGE_HEADERS = {
|
|
|
|
'Cache-Control' => 'max-age=315360000',
|
|
|
|
'Content-Type' => 'image/png'
|
|
|
|
}
|
2010-11-27 15:41:06 -08:00
|
|
|
NEOPETS_ASSET_SERVER = 'http://images.neopets.com'
|
2011-05-13 05:00:34 -07:00
|
|
|
|
2010-05-16 12:01:38 -07:00
|
|
|
set_inheritance_column 'inheritance_type'
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2011-05-13 05:00:34 -07:00
|
|
|
include SwfConverter
|
|
|
|
converts_swfs :size => [600, 600], :output_sizes => [[150, 150], [300, 300], [600, 600]]
|
|
|
|
|
|
|
|
def local_swf_path
|
|
|
|
LOCAL_ASSET_DIR.join(local_path_within_outfit_swfs)
|
|
|
|
end
|
|
|
|
|
2011-06-10 11:45:33 -07:00
|
|
|
def swf_image_dir
|
|
|
|
@swf_image_dir ||= Rails.root.join('tmp', 'asset_images_before_upload', self.id.to_s)
|
|
|
|
end
|
|
|
|
|
2011-05-13 05:00:34 -07:00
|
|
|
def swf_image_path(size)
|
2011-06-10 11:45:33 -07:00
|
|
|
swf_image_dir.join("#{size.join 'x'}.png")
|
2011-05-20 16:19:14 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_swf_conversion(images)
|
|
|
|
images.each do |size, path|
|
2011-05-22 13:30:02 -07:00
|
|
|
key = s3_key(size)
|
|
|
|
print "Uploading #{key}..."
|
2011-05-20 16:19:14 -07:00
|
|
|
IMAGE_BUCKET.put(
|
2011-05-22 13:30:02 -07:00
|
|
|
key,
|
2011-05-20 16:19:14 -07:00
|
|
|
File.open(path),
|
|
|
|
{}, # meta headers
|
|
|
|
IMAGE_PERMISSION, # permission
|
|
|
|
IMAGE_HEADERS
|
|
|
|
)
|
|
|
|
puts "done."
|
|
|
|
|
|
|
|
FileUtils.rm path
|
|
|
|
end
|
2011-06-10 11:45:33 -07:00
|
|
|
FileUtils.rmdir swf_image_dir
|
2011-08-07 15:23:44 -07:00
|
|
|
|
|
|
|
self.converted_at = Time.now
|
|
|
|
self.has_image = true
|
|
|
|
self.save!
|
2011-05-20 16:19:14 -07:00
|
|
|
end
|
|
|
|
|
2011-05-22 13:30:02 -07:00
|
|
|
def s3_key(size)
|
|
|
|
URI.encode("#{s3_path}/#{size.join 'x'}.png")
|
|
|
|
end
|
|
|
|
|
|
|
|
def s3_path
|
2011-08-07 15:23:44 -07:00
|
|
|
"#{self['type']}/#{s3_partition_path}#{self.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def s3_url(size)
|
|
|
|
"#{IMAGE_BUCKET.public_link}/#{s3_path}/#{size.join 'x'}.png"
|
2011-05-22 13:30:02 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
PARTITION_COUNT = 3
|
|
|
|
PARTITION_DIGITS = 3
|
|
|
|
PARTITION_ID_LENGTH = PARTITION_COUNT * PARTITION_DIGITS
|
|
|
|
def s3_partition_path
|
|
|
|
(id / 10**PARTITION_DIGITS).to_s.rjust(PARTITION_ID_LENGTH, '0').tap do |id_str|
|
|
|
|
PARTITION_COUNT.times do |n|
|
|
|
|
id_str.insert(PARTITION_ID_LENGTH - (n * PARTITION_DIGITS), '/')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-20 16:19:14 -07:00
|
|
|
def convert_swf_if_not_converted!
|
2011-08-07 15:23:44 -07:00
|
|
|
if needs_conversion?
|
2011-05-20 16:19:14 -07:00
|
|
|
convert_swf!
|
|
|
|
true
|
2011-08-07 15:23:44 -07:00
|
|
|
else
|
|
|
|
false
|
2011-05-20 16:19:14 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-22 10:10:29 -07:00
|
|
|
def request_image_conversion!
|
2011-05-20 16:19:14 -07:00
|
|
|
if image_requested?
|
|
|
|
false
|
|
|
|
else
|
2011-05-22 13:30:02 -07:00
|
|
|
Resque.enqueue(AssetImageConversionRequest, self.type, self.id)
|
2011-05-20 16:19:14 -07:00
|
|
|
self.image_requested = true
|
|
|
|
save!
|
|
|
|
true
|
|
|
|
end
|
2011-05-13 05:00:34 -07:00
|
|
|
end
|
|
|
|
|
2011-08-07 15:23:44 -07:00
|
|
|
def report_broken
|
|
|
|
if image_pending_repair?
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
Resque.enqueue(AssetImageConversionRequest::OnBrokenImageReport, self.type, self.id)
|
|
|
|
self.reported_broken_at = Time.now
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def needs_conversion?
|
|
|
|
!has_image? || image_pending_repair?
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_pending_repair?
|
|
|
|
reported_broken_at && (converted_at.nil? || reported_broken_at > converted_at)
|
|
|
|
end
|
|
|
|
|
2010-11-06 08:52:58 -07:00
|
|
|
attr_accessor :item
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-11-06 15:08:42 -07:00
|
|
|
has_one :contribution, :as => :contributed
|
2010-10-10 19:18:42 -07:00
|
|
|
has_many :object_asset_relationships, :class_name => 'ParentSwfAssetRelationship',
|
|
|
|
:conditions => {:swf_asset_type => 'object'}
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-05-20 16:04:56 -07:00
|
|
|
delegate :depth, :to => :zone
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-06-08 15:26:42 -07:00
|
|
|
scope :fitting_body_id, lambda { |body_id|
|
|
|
|
where(arel_table[:body_id].in([body_id, 0]))
|
|
|
|
}
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-06-08 15:26:42 -07:00
|
|
|
BodyIdsFittingStandard = PetType::StandardBodyIds + [0]
|
|
|
|
scope :fitting_standard_body_ids, lambda {
|
|
|
|
where(arel_table[:body_id].in(BodyIdsFittingStandard))
|
2010-06-07 13:33:43 -07:00
|
|
|
}
|
2011-05-02 15:07:56 -07:00
|
|
|
|
|
|
|
scope :fitting_color, lambda { |color|
|
|
|
|
body_ids = PetType.select(:body_id).where(:color_id => color.id).map(&:body_id)
|
|
|
|
where(arel_table[:body_id].in(body_ids))
|
|
|
|
}
|
|
|
|
|
2010-11-20 07:35:59 -08:00
|
|
|
scope :biology_assets, where(arel_table[:type].eq(PetState::SwfAssetType))
|
|
|
|
scope :object_assets, where(arel_table[:type].eq(Item::SwfAssetType))
|
2010-11-25 18:33:34 -08:00
|
|
|
scope :for_item_ids, lambda { |item_ids|
|
|
|
|
joins(:object_asset_relationships).
|
|
|
|
where(ParentSwfAssetRelationship.arel_table[:parent_id].in(item_ids))
|
|
|
|
}
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-05-20 16:04:56 -07:00
|
|
|
def local_url
|
2010-10-09 07:53:58 -07:00
|
|
|
'/' + File.join(PUBLIC_ASSET_DIR, local_path_within_outfit_swfs)
|
2010-05-20 16:04:56 -07:00
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-05-20 16:56:08 -07:00
|
|
|
def as_json(options={})
|
2010-10-10 19:18:42 -07:00
|
|
|
json = {
|
2010-05-20 16:04:56 -07:00
|
|
|
:id => id,
|
2011-05-22 13:30:02 -07:00
|
|
|
:type => type,
|
2010-05-20 16:04:56 -07:00
|
|
|
:depth => depth,
|
2010-06-07 16:50:49 -07:00
|
|
|
:body_id => body_id,
|
2010-10-10 19:18:42 -07:00
|
|
|
:zone_id => zone_id,
|
|
|
|
:zones_restrict => zones_restrict,
|
2011-05-20 16:19:14 -07:00
|
|
|
:is_body_specific => body_specific?,
|
2011-05-22 13:30:02 -07:00
|
|
|
:has_image => has_image?,
|
|
|
|
:s3_path => s3_path
|
2010-05-20 16:04:56 -07:00
|
|
|
}
|
2010-10-10 19:18:42 -07:00
|
|
|
if options[:for] == 'wardrobe'
|
|
|
|
json[:local_path] = local_url
|
|
|
|
else
|
|
|
|
json[:local_url] = local_url
|
|
|
|
end
|
|
|
|
json[:parent_id] = options[:parent_id] if options[:parent_id]
|
|
|
|
json
|
2010-05-20 16:04:56 -07:00
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-09 08:23:59 -07:00
|
|
|
def body_specific?
|
2010-11-27 15:41:06 -08:00
|
|
|
self.zone.type_id < 3
|
2010-10-09 08:23:59 -07:00
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-06-09 19:56:47 -07:00
|
|
|
def zone
|
2010-10-09 08:23:59 -07:00
|
|
|
Zone.find(zone_id)
|
2010-06-09 19:56:47 -07:00
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def origin_pet_type=(pet_type)
|
|
|
|
self.body_id = pet_type.body_id
|
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def origin_biology_data=(data)
|
|
|
|
self.type = 'biology'
|
|
|
|
self.zone_id = data[:zone_id].to_i
|
|
|
|
self.url = data[:asset_url]
|
|
|
|
self.zones_restrict = data[:zones_restrict]
|
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def origin_object_data=(data)
|
|
|
|
self.type = 'object'
|
|
|
|
self.zone_id = data[:zone_id].to_i
|
|
|
|
self.url = data[:asset_url]
|
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-11-27 15:41:06 -08:00
|
|
|
def mall_data=(data)
|
|
|
|
self.zone_id = data['zone'].to_i
|
|
|
|
self.url = "#{NEOPETS_ASSET_SERVER}/#{data['url']}"
|
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-10 11:43:01 -07:00
|
|
|
before_create do
|
2010-10-09 07:53:58 -07:00
|
|
|
uri = URI.parse url
|
2011-02-19 19:49:13 -08:00
|
|
|
begin
|
|
|
|
response = Net::HTTP.get_response(uri)
|
|
|
|
rescue Exception => e
|
|
|
|
raise DownloadError, e.message
|
|
|
|
end
|
2010-10-09 07:53:58 -07:00
|
|
|
if response.is_a? Net::HTTPSuccess
|
|
|
|
new_local_path = File.join(LOCAL_ASSET_DIR, local_path_within_outfit_swfs)
|
|
|
|
new_local_dir = File.dirname new_local_path
|
2011-06-04 15:40:15 -07:00
|
|
|
content = +response.body
|
2010-10-09 07:53:58 -07:00
|
|
|
FileUtils.mkdir_p new_local_dir
|
|
|
|
File.open(new_local_path, 'w') do |f|
|
|
|
|
f.print content
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
response.error!
|
|
|
|
rescue Exception => e
|
2011-02-19 19:09:12 -08:00
|
|
|
raise DownloadError, "Error loading SWF at #{url}: #{e.message}"
|
2010-10-09 07:53:58 -07:00
|
|
|
else
|
2011-02-19 19:09:12 -08:00
|
|
|
raise DownloadError, "Error loading SWF at #{url}. Response: #{response.inspect}"
|
2010-10-09 07:53:58 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-10 11:43:01 -07:00
|
|
|
before_save do
|
2010-10-09 08:23:59 -07:00
|
|
|
# If an asset body ID changes, that means more than one body ID has been
|
|
|
|
# linked to it, meaning that it's probably wearable by all bodies.
|
2010-10-10 19:18:42 -07:00
|
|
|
self.body_id = 0 if !self.body_specific? || (!self.new_record? && self.body_id_changed?)
|
2010-10-09 08:23:59 -07:00
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2011-05-22 13:30:02 -07:00
|
|
|
after_commit :on => :create do
|
|
|
|
Resque.enqueue(AssetImageConversionRequest::OnCreation, self.type, self.id)
|
2011-05-20 16:19:14 -07:00
|
|
|
end
|
|
|
|
|
2011-02-19 19:09:12 -08:00
|
|
|
class DownloadError < Exception;end
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-09 07:53:58 -07:00
|
|
|
private
|
2011-05-02 15:07:56 -07:00
|
|
|
|
2010-10-09 07:53:58 -07:00
|
|
|
def local_path_within_outfit_swfs
|
|
|
|
uri = URI.parse(url)
|
|
|
|
pieces = uri.path.split('/')
|
|
|
|
relevant_pieces = pieces[4..7]
|
|
|
|
relevant_pieces.unshift pieces[2]
|
|
|
|
File.join(relevant_pieces)
|
|
|
|
end
|
2010-05-16 12:01:38 -07:00
|
|
|
end
|
2011-05-02 15:07:56 -07:00
|
|
|
|