123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- "use strict";
- const qiniuUploader = function() {
- var config = {
- qiniuRegion: "",
- qiniuImageURLPrefix: "",
- qiniuUploadToken: "",
- qiniuUploadTokenURL: "",
- qiniuUploadTokenFunction: null,
- qiniuShouldUseQiniuFileName: false
- };
- function init(options) {
- config = {
- qiniuRegion: "",
- qiniuImageURLPrefix: "",
- qiniuUploadToken: "",
- qiniuUploadTokenURL: "",
- qiniuUploadTokenFunction: null,
- qiniuShouldUseQiniuFileName: false
- };
- updateConfigWithOptions(options);
- }
- function updateConfigWithOptions(options) {
- if (options.region) {
- config.qiniuRegion = options.region;
- } else {
- console.error("qiniu uploader need your bucket region");
- }
- if (options.uptoken) {
- config.qiniuUploadToken = options.uptoken;
- } else if (options.uptokenURL) {
- config.qiniuUploadTokenURL = options.uptokenURL;
- } else if (options.uptokenFunc) {
- config.qiniuUploadTokenFunction = options.uptokenFunc;
- }
- if (options.domain) {
- config.qiniuImageURLPrefix = options.domain;
- }
- config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName;
- }
- function upload(filePath, success, fail, options, progress, cancelTask) {
- if (filePath == null) {
- console.error("qiniu uploader need filePath to upload");
- return;
- }
- if (options) {
- updateConfigWithOptions(options);
- }
- if (config.qiniuUploadToken) {
- doUpload(filePath, success, fail, options, progress, cancelTask);
- } else if (config.qiniuUploadTokenURL) {
- getQiniuToken(function() {
- doUpload(filePath, success, fail, options, progress, cancelTask);
- });
- } else if (config.qiniuUploadTokenFunction) {
- config.qiniuUploadToken = config.qiniuUploadTokenFunction();
- if (config.qiniuUploadToken == null && config.qiniuUploadToken.length > 0) {
- console.error("qiniu UploadTokenFunction result is null, please check the return value");
- return;
- }
- doUpload(filePath, success, fail, options, progress, cancelTask);
- } else {
- console.error("qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]");
- return;
- }
- }
- function doUpload(filePath, success, fail, options, progress, cancelTask) {
- if (config.qiniuUploadToken == null && config.qiniuUploadToken.length > 0) {
- console.error("qiniu UploadToken is null, please check the init config or networking");
- return;
- }
- var url = uploadURLFromRegionCode(config.qiniuRegion);
- var fileName = filePath.split("//")[1];
- if (options && options.key) {
- fileName = options.key;
- }
- var formData = {
- "token": config.qiniuUploadToken
- };
- if (!config.qiniuShouldUseQiniuFileName) {
- formData["key"] = fileName;
- }
- var uploadTask = wx.uploadFile({
- url,
- filePath,
- name: "file",
- formData,
- success: function(res) {
- var dataString = res.data;
- if (res.data.hasOwnProperty("type") && res.data.type === "Buffer") {
- dataString = String.fromCharCode.apply(null, res.data.data);
- }
- try {
- var dataObject = JSON.parse(dataString);
- var imageUrl = config.qiniuImageURLPrefix + "/" + dataObject.key;
- dataObject.imageURL = imageUrl;
- if (success) {
- success(dataObject);
- }
- } catch (e) {
- console.log("parse JSON failed, origin String is: " + dataString);
- if (fail) {
- fail(e);
- }
- }
- },
- fail: function(error) {
- console.error(error);
- if (fail) {
- fail(error);
- }
- }
- });
- uploadTask.onProgressUpdate((res) => {
- progress && progress(res);
- });
- cancelTask && cancelTask(() => {
- uploadTask.abort();
- });
- }
- function getQiniuToken(callback) {
- wx.request({
- url: config.qiniuUploadTokenURL,
- success: function(res) {
- var token = res.data.uptoken;
- if (token && token.length > 0) {
- config.qiniuUploadToken = token;
- if (callback) {
- callback();
- }
- } else {
- console.error("qiniuUploader cannot get your token, please check the uptokenURL or server");
- }
- },
- fail: function(error) {
- console.error("qiniu UploadToken is null, please check the init config or networking: " + error);
- }
- });
- }
- function uploadURLFromRegionCode(code) {
- var uploadURL = null;
- switch (code) {
- case "ECN":
- uploadURL = "https://up.qbox.me";
- break;
- case "NCN":
- uploadURL = "https://up-z1.qbox.me";
- break;
- case "SCN":
- uploadURL = "https://up-z2.qbox.me";
- break;
- case "NA":
- uploadURL = "https://up-na0.qbox.me";
- break;
- case "ASG":
- uploadURL = "https://up-as0.qbox.me";
- break;
- default:
- console.error("please make the region is with one of [ECN, SCN, NCN, NA, ASG]");
- }
- return uploadURL;
- }
- return {
- init,
- upload
- };
- }();
- exports.qiniuUploader = qiniuUploader;
|