identifies all uploads with the SHA1 hash of the file content
This commit is contained in:
parent
6ea91b4416
commit
6c4554b941
|
@ -17,11 +17,18 @@ class Upload < ActiveRecord::Base
|
||||||
image_info = FastImage.new(file.tempfile, raise_on_failure: true)
|
image_info = FastImage.new(file.tempfile, raise_on_failure: true)
|
||||||
# compute image aspect ratio
|
# compute image aspect ratio
|
||||||
width, height = ImageSizer.resize(*image_info.size)
|
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!({
|
upload = Upload.create!({
|
||||||
user_id: user_id,
|
user_id: user_id,
|
||||||
original_filename: file.original_filename,
|
original_filename: file.original_filename,
|
||||||
filesize: File.size(file.tempfile),
|
filesize: File.size(file.tempfile),
|
||||||
|
sha: sha,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
url: ""
|
url: ""
|
||||||
|
@ -35,6 +42,8 @@ class Upload < ActiveRecord::Base
|
||||||
|
|
||||||
upload.save
|
upload.save
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
upload
|
upload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue