2013-02-06 03:16:51 +08:00
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
class Upload < ActiveRecord::Base
|
|
|
|
# attr_accessible :title, :body
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
validates_presence_of :filesize
|
|
|
|
validates_presence_of :original_filename
|
|
|
|
|
|
|
|
|
|
|
|
# Create an upload given a user, file and optional topic_id
|
|
|
|
def self.create_for(user, file, topic_id = nil)
|
|
|
|
# TODO: Need specs/tests for this functionality
|
|
|
|
return create_on_imgur(user, file, topic_id) if SiteSetting.enable_imgur?
|
|
|
|
return create_on_s3(user, file, topic_id) if SiteSetting.enable_s3_uploads?
|
|
|
|
return create_locally(user, file, topic_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Store uploads on s3
|
|
|
|
def self.create_on_imgur(user, file, topic_id)
|
|
|
|
|
|
|
|
@imgur_loaded = require 'imgur' unless @imgur_loaded
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
info = Imgur.upload_file(file)
|
|
|
|
Upload.create!({user_id: user.id,
|
|
|
|
topic_id: topic_id,
|
2013-02-07 23:45:24 +08:00
|
|
|
original_filename: file.original_filename}.merge!(info))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_locally(user, file, topic_id)
|
|
|
|
upload = Upload.create!(user_id: user.id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
url: "",
|
|
|
|
filesize: File.size(file.tempfile),
|
2013-02-07 23:45:24 +08:00
|
|
|
original_filename: file.original_filename)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
# populate the rest of the info
|
2013-02-15 01:08:37 +08:00
|
|
|
clean_name = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16]
|
2013-02-06 03:16:51 +08:00
|
|
|
image_info = FastImage.new(file.tempfile)
|
|
|
|
clean_name += ".#{image_info.type}"
|
2013-02-07 23:45:24 +08:00
|
|
|
url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload.id}"
|
2013-02-06 03:16:51 +08:00
|
|
|
path = "#{Rails.root}/public#{url_root}"
|
|
|
|
upload.width, upload.height = ImageSizer.resize(*image_info.size)
|
|
|
|
FileUtils.mkdir_p path
|
|
|
|
# not using cause mv, cause permissions are no good on move
|
|
|
|
File.open("#{path}/#{clean_name}", "wb") do |f|
|
2013-02-07 23:45:24 +08:00
|
|
|
f.write File.read(file.tempfile)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
upload.url = "#{url_root}/#{clean_name}"
|
|
|
|
upload.save
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
upload
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_on_s3(user, file, topic_id)
|
|
|
|
|
|
|
|
@fog_loaded = require 'fog' unless @fog_loaded
|
|
|
|
|
|
|
|
tempfile = file.tempfile
|
|
|
|
|
|
|
|
upload = Upload.new(user_id: user.id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
filesize: File.size(tempfile),
|
2013-02-07 23:45:24 +08:00
|
|
|
original_filename: file.original_filename)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
image_info = FastImage.new(tempfile)
|
|
|
|
blob = file.read
|
|
|
|
sha1 = Digest::SHA1.hexdigest(blob)
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
Fog.credentials_path = "#{Rails.root}/config/fog_credentials.yml"
|
|
|
|
fog = Fog::Storage.new(provider: 'AWS')
|
|
|
|
|
|
|
|
remote_filename = "#{sha1[2..-1]}.#{image_info.type}"
|
|
|
|
path = "/uploads/#{sha1[0]}/#{sha1[1]}"
|
|
|
|
location = "#{SiteSetting.s3_upload_bucket}#{path}"
|
|
|
|
directory = fog.directories.create(key: location)
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
Rails.logger.info "#{blob.size.inspect}"
|
2013-02-07 23:45:24 +08:00
|
|
|
file = directory.files.create(key: remote_filename,
|
|
|
|
body: tempfile,
|
2013-02-06 03:16:51 +08:00
|
|
|
public: true,
|
|
|
|
content_type: file.content_type)
|
|
|
|
upload.width, upload.height = ImageSizer.resize(*image_info.size)
|
|
|
|
upload.url = "#{Rails.configuration.action_controller.asset_host}#{path}/#{remote_filename}"
|
|
|
|
upload.save
|
|
|
|
|
|
|
|
upload
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|