Files
danbooru/app/models/amazon_backup.rb
r888888888 79378f9210 fix typo
2015-07-16 15:24:57 -07:00

51 lines
1.5 KiB
Ruby

require 'base64'
require 'digest/md5'
class AmazonBackup < ActiveRecord::Base
attr_accessible :last_id
def self.last_id
first.last_id
end
def self.update_id(new_id)
first.update_column(:last_id, new_id)
end
def self.execute
last_id = AmazonBackup.last_id
credentials = Aws::Credentials.new(Danbooru.config.amazon_s3_access_key_id, Danbooru.config.amazon_s3_secret_access_key)
Aws.config.update({
region: "us-east-1",
credentials: credentials
})
client = Aws::S3::Client.new
bucket = Danbooru.config.amazon_s3_bucket_name
Post.where("id > ?", last_id).limit(1000).order("id").each do |post|
if File.exists?(post.file_path)
base64_md5 = Digest::MD5.base64digest(File.read(post.file_path))
key = File.basename(post.file_path)
body = open(post.file_path, "rb")
client.put_object(bucket: bucket, key: key, body: body, content_md5: base64_md5)
end
if post.has_preview? && File.exists?(post.preview_file_path)
key = "preview/#{post.md5}.jpg"
body = open(post.preview_file_path, "rb")
client.put_object(bucket: bucket, key: key, body: body)
end
if File.exists?(post.large_file_path)
key = "large/#{post.md5}.#{post.large_file_ext}"
body = open(post.large_file_path, "rb")
client.put_object(bucket: bucket, key: key, body: body)
end
AmazonBackup.update_id(post.id)
end
rescue Exception => x
# probably some network error, retry next time
end
end