|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const SparkMD5 = require('spark-md5'); |
| 5 | +const chalk = require('chalk'); |
| 6 | +const prompts = require('prompts'); |
| 7 | +const path = require('path'); |
| 8 | +const FormData = require('form-data'); |
| 9 | +require('winston-daily-rotate-file'); |
| 10 | +const logger = require('../lib/log'); |
| 11 | +const ProgressBar = require('progress'); |
| 12 | +const { CHUNK_SIZE } = require('../lib/constants'); |
| 13 | +const { generateAuthorization, getRegistryInfo } = require('../lib/utils'); |
| 14 | +const { getExistChunks: _getExistChunks, uploadChunk: _uploadChunk, uploadSuccess: _uploadSuccess } = require('../lib/request'); |
| 15 | + |
| 16 | +const argv = require('../lib/argv'); |
| 17 | +const { requestUrl, version } = getRegistryInfo(argv.registry); |
| 18 | + |
| 19 | +let Authorization = ''; |
| 20 | +let md5 = ''; |
| 21 | +let uploadId = ''; |
| 22 | +let fileSize = 0; |
| 23 | + |
| 24 | +process.on('uncaughtException', error => { |
| 25 | + console.log(chalk.red('\n程序发生了一些异常,请稍后重试\n')); |
| 26 | + logger.error(error.stack); |
| 27 | +}) |
| 28 | + |
| 29 | +const upload = async (filePath, parts = []) => { |
| 30 | + const totalChunk = Math.ceil(fileSize / CHUNK_SIZE); |
| 31 | + |
| 32 | + const bar = new ProgressBar(':bar [:current/:total] :percent', { total: totalChunk }); |
| 33 | + const uploadChunk = async (currentChunk, currentChunkIndex, parts, isRetry) => { |
| 34 | + if (parts.some(({ partNumber, size }) => partNumber === currentChunkIndex && size === currentChunk.length)) { |
| 35 | + bar.tick(); |
| 36 | + return Promise.resolve(); |
| 37 | + } |
| 38 | + |
| 39 | + const form = new FormData(); |
| 40 | + form.append('chunk', currentChunk, { |
| 41 | + filename: requestUrl.replace(/^http(s)?:\/\/.+?\/.+?\/.+?\//, '') |
| 42 | + }); |
| 43 | + try { |
| 44 | + await _uploadChunk(requestUrl, { |
| 45 | + uploadId, |
| 46 | + version, |
| 47 | + partNumber: currentChunkIndex, |
| 48 | + size: currentChunk.length, |
| 49 | + form |
| 50 | + }, { |
| 51 | + headers: form.getHeaders(), |
| 52 | + Authorization |
| 53 | + }); |
| 54 | + bar.tick(); |
| 55 | + } catch (error) { |
| 56 | + logger.error(error.message); |
| 57 | + logger.error(error.stack); |
| 58 | + if (['ECONNREFUSED', 'ECONNRESET', 'ENOENT'].includes(error.code)) { |
| 59 | + // 没有重试过就重试一次 |
| 60 | + if (!isRetry) { |
| 61 | + logger.warn('retry') |
| 62 | + logger.warn(error.code); |
| 63 | + await uploadChunk(currentChunk, currentChunkIndex, parts, true); |
| 64 | + } else { |
| 65 | + console.log(chalk.red('网络连接异常,请重新执行命令继续上传')); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + } else { |
| 69 | + console.log(chalk.red((error.response && error.response.data) || error.message)); |
| 70 | + process.exit(1); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + console.log(`\n开始上传\n`) |
| 76 | + logger.info('开始上传') |
| 77 | + |
| 78 | + try { |
| 79 | + for (let currentChunkIndex = 1; currentChunkIndex <= totalChunk; currentChunkIndex++) { |
| 80 | + const start = (currentChunkIndex - 1) * CHUNK_SIZE; |
| 81 | + const end = ((start + CHUNK_SIZE) >= fileSize) ? fileSize : start + CHUNK_SIZE - 1; |
| 82 | + const stream = fs.createReadStream(filePath, { start, end }) |
| 83 | + let buf = []; |
| 84 | + await new Promise((resolve) => { |
| 85 | + stream.on('data', data => { |
| 86 | + buf.push(data) |
| 87 | + }) |
| 88 | + stream.on('error', error => { |
| 89 | + reject('读取文件分片异常,请重新执行命令继续上传'); |
| 90 | + }) |
| 91 | + stream.on('end', async () => { |
| 92 | + await uploadChunk(Buffer.concat(buf), currentChunkIndex, parts); |
| 93 | + buf = null; |
| 94 | + resolve(); |
| 95 | + }) |
| 96 | + }).catch(error => { |
| 97 | + throw Error(error) |
| 98 | + }) |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + logger.error(error.message); |
| 102 | + logger.error(error.stack); |
| 103 | + console.log(chalk(error.message)); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + const res = await _uploadSuccess(requestUrl, { |
| 109 | + version, |
| 110 | + uploadId, |
| 111 | + fileSize, |
| 112 | + fileTag: md5 |
| 113 | + }, { |
| 114 | + Authorization |
| 115 | + }); |
| 116 | + if (res.code) { |
| 117 | + throw (res.message); |
| 118 | + } |
| 119 | + } catch (error) { |
| 120 | + logger.error(error.message); |
| 121 | + logger.error(error.stack); |
| 122 | + console.log(chalk.red((error.response && error.response.data) || error.message)); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + console.log(chalk.green(`\n上传完毕\n`)) |
| 127 | + logger.info('************************ 上传完毕 ************************') |
| 128 | +} |
| 129 | + |
| 130 | +const getFileMD5Success = async (filePath) => { |
| 131 | + try { |
| 132 | + const res = await _getExistChunks(requestUrl, { |
| 133 | + version, |
| 134 | + fileTag: md5 |
| 135 | + }, { |
| 136 | + Authorization |
| 137 | + }); |
| 138 | + if (res.code) { |
| 139 | + throw (res.message); |
| 140 | + } |
| 141 | + uploadId = res.data.uploadId; |
| 142 | + |
| 143 | + // 上传过一部分 |
| 144 | + if (Array.isArray(res.data.parts)) { |
| 145 | + await upload(filePath, res.data.parts); |
| 146 | + } else { |
| 147 | + // 未上传过 |
| 148 | + await upload(filePath); |
| 149 | + } |
| 150 | + } catch (error) { |
| 151 | + logger.error(error.message); |
| 152 | + logger.error(error.stack); |
| 153 | + console.log(chalk.red((error.response && error.response.data) || error.message)); |
| 154 | + return; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +const getFileMD5 = async (filePath) => { |
| 159 | + const totalChunk = Math.ceil(fileSize / CHUNK_SIZE); |
| 160 | + const spark = new SparkMD5.ArrayBuffer(); |
| 161 | + try { |
| 162 | + console.log(`\n开始计算 MD5\n`) |
| 163 | + logger.info('开始计算 MD5') |
| 164 | + |
| 165 | + const bar = new ProgressBar(':bar [:current/:total] :percent', { total: totalChunk }); |
| 166 | + await new Promise(resolve => { |
| 167 | + stream = fs.createReadStream(filePath, { highWaterMark: CHUNK_SIZE }) |
| 168 | + stream.on('data', chunk => { |
| 169 | + bar.tick(); |
| 170 | + spark.append(chunk) |
| 171 | + }) |
| 172 | + stream.on('error', error => { |
| 173 | + reject('读取文件分片异常,请重新执行命令继续上传'); |
| 174 | + }) |
| 175 | + stream.on('end', async () => { |
| 176 | + md5 = spark.end(); |
| 177 | + spark.destroy(); |
| 178 | + console.log(`\n文件 MD5:${md5}\n`) |
| 179 | + await getFileMD5Success(filePath); |
| 180 | + resolve(); |
| 181 | + }) |
| 182 | + }).catch(error => { |
| 183 | + throw Error(error); |
| 184 | + }) |
| 185 | + } catch (error) { |
| 186 | + console.log(chalk.red((error.response && error.response.data) || error.message)); |
| 187 | + logger.error(error.message); |
| 188 | + logger.error(error.stack); |
| 189 | + return; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +const beforeUpload = async (filePath) => { |
| 194 | + try { |
| 195 | + const stat = fs.lstatSync(filePath); |
| 196 | + if (stat.isDirectory()) { |
| 197 | + console.log(chalk.red(`\n${filePath}不合法,需指定一个文件\n`)) |
| 198 | + return ; |
| 199 | + } |
| 200 | + fileSize = stat.size; |
| 201 | + } catch (error) { |
| 202 | + if (error.code === 'ENOENT') { |
| 203 | + console.log(chalk.red(`未找到 ${filePath}`)); |
| 204 | + } else { |
| 205 | + logger.error(error.message); |
| 206 | + logger.error(error.stack); |
| 207 | + console.log(chalk.red((error.response && error.response.data) || error.message)); |
| 208 | + } |
| 209 | + process.exitCode = 1; |
| 210 | + return; |
| 211 | + } |
| 212 | + await getFileMD5(filePath); |
| 213 | +} |
| 214 | + |
| 215 | +const onUpload = (_username, _password) => { |
| 216 | + Authorization = generateAuthorization(_username, _password); |
| 217 | + |
| 218 | + logger.info('************************ 准备上传 ************************') |
| 219 | + |
| 220 | + if (path.isAbsolute(argv.path)) { |
| 221 | + beforeUpload(argv.path); |
| 222 | + } else { |
| 223 | + beforeUpload(path.join(process.cwd(), argv.path)) |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +const [username, password] = argv.username.split(':'); |
| 228 | + |
| 229 | +if (username && password) { |
| 230 | + onUpload(username, password); |
| 231 | +} else { |
| 232 | + prompts([ |
| 233 | + { |
| 234 | + type: 'password', |
| 235 | + name: 'password', |
| 236 | + message: '请输入登陆密码:', |
| 237 | + } |
| 238 | + ], { |
| 239 | + onCancel: () => { } |
| 240 | + } |
| 241 | + ).then(async (answers) => { |
| 242 | + if (!answers.password) { |
| 243 | + return; |
| 244 | + } |
| 245 | + onUpload(argv.username, answers.password); |
| 246 | + }) |
| 247 | +} |
0 commit comments