This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (93 loc) · 3.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
// USSAdapter
// Store Parse Files in UPYUN Storage Service: https://www.upyun.com/products/file-storage
const upyun = require('upyun');
function requiredOrFromEnvironment(options, key, env) {
options[key] = options[key] || process.env[env];
if (!options[key]) {
throw `USSAdapter requires an ${key}`;
}
return options;
}
function fromEnvironmentOrDefault(options, key, env, defaultValue) {
options[key] = options[key] || process.env[env] || defaultValue;
return options;
}
function optionsFromArguments(args) {
let options = {};
let serviceNameOrOptions = args[0];
if (typeof serviceNameOrOptions == 'string') {
options.serviceName = serviceNameOrOptions;
options.operatorName = args[1];
options.operatorPassword = args[2];
let otherOptions = args[3];
if (otherOptions) {
options.bucketPrefix = otherOptions.bucketPrefix;
options.directAccess = otherOptions.directAccess;
options.baseUrl = otherOptions.baseUrl;
}
} else {
options = Object.assign({}, serviceNameOrOptions);
}
options = requiredOrFromEnvironment(options, 'serviceName', 'USS_SERVICE_NAME');
options = requiredOrFromEnvironment(options, 'operatorName', 'USS_OPERATOR_NAME');
options = requiredOrFromEnvironment(options, 'operatorPassword', 'USS_OPERATOR_PASSWORD');
options = fromEnvironmentOrDefault(options, 'bucketPrefix', 'USS_BUCKET_PREFIX', '');
options = fromEnvironmentOrDefault(options, 'directAccess', 'USS_DIRECT_ACCESS', false);
if (options.directAccess) {
options = requiredOrFromEnvironment(options, 'baseUrl', 'USS_BASE_URL');
}
return options;
}
/*
supported options
*serviceName / 'USS_SERVICE_NAME'
*operatorName / 'USS_OPERATOR_NAME'
*operatorPassword / 'USS_OPERATOR_PASSWORD'
{ bucketPrefix / 'USS_BUCKET_PREFIX' defaults to ''
directAccess / 'USS_DIRECT_ACCESS' defaults to false
*baseUrl / 'USS_BASE_URL'
*/
function USSAdapter() {
let options = optionsFromArguments(arguments);
this._baseUrl = options.baseUrl;
this._bucketPrefix = options.bucketPrefix;
this._directAccess = options.directAccess;
let service = new upyun.Service(options.serviceName, options.operatorName, options.operatorPassword);
this._ussClient = new upyun.Client(service);
}
USSAdapter.prototype.createFile = function (filename, data, contentType) {
return new Promise((resolve, reject) => {
this._ussClient.putFile(this._bucketPrefix + filename, data).then(function () {
resolve();
}, function () {
reject();
});
});
}
USSAdapter.prototype.deleteFile = function (filename) {
return new Promise((resolve, reject) => {
this._ussClient.deleteFile(this._bucketPrefix + filename).then(function () {
resolve();
}, function () {
reject();
});
});
}
USSAdapter.prototype.getFileData = function (filename) {
return new Promise((resolve, reject) => {
this._ussClient.getFile(this._bucketPrefix + filename).then(function (result) {
resolve(result);
}, function () {
reject();
});
});
}
USSAdapter.prototype.getFileLocation = function (config, filename) {
if (this._directAccess) {
return `${this._baseUrl}/${this._bucketPrefix + filename}`;
}
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
}
module.exports = USSAdapter;
module.exports.default = USSAdapter;