UNPKG

react-native-omplayer

Version:

video player for react-native

150 lines (134 loc) 4.76 kB
// 下载https://resource.ibrainbaby.cn/FFmpeg.zip,解压到ios/FFmpeg下 const crypto = require('crypto'); const fs = require('fs'); const http = require('https'); const path = require('path'); var ProgressBar = require('progress'); var adm_zip = require('adm-zip'); const tZipUrl = 'https://resource.ibrainbaby.cn/FFmpeg.zip'; const downloadPath = path.resolve(__dirname, '../ios/FFmpeg.zip') // const zipMD5 = '653ad60530b0ff2853408a9cea5ee5e1'; const zipMD5 = 'c53541150b603fdab683117d7b7faba4'; let retry = 0; const MAX_RETRY = 2; const md5File = function (path, callback) { fs.readFile(path, function(err, data) { if (err) { console.log(err); return; } var md5Value= crypto.createHash('md5').update(data, 'utf8').digest('hex'); callback(md5Value); }); } const unzipFile = function() { console.log('文件开始解压') const zipPath = path.resolve(__dirname, '../ios') var unzip = new adm_zip(downloadPath); unzip.extractAllTo(zipPath); deleteFolderRecursive(path.resolve(zipPath, './__MACOSX')) console.log('压缩成功'); fs.exists(downloadPath, (exists) => { if (exists) { fs.unlinkSync(downloadPath); } }) } const deleteFolderRecursive = function(path) { if( fs.existsSync(path) ) { fs.readdirSync(path).forEach(function(file) { var curPath = path + "/" + file; if(fs.statSync(curPath).isDirectory()) { // recurse deleteFolderRecursive(curPath); } else { // delete file fs.unlinkSync(curPath); } }); fs.rmdirSync(path); } }; const downLoadTask = function () { let receivedBytes = 0; let totalBytes = 0; let bar = null; fs.exists(downloadPath, (exists) => { if (exists) { md5File(downloadPath, (md5Hash) => { if (md5Hash === zipMD5) { unzipFile(); } else { retryFn(); } }); } else { const req = http.get(tZipUrl, (res) => { console.log('下载中。。。'); let chunks = []; res.on('data', (chunk) => { chunks.push(chunk); receivedBytes += chunk.length; bar.tick(chunk.length) }); res.on('end', (data) => { var data = null; switch(chunks.length) { case 0: data = new Buffer.alloc(0); break; case 1: data = chunks[0]; break; default: data = new Buffer.alloc(receivedBytes); for (var i = 0, pos = 0, l = chunks.length; i < l; i++) { var chunk = chunks[i]; chunk.copy(data, pos); pos += chunk.length; } break; } fs.writeFile(downloadPath, data, function(err) { if (err) { console.log('保存失败'); retryFn(); } else { console.log('保存成功'); md5File(downloadPath, (md5Hash) => { if (md5Hash === zipMD5) { unzipFile(); } else { retryFn(); } }); } } ) }); res.on('error', function(err) { retryFn(); }); }) req.on('response', (data) => { totalBytes = parseInt(data.headers['content-length'], 10); bar = new ProgressBar(' downloading [:bar] :rate/bps :percent :etas', { complete: '=', incomplete: ' ', width: 20, total: totalBytes }); }); } }); } const retryFn = function() { fs.exists(downloadPath, (exists) => { if (exists) { fs.unlinkSync(downloadPath); } retry += 1; if (retry < MAX_RETRY) { console.log(`请求失败..正在第${retry}次尝试`); downLoadTask(); } else { console.log('请求失败'); } }) } downLoadTask();