UNPKG

owhat-job

Version:

owhat红包清理

122 lines (121 loc) 6.02 kB
import V from 'gcl'; import qiniu from 'qiniu'; import OSS from 'ali-oss'; export const upload = { //文件上传 getduration: function(path) { if (V.isValid(path) && path.indexOf('?d=') >= 0) { let dur = path.split('?d=')[1]; if (dur.indexOf(':') >= 0) { let dur2 = parseInt(dur.split(':')[1]); if (dur2 >= 60) return V.format("{min}:{sec}", { min: (Math.floor(dur2 / 60) < 10 ? '0' : '') + Math.floor(dur2 / 60), sec: (Math.floor(dur2 % 60) < 10 ? '0' : '') + Math.floor(dur2 % 60) }); else return dur; } else return dur.indexOf('-') >= 0 ? ((dur.split('-').length > 2 ? '' : '00:') + dur.replace(/-/g, ':').replace(/:\d{6}/g, '')) : ''; } else return ''; }, /** * 获取七牛Token */ getToken: function(ak, sk, bucket) { const { _, __ } = pri(this); if (!__.mac) __.mac = new qiniu.auth.digest.Mac(ak, sk); if (!__.putPolicy) __.putPolicy = new qiniu.rs.PutPolicy({ scope: bucket, returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}' }); return __.putPolicy.uploadToken(__.mac); }, /** * 获取七牛Token * @param {string} ak * @param {string} sk * @param {string} bucket * @param {string} path * @param {string} notify * @param {string} viewpipe */ getVideoToken: function(ak, sk, bucket, path, notify, viewpipe) { try { const { _, __ } = pri(this); if (!__.mac) __.mac = new qiniu.auth.digest.Mac(ak, sk); // //avthumb/mp4/vcodec/libx264/acodec/libmp3lame/s/640x360/autoscale/1/ab/64k const config = { scope: bucket, deadline: new Date().add('h', 1).getTime(), persistentOps: 'avthumb/mp4/vcodec/libx264/acodec/libfaac/s/640x360/autoscale/1/ab/64k|saveas/' + qiniu.util.urlsafeBase64Encode(bucket + ":qiniu/" + path) + `;vsample/jpg/ss/1/t/30/s/640x360/interval/5/pattern/${qiniu.util.urlsafeBase64Encode(path.replace('.mp4','-$(count).jpg'))}`, persistentNotifyUrl: notify, returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"persistentId":"${persistentId}","bucket":"$(bucket)","name":"$(x:name)"}' }; if (!!viewpipe) config.persistentPipeline = viewpipe; console.log('getVideoToken1', "qiniu/" + path); console.log('getVideoToken', V.toJsonString(config)); return new qiniu.rs.PutPolicy(config).uploadToken(__.mac); } catch (e) { console.log('getVideoToken', e.message); throw e; } }, uploadfile: async function(upload, url, pro) { let User = { upload: upload }; let ret = []; { const now = new Date(); switch (pro.FileProvider + '') { case '0': //七牛 { var config = new qiniu.conf.Config() config.zone = qiniu.zone[pro.FileRegion || 'Zone_z1']; //设置华北地区 const formUploader = new qiniu.form_up.FormUploader(config); const putExtra = new qiniu.form_up.PutExtra(); const qiniupast = await V.callback(call => { formUploader.putFile(extend.toLowerCase() == 'mp4' ? __.getVideoToken(pro.FileID, pro.FileSec, pro.FileScope, User.upload, pro.FileNotify || '', pro.FilePipe) : __.getToken(pro.FileID, pro.FileSec, pro.FileScope), url, User.upload, putExtra, function(respErr, respBody, respInfo) { if (respErr) { call(respErr); } if (respInfo.statusCode == 200) { call(null, respBody); } else { call(new Error(`${respInfo.statusCode}:${V.toJsonString(respBody)}`)); } }); }); const info = V.json(extend.toLowerCase() == 'mp4' ? await V.Net.get(ret[ret.length - 1] + '?avinfo') : '{}'); if (info['format'] && info['format']['duration']) { var dur = Math.round(parseFloat(info['format']['duration'])); ret[ret.length - 1] += '?d=' + V.format("{min}:{sec}", { min: (dur < 600 ? '0' : '') + Math.floor(dur / 60), sec: (dur % 60 < 10 ? "0" : '') + dur % 60 }); } if (info['streams'] && info['streams'][0]) { ret[ret.length - 1] += (info['format'] && info['format']['duration'] ? '&' : '?') + 's=' + V.format("{width}:{height}", info['streams'][0]); } } break; case '1': { //上传完成后 不知道 是否可以从公网访问 let client = new OSS({ region: pro.FileRegion, bucket: pro.FileScope, accessKeyId: pro.FileID, accessKeySecret: pro.FileSec, secure: true, }); await client.put(url, User.upload); } break; default: throw new Error('暂时不支持此文件提供商:' + pro.FileProvider); } } return ret; } }; export default { upload }; var pri = V.pris();