Skip to content

Commit

Permalink
add cdn hosted flag to posts
Browse files Browse the repository at this point in the history
  • Loading branch information
r888888888 committed Feb 8, 2017
1 parent c01f48f commit b57b541
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/models/amazon_backup.rb
Expand Up @@ -21,7 +21,8 @@ def self.execute
region: "us-east-1",
credentials: credentials
})
client = Aws::S3::Client.new
logger = Logger.new(STDOUT)
client = Aws::S3::Client.new(logger: logger)
bucket = Danbooru.config.aws_s3_bucket_name

Post.where("id > ?", last_id).limit(1000).order("id").each do |post|
Expand Down
7 changes: 6 additions & 1 deletion app/models/post.rb
Expand Up @@ -105,7 +105,11 @@ def file_name
end

def file_url
"/data/#{seo_tag_string}#{file_path_prefix}#{md5}.#{file_ext}"
if cdn_hosted?
Danbooru.config.danbooru_s3_base_url + "/#{file_path_prefix}#{md5}.#{file_ext}"
else
"/data/#{seo_tag_string}#{file_path_prefix}#{md5}.#{file_ext}"
end
end

def large_file_url
Expand Down Expand Up @@ -1709,6 +1713,7 @@ def update_iqdb

BOOLEAN_ATTRIBUTES = %w(
has_embedded_notes
cdn_hosted
)
has_bit_flags BOOLEAN_ATTRIBUTES

Expand Down
21 changes: 21 additions & 0 deletions lib/tasks/images.rake
@@ -1,6 +1,27 @@
require 'danbooru_image_resizer/danbooru_image_resizer'

namespace :images do
desc "Enable CDN"
task :enable_cdn, [:min_id, :max_id] => :environment do |t, args|
CurrentUser.scoped(User.admins.first, "127.0.0.1") do
credentials = Aws::Credentials.new(Danbooru.config.aws_access_key_id, Danbooru.config.aws_secret_access_key)
Aws.config.update({
region: "us-east-1",
credentials: credentials
})
client = Aws::S3::Client.new
bucket = Danbooru.config.aws_s3_bucket_name

Post.where("id >= ? and id <= ?", args[:min_id], args[:max_id]).find_each do |post|
post.cdn_hosted = true
post.save
key = File.basename(post.file_path)
client.copy_object(bucket: bucket, key: key, acl: "public-read", storage_class: "STANDARD", copy_source: "/#{bucket}/#{key}", metadata_directive: "COPY")
# client.put_object(bucket: bucket, key: key, body: body, content_md5: base64_md5, acl: "public-read", storage_class: "STANDARD")
end
end
end

desc "Redownload an image from Pixiv"
task :download_pixiv => :environment do
post_id = ENV["id"]
Expand Down

0 comments on commit b57b541

Please sign in to comment.