identifies all uploads with the SHA1 hash of the file content

This commit is contained in:
Régis Hanol 2013-06-15 10:33:57 +02:00
parent 6ea91b4416
commit 6c4554b941
2 changed files with 28 additions and 13 deletions

View File

@ -17,11 +17,18 @@ class Upload < ActiveRecord::Base
image_info = FastImage.new(file.tempfile, raise_on_failure: true)
# compute image aspect ratio
width, height = ImageSizer.resize(*image_info.size)
# compute the sha
sha = Digest::SHA1.file(file.tempfile).hexdigest
# check if the file has already been uploaded
upload = Upload.where(sha: sha).first
# otherwise, create it
if upload.blank?
upload = Upload.create!({
user_id: user_id,
original_filename: file.original_filename,
filesize: File.size(file.tempfile),
sha: sha,
width: width,
height: height,
url: ""
@ -35,6 +42,8 @@ class Upload < ActiveRecord::Base
upload.save
end
upload
end

View File

@ -0,0 +1,6 @@
class AddShaToUploads < ActiveRecord::Migration
def change
add_column :uploads, :sha, :string, null: true
add_index :uploads, :sha, unique: true
end
end