Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

saving to s3

  • Loading branch information...
commit a38e6017653974bbce691c366d5f7cfd7c4fe538 1 parent bce48d9
Arthur PETRY authored
Showing with 28 additions and 2 deletions.
  1. +28 −2 app/models/track.rb
View
30 app/models/track.rb
@@ -1,3 +1,5 @@
+require 'digest/sha1'
+
class Track
# include MongoMapper::Document
# include Timestamp
@@ -16,15 +18,15 @@ class Track
key :seconds #, Integer, :required => true
key :format #, String, :required => true
key :cover #, String
- key :file #, String, :required => true
key :compilation #, Boolean
+ key :sha1
key :user_id #, String#, :required => true
# after_save :set_album
# after_save :move_file
- attr_accessor :file_data, :content_type, :original_path
+ attr_accessor :file_data, :content_type, :original_path, :file
def duration
min = seconds / 60
@@ -41,6 +43,22 @@ def file_data=(data)
set_attributes_from_file
end
+ def save_to_s3
+ content = file_data.read
+ self.sha1 = Digest::SHA1.hexdigest(content)
+ new_object = file_bucket.objects.build(self.sha1)
+ new_object.content = content
+ new_object.content_type = self.content_type
+ new_object.save
+ end
+
+ def save
+ if file_data
+ save_to_s3
+ end
+ super
+ end
+
def move_file
if file_data
path = Rails.root.join('storage', 'tracks', "user_#{user_id}", id[0..1], id[2..3], id[4..-1]+'.'+format)
@@ -164,4 +182,12 @@ def self.want(attributes)
return "Ok, I want it !"
end
+private
+ def s3_service
+ @s3_service ||= S3::Service.new(:access_key_id => ENV['S3_KEY_ID'], :secret_access_key => ENV['S3_SECRET'])
+ end
+
+ def file_bucket
+ @file_bucket ||= s3_service.buckets.find(ENV['S3_BUCKET_PREFIX']+"-audio-files")
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.