Skip to content

Instantly share code, notes, and snippets.

@camwest
Created March 31, 2009 19:19
Show Gist options
  • Save camwest/88350 to your computer and use it in GitHub Desktop.
Save camwest/88350 to your computer and use it in GitHub Desktop.
##### asset.rb ######
require "net/http"
require "cgi"
require "aws/s3"
class Asset < ActiveRecord::Base
belongs_to :asset_directory
belongs_to :mime_type
#static methods
def Asset.cryptFileName(filename)
(rand(10000).to_s+filename+Time.now.to_s).crypt('xx').gsub(/\./, '_').split("/").join("_")
end
#instance methods
def directory
asset_directory
end
def directory=(x)
asset_directory=x
end
def filename
''
end
def filename=(x)
end
def payload
@data
end
def payload=(x)
return if x=='' # don't change anything
@payload_filename = x.original_filename
/\.([^.]+)$/ =~ x.original_filename
@payload_ext = $1
self.mime_type_id = MimeType.find_or_create(x.content_type).id
self.bytes = x.length
# only send small images and documents to amazon, don't rename so there are no ugly pdf names
if self.bytes < 65536 || self.mime_type_id == 4 || self.mime_type_id == 9 || self.mime_type_id == 6
# STORE THE DATA ON S3
self.storage_type = 2
self.data = send_to_amazon(x.original_filename, x.read)
else
# STORE THE DATA ON CDN
self.storage_type = 1
filename = Asset.cryptFileName(@payload_filename)
filename << '.'+(mime_type.file_ext || @payload_ext)
self.data = filename
unless File.exists?(Asset.file_repo('.'))
Dir.mkdir(Asset.file_repo('').sub(/\/$/,''))
end
File.open(Asset.file_repo(filename),"wb") do |f|
f.write(x.read)
end
end
end
def Asset.file_repo(x)
"#{RAILS_ROOT}/public"+Asset.local_repo(x)
end
def Asset.batch_repo(x)
"#{RAILS_ROOT}/public/system/batch/#{x}"
end
def Asset.local_repo(x)
return "/system/asset/#{x}"
end
def Asset.find_on_cdn(x)
response = nil
server_path = "videos-001.pdcwwe.com"
x = x.gsub(" ", "%20")
Net::HTTP.start(server_path, 80) do |http|
response = http.head("/#{x}")
end
if(response.message == "OK")
return "http://" + server_path + "/" + x
else
return false
end
end
def retrieve
case self.storage_type
when 1
path = Asset.find_on_cdn(self.data)
logger.info("FINDING ON REPO")
if(path)
return path
else
return Asset.local_repo(self.data)
end
when 2
return "http://images.pdcwwe.com/" + self.data
end
#default
return self.data
end
def url
"/assets/get/#{self.id}"
end
# this is the directory path that it takes to find
# this asset
def expand_path
p = []
d = self.asset_directory
while d do
p.push(d.id)
d=d.parent
end
p
end
# sends the file to amazon and returns the url for the model object
def send_to_amazon(filename, file)
#first connect to s3
connect_to_s3()
#upload the file
AWS::S3::S3Object.store(filename,
file,
AmazonConfig::AMAZON_IMAGE_BUCKET_NAME,
:access => :public_read)
#return the url
# REPLACE THIS WITH THE BUCKET NAME WHEN IT IS WORKING
filename
end
# connects to S3
def connect_to_s3
AWS::S3::Base.establish_connection!(
:access_key_id => AmazonConfig::AMAZON_ACCESS_KEY,
:secret_access_key => AmazonConfig::AMAZON_SECRET_ACCESS_KEY
)
end
end
####### author.rb ######
class Author < ActiveRecord::Base
has_many :overviews
belongs_to :asset
end
####overview.rb #####
def Overview.get_small_image(content)
if(!content.author)
return (content.small_image) ? Overview.get_image_path(content.small_image) : ''
else
return content.author.asset.url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment