renamed the `sha` column to the proper `sha1`

This commit is contained in:
Régis Hanol 2013-06-17 22:16:14 +02:00
parent 1051b8de84
commit ae3543872c
5 changed files with 28 additions and 13 deletions

View File

@ -38,9 +38,9 @@ class Upload < ActiveRecord::Base
def self.create_for(user_id, file)
# compute the sha
sha = Digest::SHA1.file(file.tempfile).hexdigest
sha1 = Digest::SHA1.file(file.tempfile).hexdigest
# check if the file has already been uploaded
upload = Upload.where(sha: sha).first
upload = Upload.where(sha1: sha1).first
# otherwise, create it
if upload.blank?
@ -53,7 +53,7 @@ class Upload < ActiveRecord::Base
user_id: user_id,
original_filename: file.original_filename,
filesize: File.size(file.tempfile),
sha: sha,
sha1: sha1,
width: width,
height: height,
url: ""
@ -61,7 +61,7 @@ class Upload < ActiveRecord::Base
# make sure we're at the beginning of the file (FastImage is moving the pointer)
file.rewind
# store the file and update its url
upload.url = Upload.store_file(file, sha, image_info, upload.id)
upload.url = Upload.store_file(file, sha1, image_info, upload.id)
# save the url
upload.save
end
@ -69,9 +69,9 @@ class Upload < ActiveRecord::Base
upload
end
def self.store_file(file, sha, image_info, upload_id)
return S3.store_file(file, sha, image_info, upload_id) if SiteSetting.enable_s3_uploads?
return LocalStore.store_file(file, sha, image_info, upload_id)
def self.store_file(file, sha1, image_info, upload_id)
return S3.store_file(file, sha1, image_info, upload_id) if SiteSetting.enable_s3_uploads?
return LocalStore.store_file(file, sha1, image_info, upload_id)
end
def self.uploaded_regex
@ -105,11 +105,11 @@ end
# url :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# sha :string(255)
# sha1 :string(40)
#
# Indexes
#
# index_uploads_on_sha (sha) UNIQUE
# index_uploads_on_sha1 (sha1) UNIQUE
# index_uploads_on_user_id (user_id)
#

View File

@ -0,0 +1,15 @@
class RenameShaColumn < ActiveRecord::Migration
def up
remove_index :uploads, :sha
rename_column :uploads, :sha, :sha1
change_column :uploads, :sha1, :string, limit: 40
add_index :uploads, :sha1, unique: true
end
def down
remove_index :uploads, :sha1
change_column :uploads, :sha1, :string, limit: 255
rename_column :uploads, :sha1, :sha
add_index :uploads, :sha, unique: true
end
end

View File

@ -1,6 +1,6 @@
module LocalStore
def self.store_file(file, sha, image_info, upload_id)
def self.store_file(file, sha1, image_info, upload_id)
clean_name = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16] + ".#{image_info.type}"
url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload_id}"
path = "#{Rails.root}/public#{url_root}"

View File

@ -1,13 +1,13 @@
module S3
def self.store_file(file, sha, image_info, upload_id)
def self.store_file(file, sha1, image_info, upload_id)
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
raise Discourse::SiteSettingMissing.new("s3_access_key_id") if SiteSetting.s3_access_key_id.blank?
raise Discourse::SiteSettingMissing.new("s3_secret_access_key") if SiteSetting.s3_secret_access_key.blank?
@fog_loaded = require 'fog' unless @fog_loaded
remote_filename = "#{upload_id}#{sha}.#{image_info.type}"
remote_filename = "#{upload_id}#{sha1}.#{image_info.type}"
options = S3.generate_options
directory = S3.get_or_create_directory(SiteSetting.s3_upload_bucket, options)

View File

@ -34,7 +34,7 @@ describe Upload do
upload.user_id.should == user_id
upload.original_filename.should == logo.original_filename
upload.filesize.should == File.size(logo.tempfile)
upload.sha.should == Digest::SHA1.file(logo.tempfile).hexdigest
upload.sha1.should == Digest::SHA1.file(logo.tempfile).hexdigest
upload.width.should == 244
upload.height.should == 66
upload.url.should == url