修改问题
This commit is contained in:
+119
-1
@@ -15,6 +15,7 @@
|
||||
'use strict';
|
||||
|
||||
var documentRef = root.document;
|
||||
var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
||||
|
||||
function getApi() {
|
||||
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
@@ -78,6 +79,108 @@
|
||||
return (fileName || '文件') + ' 上传完成,OSS ID:' + ossId;
|
||||
}
|
||||
|
||||
function getUploadMode(fileSize, chunkSize) {
|
||||
var size = Number(fileSize) || 0;
|
||||
var limit = Number(chunkSize) || DEFAULT_CHUNK_SIZE;
|
||||
|
||||
return size > limit ? 'resumable' : 'single';
|
||||
}
|
||||
|
||||
function buildResumableInitBody(file, fileMd5, chunkSize, options) {
|
||||
var source = file || {};
|
||||
var settings = options || {};
|
||||
var size = Number(source.size) || 0;
|
||||
var sizePerChunk = Number(chunkSize) || DEFAULT_CHUNK_SIZE;
|
||||
var body = {
|
||||
fileName: String(source.name || ''),
|
||||
fileSize: size,
|
||||
fileMd5: String(fileMd5 || ''),
|
||||
contentType: String(source.type || 'application/octet-stream'),
|
||||
chunkSize: sizePerChunk,
|
||||
totalChunks: Math.ceil(size / sizePerChunk)
|
||||
};
|
||||
|
||||
if (settings.bizType) body.bizType = settings.bizType;
|
||||
if (settings.usageScene) body.usageScene = settings.usageScene;
|
||||
return body;
|
||||
}
|
||||
|
||||
function buildResumableCompleteBody(uploadId, fileMd5, fileSize) {
|
||||
return {
|
||||
uploadId: String(uploadId || ''),
|
||||
fileMd5: String(fileMd5 || ''),
|
||||
fileSize: Number(fileSize) || 0
|
||||
};
|
||||
}
|
||||
|
||||
function toBinaryString(buffer) {
|
||||
var bytes = new Uint8Array(buffer);
|
||||
var chunkSize = 0x8000;
|
||||
var chunks = [];
|
||||
var index;
|
||||
|
||||
for (index = 0; index < bytes.length; index += chunkSize) {
|
||||
chunks.push(String.fromCharCode.apply(null, bytes.subarray(index, index + chunkSize)));
|
||||
}
|
||||
|
||||
return chunks.join('');
|
||||
}
|
||||
|
||||
async function hashBlob(blob) {
|
||||
if (!root.hex_md5 || !blob || !blob.arrayBuffer) {
|
||||
throw new Error('缺少文件 MD5 工具');
|
||||
}
|
||||
|
||||
return root.hex_md5(toBinaryString(await blob.arrayBuffer()));
|
||||
}
|
||||
|
||||
async function uploadResumable(api, file, options) {
|
||||
var settings = options || {};
|
||||
var chunkSize = Number(settings.chunkSize) || DEFAULT_CHUNK_SIZE;
|
||||
var hash = settings.hashBlob || hashBlob;
|
||||
var fileMd5;
|
||||
var init;
|
||||
var uploadId;
|
||||
var totalChunks;
|
||||
var index;
|
||||
var start;
|
||||
var end;
|
||||
var chunk;
|
||||
|
||||
if (!api || !file || !file.slice) throw new Error('请选择可分片上传的文件');
|
||||
|
||||
fileMd5 = await hash(file);
|
||||
init = await api.initResumableUpload(buildResumableInitBody(file, fileMd5, chunkSize, settings));
|
||||
uploadId = pick(init, ['uploadId', 'id'], '');
|
||||
if (!uploadId) throw new Error('分片上传初始化缺少 uploadId');
|
||||
|
||||
totalChunks = Math.ceil(file.size / chunkSize);
|
||||
for (index = 0; index < totalChunks; index += 1) {
|
||||
start = index * chunkSize;
|
||||
end = Math.min(start + chunkSize, file.size);
|
||||
chunk = file.slice(start, end);
|
||||
await api.uploadChunk({
|
||||
uploadId: uploadId,
|
||||
chunkIndex: index,
|
||||
chunkMd5: await hash(chunk),
|
||||
file: chunk
|
||||
});
|
||||
if (settings.onProgress) settings.onProgress(index + 1, totalChunks);
|
||||
}
|
||||
|
||||
return api.completeResumableUpload(buildResumableCompleteBody(uploadId, fileMd5, file.size));
|
||||
}
|
||||
|
||||
async function uploadFileForPage(api, file, options) {
|
||||
var settings = options || {};
|
||||
var mode = settings.mode === 'single' || settings.mode === 'resumable'
|
||||
? settings.mode
|
||||
: getUploadMode(file && file.size, settings.chunkSize);
|
||||
|
||||
if (mode === 'single') return api.uploadFile(file);
|
||||
return uploadResumable(api, file, settings);
|
||||
}
|
||||
|
||||
async function uploadFromInput(input) {
|
||||
var api = getApi();
|
||||
var file = input.files && input.files[0];
|
||||
@@ -86,6 +189,8 @@
|
||||
var target = targetSelector && query(targetSelector);
|
||||
var status = statusSelector && query(statusSelector);
|
||||
var multiple = input.getAttribute('data-upload-multiple') === 'true';
|
||||
var chunkSize = Number(input.getAttribute('data-upload-chunk-size')) || DEFAULT_CHUNK_SIZE;
|
||||
var mode = input.getAttribute('data-upload-mode') || '';
|
||||
var result;
|
||||
|
||||
if (!api || !file || !target) return;
|
||||
@@ -94,7 +199,15 @@
|
||||
input.disabled = true;
|
||||
|
||||
try {
|
||||
result = normalizeUploadResult(await api.uploadFile(file));
|
||||
result = normalizeUploadResult(await uploadFileForPage(api, file, {
|
||||
mode: mode || getUploadMode(file.size, chunkSize),
|
||||
chunkSize: chunkSize,
|
||||
bizType: input.getAttribute('data-upload-biz-type') || undefined,
|
||||
usageScene: input.getAttribute('data-upload-usage-scene') || undefined,
|
||||
onProgress: function (current, total) {
|
||||
if (status) status.textContent = '分片上传 ' + current + '/' + total;
|
||||
}
|
||||
}));
|
||||
if (!result.ossId) throw new Error('上传结果缺少 OSS ID');
|
||||
target.value = mergeOssIds(target.value, result.ossId, multiple);
|
||||
if (status) status.textContent = buildUploadStatus(result.fileName || file.name, result.ossId);
|
||||
@@ -127,6 +240,11 @@
|
||||
normalizeUploadResult: normalizeUploadResult,
|
||||
mergeOssIds: mergeOssIds,
|
||||
buildUploadStatus: buildUploadStatus,
|
||||
getUploadMode: getUploadMode,
|
||||
buildResumableInitBody: buildResumableInitBody,
|
||||
buildResumableCompleteBody: buildResumableCompleteBody,
|
||||
uploadResumable: uploadResumable,
|
||||
uploadFileForPage: uploadFileForPage,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user