Created
February 13, 2015 12:15
-
-
Save maolion/b066a85e6328e9c591b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
查看状态 : https://portal.qiniu.com/mps/pipeline/{queue id}/job/{object id} | |
提交 : https://portal.qiniu.com/mps/pipeline/{queue id}/job | |
post 字段 | |
--------------------- | |
pipelineId:队列id | |
srcBucket:空间 | |
srcKey:源文件 | |
command:video-Generic-720P | |
dstKey:目标文件 | |
dstBucket:目标空间 | |
callbackUrl: | |
--------------------- | |
删除: https://portal.qiniu.com/bucket/我什么也不知道/files/0/delete | |
post 字段 | |
--------------------- | |
bucket : 目标仓库 | |
keys : [key列表] | |
--------------------- | |
*/ | |
var next =function (opt){ | |
var DEF_OPT = { | |
pipelineId : '1380386196.a', | |
srcBucket : '我什么也不知道', | |
command : 'video-Generic-720P', | |
destBucket : '我什么也不知道', | |
callbackUrl : '', | |
dstKey : '$1.mp4' | |
}; | |
var els = {}; | |
var opt = $.extend({}, DEF_OPT, opt); | |
var | |
failed = [] | |
; | |
var RE_FILENAME = /^([\s\S]+)(\.[^\.]+)$/; | |
function remove(file, bucket){ | |
if (!els.hasOwnProperty(file)) return; | |
$.ajax({ | |
url : 'https://portal.qiniu.com/bucket/我什么也不知道/files/0/delete', | |
type : 'POST', | |
dataType : 'json', | |
data : { | |
bucket : bucket || opt.srcBucket, | |
keys : [file] | |
}, | |
success : function(res) { | |
if (!res.error) { | |
els[file].css({ | |
textDecoration : 'line-through', | |
color : '#ccc' | |
}); | |
console.log('删除资源成功:', file); | |
} else { | |
els[file].css('color', 'yellow'); | |
console.log('删除资源失败:', file, res.failed_keys); | |
} | |
}, | |
error : function(){ | |
els[file].css('color', 'yellow'); | |
console.log('删除资源失败:', file); | |
} | |
}); | |
}; | |
var R_STATUS = /状态.*>([^>]+)<\/div><\/div>/; | |
function listenStatus(file, oid, pid, callback) { | |
$.get('https://portal.qiniu.com/mps/pipeline/'+pid+'/job/'+oid, function(res){ | |
var r = (res||'').match(R_STATUS); | |
if (r && r[1] === '成功') { | |
els[file].css('color', 'blue'); | |
console.log('任务成功:', file); | |
callback(file, oid, pid); | |
return; | |
} | |
setTimeout(function(){ | |
listenStatus(file, oid, pid, callback); | |
}, 30000); | |
}).fail(function(){ | |
els[file].css('color', 'red'); | |
console.log('任务失败:', file); | |
failed.push(file); | |
}); | |
} | |
function next(file, nopt){ | |
if (arguments.length) { | |
if (typeof file !== 'string') { | |
nopt = file; | |
file = failed.shift(); | |
} | |
} else { | |
file = failed.shift(); | |
} | |
if (!els.hasOwnProperty(file)) return; | |
nopt = nopt ? $.extend({}, opt, nopt) : opt; | |
nopt.srcKey = file; | |
nopt.dstKey = file.replace(RE_FILENAME, DEF_OPT.dstKey); | |
els[file].css('color', '#ccc'); | |
$.ajax({ | |
url : 'https://portal.qiniu.com/mps/pipeline/'+nopt.pipelineId+'/job', | |
type : 'POST', | |
dataType : 'json', | |
data : nopt, | |
success : function(res){ | |
if (res.Success) { | |
els[file].css('color', 'green'); | |
console.log('创建任务成功:', file); | |
listenStatus(file, res.Data, nopt.pipelineId, function(){ | |
remove(file, nopt.srcBucket); | |
}); | |
} else { | |
failed.push(file); | |
els[file].css('color', 'red'); | |
console.error('创建任务失败:', file); | |
} | |
}, | |
error : function(){ | |
failed.push(file); | |
els[file].css('color', 'red'); | |
console.error('创建任务失败:', file, '(请求失败!)'); | |
} | |
}); | |
return next; | |
} | |
$('.files-table input:checked').closest('tr').find('td.file-name span.name').each(function(i, item){ | |
var | |
item = $(item), | |
file = item.text() | |
; | |
els[file] = item; | |
next(file); | |
}); | |
return next; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
屌爆了!