fixed upload->post converion process

This commit is contained in:
albert
2010-03-17 19:20:44 -04:00
parent ca8be10ab9
commit dcf8d0df4c
20 changed files with 346 additions and 100 deletions

View File

@@ -2,6 +2,7 @@ class Post < ActiveRecord::Base
attr_accessor :updater_id, :updater_ip_addr, :old_tag_string
after_destroy :delete_files
after_destroy :delete_favorites
after_destroy :update_tag_post_counts
after_save :create_version
before_save :merge_old_tags
before_save :normalize_tags
@@ -9,6 +10,7 @@ class Post < ActiveRecord::Base
before_save :update_tag_post_counts
before_save :set_tag_counts
belongs_to :updater, :class_name => "User"
belongs_to :approver, :class_name => "User"
has_one :unapproval, :dependent => :destroy
has_one :upload, :dependent => :destroy
has_one :moderation_detail, :class_name => "PostModerationDetail", :dependent => :destroy
@@ -16,6 +18,7 @@ class Post < ActiveRecord::Base
has_many :votes, :class_name => "PostVote", :dependent => :destroy
has_many :notes, :dependent => :destroy
validates_presence_of :updater_id, :updater_ip_addr
validates_uniqueness_of :md5
attr_accessible :source, :rating, :tag_string, :old_tag_string, :updater_id, :updater_ip_addr, :last_noted_at
module FileMethods
@@ -74,6 +77,10 @@ class Post < ActiveRecord::Base
file_url
end
end
def is_image?
file_ext =~ /jpg|gif|png/
end
end
module ImageMethods
@@ -168,6 +175,12 @@ class Post < ActiveRecord::Base
increment_tags = tag_array - tag_array_was
execute_sql("UPDATE tags SET post_count = post_count - 1 WHERE name IN (?)", decrement_tags) if decrement_tags.any?
execute_sql("UPDATE tags SET post_count = post_count + 1 WHERE name IN (?)", increment_tags) if increment_tags.any?
decrement_tags.each do |tag|
expire_cache(tag)
end
increment_tags.each do |tag|
expire_cache(tag)
end
end
def set_tag_counts
@@ -501,15 +514,32 @@ class Post < ActiveRecord::Base
module CountMethods
def fast_count(tags)
Cache.get("pfc:#{Cache.sanitize(tags)}", 24.hours) do
Post.find_by_tags(tags).count
end
count = Cache.get("pfc:#{Cache.sanitize(tags)}")
return count unless count.nil?
count = Post.find_by_tags(tags).count
expiry = (count < 100) ? 0 : (count * 4).minutes
Cache.put("pfc:#{Cache.sanitize(tags)}", count, expiry)
count
end
def fast_delete_count(tags)
Cache.get("pfdc:#{Cache.sanitize(tags)}", 24.hours) do
Post.find_by_tags("#{tags} status:deleted").count
count = Cache.get("pfdc:#{Cache.sanitize(tags)}")
return count unless count.nil?
count = Post.find_by_tags("#{tags} status:deleted").count
expiry = (count < 100) ? 0 : (count * 4).minutes
Cache.put("pfc:#{Cache.sanitize(tags)}", count, expiry)
count
end
end
module CacheMethods
def expire_cache(tag_name)
if Post.fast_count("") < 1000
Cache.delete("pfc:")
Cache.delete("pfdc:")
end
Cache.delete("pfc:#{Cache.sanitize(tag_name)}")
Cache.delete("pfdc:#{Cache.sanitize(tag_name)}")
end
end
@@ -525,9 +555,14 @@ class Post < ActiveRecord::Base
extend SearchMethods
include VoteMethods
extend CountMethods
include CacheMethods
def reload(options = nil)
super
reset_tag_array_cache
end
def presenter
@presenter ||= PostPresenter.new(self)
end
end

View File

@@ -18,6 +18,12 @@ class Upload < ActiveRecord::Base
raise
end
end
# Because uploads are processed serially, there's no race condition here.
def validate_md5_uniqueness
md5_post = Post.find_by_md5(md5)
merge_tags(md5_post) if md5_post
end
def validate_file_exists
unless File.exists?(file_path)
@@ -51,6 +57,7 @@ class Upload < ActiveRecord::Base
self.file_ext = content_type_to_file_ext(content_type)
validate_file_content_type
calculate_hash(file_path)
validate_md5_uniqueness
validate_md5_confirmation
calculate_file_size(file_path)
calculate_dimensions(file_path) if has_dimensions?
@@ -85,6 +92,15 @@ class Upload < ActiveRecord::Base
end
end
end
def merge_tags(post)
post.tag_string += " #{tag_string}"
post.updater_id = uploader_id
post.updater_ip_addr = uploader_ip_addr
post.save
update_attribute(:status, "duplicate: #{post.id}")
raise
end
end
module FileMethods
@@ -274,4 +290,8 @@ class Upload < ActiveRecord::Base
include FilePathMethods
include CgiFileMethods
include StatusMethods
def presenter
@presenter ||= UploadPresenter.new(self)
end
end