FIX 附件文件兼容oss地址

This commit is contained in:
jasder duan 2026-03-31 15:31:34 +08:00
parent 860a78c92f
commit b1e666d5c8
3 changed files with 110 additions and 0 deletions

View File

@ -145,3 +145,6 @@ gem 'doorkeeper'
gem 'doorkeeper-jwt'
gem 'gitea-client', '~> 1.4.2'
# 阿里云oss
gem 'aliyun-sdk', '~> 0.8.0'

View File

@ -14,6 +14,8 @@ class AttachmentsController < ApplicationController
# 2. 如果没有cdnsend_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

View File

@ -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