zhaojs
2023-06-29 28534913afd81c7d4636badb5f5a518f471b02a3
public/assets/js/addons.js
@@ -1,5 +1,5 @@
define([], function () {
    if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'alioss') {
    if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'hwobs') {
    require(['upload'], function (Upload) {
        //获取文件MD5值
        var getFileMd5 = function (file, cb) {
@@ -8,7 +8,7 @@
                cb && cb(file.upload.uuid);
                return;
            }
            require(['../addons/alioss/js/spark'], function (SparkMD5) {
            require(['../addons/hwobs/js/spark'], function (SparkMD5) {
                var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
                    chunkSize = 10 * 1024 * 1024,
                    chunks = Math.ceil(file.size / chunkSize),
@@ -38,7 +38,6 @@
                }
                loadNext();
            });
        };
@@ -46,7 +45,7 @@
        //初始化中完成判断
        Upload.events.onInit = function () {
            _onInit.apply(this, Array.prototype.slice.apply(arguments));
            //如果上传接口不是阿里OSS,则不处理
            //如果上传接口不是hwobs,则不处理
            if (this.options.url !== Config.upload.uploadurl) {
                return;
            }
@@ -54,7 +53,7 @@
                //关闭自动处理队列功能
                autoQueue: false,
                params: function (files, xhr, chunk) {
                    var params = Config.upload.multipart;
                    var params = $.extend({}, Config.upload.multipart);
                    if (chunk) {
                        return $.extend({}, params, {
                            filesize: chunk.file.size,
@@ -62,29 +61,27 @@
                            chunkid: chunk.file.upload.uuid,
                            chunkindex: chunk.index,
                            chunkcount: chunk.file.upload.totalChunkCount,
                            chunksize: this.options.chunkSize,
                            chunkfilesize: chunk.dataBlock.data.size,
                            chunksize: this.options.chunkSize,
                            width: chunk.file.width || 0,
                            height: chunk.file.height || 0,
                            type: chunk.file.type,
                            uploadId: chunk.file.uploadId,
                            key: chunk.file.key,
                        });
                    } else {
                        params = $.extend({}, params, files[0].params);
                        params.category = files[0].category || '';
                    }
                    return params;
                },
                chunkSuccess: function (chunk, file, response) {
                    var etag = chunk.xhr.getResponseHeader("ETag").replace(/(^")|("$)/g, '');
                    file.etags = file.etags ? file.etags : [];
                    file.etags[chunk.index] = etag;
                    this.etags = this.etags ? this.etags : [];
                    this.etags[chunk.index] = etag;
                },
                chunksUploaded: function (file, done) {
                    var that = this;
                    Fast.api.ajax({
                        url: "/addons/alioss/index/upload",
                        url: "/addons/hwobs/index/upload",
                        data: {
                            action: 'merge',
                            filesize: file.size,
@@ -94,9 +91,9 @@
                            md5: file.md5,
                            key: file.key,
                            uploadId: file.uploadId,
                            etags: file.etags,
                            etags: this.etags,
                            category: file.category || '',
                            aliosstoken: Config.upload.multipart.aliosstoken,
                            hwobstoken: Config.upload.multipart.hwobstoken,
                        },
                    }, function (data, ret) {
                        done(JSON.stringify(ret));
@@ -118,16 +115,17 @@
                    if (response) {
                        ret = typeof response === 'string' ? JSON.parse(response) : response;
                    }
                    if (file.xhr.status === 200) {
                    if (file.xhr.status === 200 || file.xhr.status === 204) {
                        if (Config.upload.uploadmode === 'client') {
                            ret = {code: 1, data: {url: '/' + file.key}};
                        }
                        if (ret.code == 1) {
                            var url = ret.data.url || '';
                            Fast.api.ajax({
                                url: "/addons/alioss/index/notify",
                                data: {name: file.name, url: url, md5: file.md5, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: file.category || '', aliosstoken: Config.upload.multipart.aliosstoken}
                                url: "/addons/hwobs/index/notify",
                                data: {name: file.name, url: url, md5: file.md5, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: file.category || '', hwobstoken: Config.upload.multipart.hwobstoken}
                            }, function () {
                                return false;
                            }, function () {
@@ -157,8 +155,8 @@
                        var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
                        category = typeof category === 'function' ? category.call(that, file) : category;
                        Fast.api.ajax({
                            url: "/addons/alioss/index/params",
                            data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, aliosstoken: Config.upload.multipart.aliosstoken},
                            url: "/addons/hwobs/index/params",
                            data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, hwobstoken: Config.upload.multipart.hwobstoken},
                        }, function (data) {
                            file.md5 = md5;
                            file.id = data.id;
@@ -168,6 +166,8 @@
                            file.policy = data.policy;
                            file.signature = data.signature;
                            file.partsAuthorization = data.partsAuthorization;
                            file.headers = data.headers;
                            delete data.headers;
                            file.params = data;
                            file.category = category;
@@ -201,6 +201,8 @@
                        } else {
                            return "PUT";
                        }
                    } else {
                        return "POST";
                    }
                    return _method;
                };
@@ -213,8 +215,7 @@
                            }
                        });
                        var index = chunk.dataBlock.chunkIndex;
                        // debugger;
                        this.options.headers = {"Authorization": "OSS " + files[0]['id'] + ":" + files[0]['partsAuthorization'][index], "x-oss-date": files[0]['date']};
                        this.options.headers = {"Authorization": files[0]['partsAuthorization'][index], "x-amz-date": files[0]['date']};
                        if (!chunk) {
                            return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
                        } else {
@@ -223,18 +224,51 @@
                    }
                    return _url;
                };
                this.options.params = function (files, xhr, chunk) {
                    var params = Config.upload.multipart;
                    delete params.category;
                    if (chunk) {
                        return $.extend({}, params, {
                            filesize: chunk.file.size,
                            filename: chunk.file.name,
                            chunkid: chunk.file.upload.uuid,
                            chunkindex: chunk.index,
                            chunkcount: chunk.file.upload.totalChunkCount,
                            chunkfilesize: chunk.dataBlock.data.size,
                            width: chunk.file.width || 0,
                            height: chunk.file.height || 0,
                            type: chunk.file.type,
                        });
                    } else {
                        var retParams = $.extend({}, params, files[0].params || {});
                        delete retParams.hwobstoken;
                        delete retParams.date;
                        delete retParams.md5;
                        if (Config.upload.uploadmode !== 'client') {
                            params.category = files[0].category || '';
                        }
                        return retParams;
                    }
                };
                this.on("sending", function (file, xhr, formData) {
                    var that = this;
                    var _send = xhr.send;
                    //仅允许部分字段
                    var allowFields = ['partNumber', 'uploadId', 'key', 'AccessKeyId', 'policy', 'signature', 'file'];
                    formData.forEach(function (value, key) {
                        if (allowFields.indexOf(key) < 0) {
                            formData.delete(key);
                        }
                    });
                    if (file.upload.chunked) {
                        var _send = xhr.send;
                        xhr.send = function () {
                            var chunk = null;
                            file.upload.chunks.forEach(function (item) {
                                if (item.status == 'uploading') {
                                    chunk = item;
                                }
                            });
                            if (chunk) {
                            if (file.upload.chunked) {
                                var chunk = null;
                                file.upload.chunks.forEach(function (item) {
                                    if (item.status == 'uploading') {
                                        chunk = item;
                                    }
                                });
                                _send.call(xhr, chunk.dataBlock.data);
                            }
                        };