先上传到服务器,再传到七牛
kind: 1为图片,2为视频
type: 根据业务不同分来处理
文件(model): qiniu_check.rb
外部直接调用: QiniuCheck.media_process
require 'net/http/post/multipart'
class QiniuCheck
class << self
def media_process(media_path, kind, type)
if kind == "1"
self.image_process(media_path)
elsif kind == "2"
self.video_process(media_path, type)
end
end
def image_process(image_path)
rb = %Q({"#{image_path}":$(key),"width":$(imageInfo.width),"height":$(imageInfo.height)})
put_policy = Qiniu::Auth::PutPolicy.new('marry')
put_policy.return_body = rb
token = Qiniu::Auth.generate_uptoken(put_policy)
path = image_path.path
code, result, response_headers = Qiniu::Storage.upload_with_put_policy(
put_policy,
path
)
puts "---"
puts result
result["#{image_path}"] = "http://marry.qiniudn.com/" + result["#{image_path}"]
result
end
def video_process(video_path, type)
put_policy = Qiniu::Auth::PutPolicy.new('marry')
#put_policy.return_body = rb
time = Time.now.to_i
file_name_1 = "#{Random.new(time).rand}" + SecureRandom.hex
file_name_2 = "#{Random.new(time).rand}" + SecureRandom.hex
file_name_base64_1 = Qiniu::Utils.urlsafe_base64_encode("marry:#{file_name_1}.m3u8")
file_name_base64_2 = Qiniu::Utils.urlsafe_base64_encode("marry:#{file_name_2}.m3u8")
if type == "opu"
put_policy.persistent_ops = "avthumb/mp4/vb/1000k/vcodec/libx264/acodec/libfaac/s/640x480;avthumb/mp4/vb/1500k/vcodec/libx264/acodec/libfaac/s/1024x768;avthumb/flv/ar/44100/vb/512k/vcodec/flv/acodec/libmp3lame/s/320x240;avthumb/m3u8/segtime/10/vb/1000k/vcodec/libx264/acodec/libfaac/s/640x480|saveas/#{file_name_base64_1};avthumb/m3u8/segtime/10/vb/1500k/vcodec/libx264/acodec/libfaac/s/1024x768|saveas/#{file_name_base64_2};vframe/jpg/offset/10/rotate/auto"
elsif type == "story"
put_policy.persistent_ops = "avthumb/mp4/vb/1000k/vcodec/libx264/acodec/libfaac/s/640x480;avthumb/mp4/vb/1500k/vcodec/libx264/acodec/libfaac/s/1024x768;avthumb/flv/ar/44100/vb/512k/vcodec/flv/acodec/libmp3lame/s/320x240;avthumb/m3u8/segtime/10/vb/1000k/vcodec/libx264/acodec/libfaac/s/640x480|saveas/#{file_name_base64_1};avthumb/m3u8/segtime/10/vb/1500k/vcodec/libx264/acodec/libfaac/s/1024x768|saveas/#{file_name_base64_2};vframe/jpg/offset/1/rotate/auto"
end
put_policy.persistent_notify_url = APP_HOST + "qiniu/qiniu_persistent_result"
put_policy.expires_in = 600
token = Qiniu::Auth.generate_uptoken(put_policy)
path = video_path.path
code, result, response_headers = Qiniu::Storage.upload_with_put_policy(
put_policy,
path
)
result["#{video_path}"] = "http://marry.qiniudn.com/" + result["key"]
result
end
end
end
直接上传至七牛
//= require 'controls/qiniu_control'
//动态添加媒体
var mediaSubject = function () {
return {
initUploadMedia: function (browse_button, $items, $template, kind, callback) {
// 图片
if (kind == 1) {
var coverImageUploader = new QiniuUploader();
coverImageUploader.upload({
browse_button: browse_button,
uptoken_url: '/qiniu/image_up_token'
}, {
'FileUploaded': function (up, file, info) {
var info = $.parseJSON(info);
$items.loadTemplate($template, {
path: 'http://marry.qiniudn.com/' + info.image_path,
media_path: 'http://marry.qiniudn.com/' + info.image_path,
width: info.width,
height: info.height,
kind: 1
}, {
append: true
});
//回调,判断是否传参,以及参数类型
if(callback && callback instanceof Function) {
callback();
};
}
});
// 视频
}else if(kind == 2){
var detailVideoUploader = new QiniuUploader();
detailVideoUploader.upload({
browse_button: browse_button,
uptoken_url: '/qiniu/video_up_token'
}, {
'FileUploaded': function (up, file, info) {
var info = $.parseJSON(info);
var videoPath = 'http://marry.qiniudn.com/' + info.video_path;
// console.log(info);
$items.loadTemplate($template, {
path: 'http://marry.qiniudn.com/' + info.video_path + "?vframe/jpg/offset/1|imageView/5/w/211/h/161",
media_path: 'http://marry.qiniudn.com/' + info.video_path,
persistent_id: info.persistent_id,
kind: 2
}, {
append: true
});
//回调,判断是否传参,以及参数类型
if(callback && callback instanceof Function) {
callback();
};
}
});
}else{
alert("上传失败");
};
}
}
}();
def video_up_token
# 前端能拿到的参数
return_body = %Q({"video_path":$(key),"hash":$(hash),"persistent_id":$(persistentId)})
# 转码格式及分辨率设置
persistent_options = %w(
vframe/jpg/offset/10
avthumb/mp4/vb/1000k/vcodec/libx264/acodec/libfaac/s/640x480/autoscale/1
avthumb/mp4/vb/1400k/vcodec/libx264/acodec/libfaac/s/1024x768/autoscale/1
avthumb/flv/ar/44100/vb/512k/vcodec/flv/acodec/libmp3lame/s/320x240/autoscale/1
avthumb/m3u8/segtime/10/vb/1000k/vcodec/libx264/acodec/libfaac/s/640x480/autoscale/1
avthumb/m3u8/segtime/10/vb/1500k/vcodec/libx264/acodec/libfaac/s/1024x768/autoscale/1)
# 转码完成后的回调地址 TODO 写入配置文件
persistent_notify_url = "#{APP_HOST}qiniu/qiniu_persistent_result"
uptoken = Qiniu.generate_upload_token(
scope: 'marry',
deadline: (Time.now + 30.seconds).to_i,
:expires_in => 2400,
return_body: return_body,
persistent_ops: persistent_options.join(';'),
persistent_notify_url: persistent_notify_url,
persistent_pipeline: %w(opu picture story other).sample
)
render json: { uptoken: uptoken }
end
<%= javascript_include_tag 'dynamic_add_media.js' %>
<script>
$(function () {
mediaSubject.initUploadMedia('up-pic', $("#media_items"), $("#media_template"), 1, drag);
function drag(){
}
});
</script>