diff --git a/Gemfile b/Gemfile index 65a0dbfb6..402bcd0c7 100644 --- a/Gemfile +++ b/Gemfile @@ -145,3 +145,6 @@ gem 'doorkeeper' gem 'doorkeeper-jwt' gem 'gitea-client', '~> 1.4.2' + +# 阿里云oss +gem 'aliyun-sdk', '~> 0.8.0' diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 0cdba3847..e928e378c 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -14,6 +14,8 @@ class AttachmentsController < ApplicationController # 2. 如果没有cdn,send_file if @file.cloud_url.present? update_downloads(@file) + + cloud_url = @file.get_cloud_url redirect_to @file.cloud_url and return end @@ -258,4 +260,25 @@ class AttachmentsController < ApplicationController end end + def get_cloud_url(disposition="attachment") + if (self.container_type == "Shixun" && [11, 12, 13].include?(self.attachtype)) || + ((["VirtualClassroomGoodLesson", "VirtualClassroomDiscussion", "Course"].include?(self.container_type)) && self.attachtype == 11) || + (self.container_type == "VirtualClassroomGoodLesson" && self.attachtype == 30) || + (self.container_type == "VirtualClassroomDiscussion" && self.attachtype == 31) + cloud_url = self.oss_absolute_url + elsif self.digest.start_with?(SLUG_PREFIX) + bucket = %w[SmartPlan].include?(self.container_type) ? OSS.client.get_bucket(OSS.bucket_big_files) : OSS.client.get_bucket(OSS.attachments_oss_bucket) + cloud_url = bucket.object_url(self.cloud_url, true, 10 * 3600 * 24, {"response-content-disposition": "attachment;\"filename=#{URI.encode(self.filename.strip)}\";attachment_id=#{self.id}"}) + elsif ["Stage", "Course"].include?(self.container_type) && OSS.client.get_bucket(OSS.bucket_big_files).object_exists?(self.cloud_url) + cloud_url = ApplicationController.helpers.cdn_a_auth_url(self) + elsif self.cloud_url.include?("http") + cloud_url = self.cloud_url + else + bucket = OSS.client.get_bucket(OSS.attachments_oss_bucket) + cloud_url = bucket.object_url(self.cloud_url, true, 10 * 3600 * 24, {"response-content-disposition": "attachment;\"filename=#{URI.encode(self.filename.strip)}\";attachment_id=#{self.id}"}) + end + cloud_url + end + + end diff --git a/config/initializers/aliyun_oss_init.rb b/config/initializers/aliyun_oss_init.rb new file mode 100644 index 000000000..f20b6407d --- /dev/null +++ b/config/initializers/aliyun_oss_init.rb @@ -0,0 +1,84 @@ +require 'aliyun/sts' +require 'aliyun/oss' +module OSS + def self.client + unless @client + #Aliyun::OSS::Logging.set_log_file("#{Rails.root}/log/oss_sdk.log") + @client = Aliyun::OSS::Client.new( + endpoint: aliyun_vod_config['endpoint'], + cname: aliyun_vod_config['cname'], + access_key_id: aliyun_vod_config['access_key_id'], + access_key_secret: aliyun_vod_config['access_key_secret'] + ) + end + + @client + end + + def self.public_bucket_client + @public_bucket_client = Aliyun::OSS::Client.new( + endpoint: aliyun_vod_config['public_bucket_endpoint'], + cname: aliyun_vod_config['cname'], + access_key_id: aliyun_vod_config['access_key_id'], + access_key_secret: aliyun_vod_config['access_key_secret'] + ) + end + + def self.end_point + aliyun_vod_config['endpoint'] + end + + def self.access_key_id + aliyun_vod_config['access_key_id'] + end + + def self.access_key_secret + aliyun_vod_config['access_key_secret'] + end + + def self.bucket + aliyun_vod_config['bucket'] + end + + # 公共库 + def self.public_bucket + aliyun_vod_config['public_bucket'] + end + + # attachments 附件存储库 + def self.attachments_oss_bucket + aliyun_vod_config['attachments_oss'] + end + + #实训大数据集 + def self.bucket_big_files + aliyun_vod_config['bucket_big_files'] + end + + def self.region + aliyun_vod_config['region'] + end + + def self.aliyun_vod_config + Rails.application.config_for(:configuration)['aliyun_oss'] + end +end + +module STS + def self.client + unless @sts_client + config = Rails.application.config_for(:configuration) + aliyun_vod_config = config['aliyun_oss'] + @sts_client = Aliyun::STS::Client.new( + access_key_id: aliyun_vod_config['access_key_id'], + access_key_secret: aliyun_vod_config['access_key_secret']) + + end + + @sts_client + end + + def self.role_arn + "acs:ram::1829848226361863:role/oss-anticheat".freeze + end +end