discourse/app/models/upload.rb

96 lines
3.0 KiB
Ruby
Raw Normal View History

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
2013-04-07 23:52:46 +08:00
# Create an upload given a user, file and topic
def self.create_for(user_id, file, topic_id)
return create_on_imgur(user_id, file, topic_id) if SiteSetting.enable_imgur?
return create_on_s3(user_id, file, topic_id) if SiteSetting.enable_s3_uploads?
return create_locally(user_id, file, topic_id)
2013-02-06 03:16:51 +08:00
end
2013-04-07 23:52:46 +08:00
# Store uploads on imgur
def self.create_on_imgur(user_id, file, topic_id)
2013-02-06 03:16:51 +08:00
@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)
2013-04-07 23:52:46 +08:00
Upload.create!({
user_id: user_id,
topic_id: topic_id,
original_filename: file.original_filename
}.merge!(info))
2013-02-06 03:16:51 +08:00
end
2013-04-07 23:52:46 +08:00
# Store uploads on s3
def self.create_on_s3(user_id, file, topic_id)
2013-02-06 03:16:51 +08:00
@fog_loaded = require 'fog' unless @fog_loaded
tempfile = file.tempfile
2013-04-07 23:52:46 +08:00
upload = Upload.new(user_id: user_id,
2013-02-06 03:16:51 +08:00
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, raise_on_failure: true)
2013-02-06 03:16:51 +08:00
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
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 = "//#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{path}/#{remote_filename}"
2013-03-31 00:56:25 +08:00
2013-02-06 03:16:51 +08:00
upload.save
upload
end
2013-04-07 23:52:46 +08:00
def self.create_locally(user_id, file, topic_id)
upload = Upload.create!({
user_id: user_id,
topic_id: topic_id,
url: "",
filesize: File.size(file.tempfile),
original_filename: file.original_filename
})
# populate the rest of the info
clean_name = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16]
image_info = FastImage.new(file.tempfile, raise_on_failure: true)
2013-04-07 23:52:46 +08:00
clean_name += ".#{image_info.type}"
url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload.id}"
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|
f.write File.read(file.tempfile)
end
upload.url = Discourse::base_uri + "#{url_root}/#{clean_name}"
upload.save
upload
end
2013-02-06 03:16:51 +08:00
end