Skip to content

Commit b18656e

Browse files
committed
add local storage
1 parent 88208b5 commit b18656e

4 files changed

Lines changed: 66 additions & 34 deletions

File tree

‎config/config.json‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
"bucketName": "",
1414
"downloadUrl": ""
1515
},
16+
"local": {
17+
"storageDir": "",
18+
"downloadUrl": "http://localhost:3000"
19+
},
1620
"common": {
1721
"loginSecret": "CodePushServer",
1822
"diffNums": 3,
19-
"dataDir": ""
23+
"dataDir": "",
24+
"storageType": "qiniu"
2025
}
2126
},
2227
"production": {
@@ -33,10 +38,15 @@
3338
"bucketName": "",
3439
"downloadUrl": ""
3540
},
41+
"local": {
42+
"storageDir": "",
43+
"downloadUrl": "http://localhost:3000"
44+
},
3645
"common": {
3746
"loginSecret": "CodePushServer",
3847
"diffNums": 3,
39-
"dataDir": ""
48+
"dataDir": "",
49+
"storageType": "qiniu"
4050
}
4151
}
4252
}

‎core/services/client-manager.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var Promise = require('bluebird');
33
var models = require('../../models');
44
var _ = require('lodash');
5-
var config = _.get(require('../config'), 'qiniu', {});
5+
var common = require('../utils/common');
66

77
var proto = module.exports = function (){
88
function ClientManager() {
@@ -42,9 +42,10 @@ proto.updateCheck = function(deploymentKey, appVersion, label, packageHash) {
4242
}
4343
return models.Packages.findById(packageId)
4444
.then(function (packages) {
45+
var downloadURL = common.getDownloadUrl();
4546
if (!_.isEmpty(packages) && !_.eq(_.get(packages, 'package_hash', ""), packageHash)) {
4647
return models.PackagesDiff.findOne({where: {package_id:packages.id, diff_against_package_hash: packageHash}}).then(function (diffPackage) {
47-
rs.downloadURL = _.get(config, 'downloadUrl') + '/' + _.get(packages,'blob_url');
48+
rs.downloadURL = `${downloadURL}/${_.get(packages, 'blob_url')}`;
4849
rs.description = _.get(packages, 'description', '');
4950
rs.isAvailable = true;
5051
rs.isMandatory = _.eq(deploymentsVersions.is_mandatory, 1) ? true : false;
@@ -54,7 +55,7 @@ proto.updateCheck = function(deploymentKey, appVersion, label, packageHash) {
5455
rs.packageSize = _.get(packages, 'size', 0);
5556
rs.shouldRunBinaryVersion = false;
5657
if (!_.isEmpty(diffPackage)) {
57-
rs.downloadURL = _.get(config, 'downloadUrl') + '/' + _.get(diffPackage, 'diff_blob_url');
58+
rs.downloadURL = `${downloadURL}/${_.get(diffPackage, 'diff_blob_url')}`;
5859
rs.packageSize = _.get(diffPackage, 'diff_size', 0);
5960
}
6061
return;

‎core/services/package-manager.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ proto.generateOneDiffPackage = function (workDirectoryPath, packageId, dataCente
200200
.then(function (data) {
201201
return security.qetag(data.path)
202202
.then(function (diffHash) {
203-
return common.uploadFileToQiniu(diffHash, fileName)
203+
return common.uploadFileToStorage(diffHash, fileName)
204204
.then(function () {
205205
var stats = fs.statSync(fileName);
206206
return models.PackagesDiff.create({
@@ -296,10 +296,11 @@ proto.releasePackage = function (deploymentId, packageInfo, fileType, filePath,
296296
}
297297
})
298298
.then(function () {
299-
return security.qetag(manifestFile).then(function (manifestHash) {
299+
return security.qetag(manifestFile)
300+
.then(function (manifestHash) {
300301
return Promise.all([
301-
common.uploadFileToQiniu(manifestHash, manifestFile),
302-
common.uploadFileToQiniu(blobHash, filePath)
302+
common.uploadFileToStorage(manifestHash, manifestFile),
303+
common.uploadFileToStorage(blobHash, filePath)
303304
])
304305
.spread(function (up1, up2) {
305306
return [packageHash, manifestHash, blobHash];

‎core/utils/common.js‎

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ var Promise = require('bluebird');
33
var fs = require("fs");
44
var fsextra = require("fs.extra");
55
var unzip = require('node-unzip-2');
6-
var qiniu = require("qiniu");
6+
var config = require('../config');
77
var _ = require('lodash');
8-
var config = _.get(require('../config'), 'qiniu', {});
8+
var qiniu = require("qiniu");
99
var common = {};
1010
module.exports = common;
1111

@@ -110,35 +110,55 @@ common.unzipFile = function (zipFile, outputPath) {
110110
common.uptoken = function (bucket, key) {
111111
var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key);
112112
return putPolicy.token();
113+
};
114+
115+
common.uploadFileToStorage = function (key, filePath) {
116+
if (_.get(config, 'common.storageType') == 'local') {
117+
return common.uploadFileToLocal(key, filePath);
118+
}
119+
return common.uploadFileToQiniu(key, filePath);
120+
};
121+
122+
common.uploadFileToLocal = function (key, filePath) {
123+
return new Promise(function (resolve, reject, notify) {
124+
resolve(filePath);
125+
});
126+
};
127+
128+
common.getDownloadUrl = function () {
129+
if (_.get(config, 'common.storageType') == 'local') {
130+
return _.get(config, 'local.downloadUrl');
131+
}
132+
return _.get(config, 'qiniu.downloadUrl');
113133
}
114134

115135
common.uploadFileToQiniu = function (key, filePath) {
116136
return new Promise(function (resolve, reject, notify) {
117-
try {
118-
qiniu.conf.ACCESS_KEY = _.get(config, "accessKey");
119-
qiniu.conf.SECRET_KEY = _.get(config, "secretKey");
120-
var bucket = _.get(config, "bucketName", "jukang");
121-
var client = new qiniu.rs.Client();
122-
client.stat(bucket, key, function(err, ret) {
123-
if (!err) {
124-
resolve(ret.hash);
125-
} else {
137+
qiniu.conf.ACCESS_KEY = _.get(config, "qiniu.accessKey");
138+
qiniu.conf.SECRET_KEY = _.get(config, "qiniu.secretKey");
139+
var bucket = _.get(config, "qiniu.bucketName", "jukang");
140+
var client = new qiniu.rs.Client();
141+
client.stat(bucket, key, function(err, ret) {
142+
if (!err) {
143+
resolve(ret.hash);
144+
} else {
145+
try {
126146
var uptoken = common.uptoken(bucket, key);
127-
var extra = new qiniu.io.PutExtra();
128-
qiniu.io.putFile(uptoken, key, filePath, extra, function(err, ret) {
129-
if(!err) {
130-
// 上传成功, 处理返回值
131-
resolve(ret.hash);
132-
} else {
133-
// 上传失败, 处理返回代码
134-
reject({message: JSON.stringify(err)});
135-
}
136-
});
147+
} catch (e) {
148+
reject({message: e.message});
137149
}
138-
});
139-
} catch(e) {
140-
reject(e)
141-
}
150+
var extra = new qiniu.io.PutExtra();
151+
qiniu.io.putFile(uptoken, key, filePath, extra, function(err, ret) {
152+
if(!err) {
153+
// 上传成功, 处理返回值
154+
resolve(ret.hash);
155+
} else {
156+
// 上传失败, 处理返回代码
157+
reject({message: JSON.stringify(err)});
158+
}
159+
});
160+
}
161+
});
142162
});
143163
};
144164

0 commit comments

Comments
 (0)