forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmazonS3.js
100 lines (86 loc) · 2.81 KB
/
AmazonS3.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
/* globals FileUpload */
import _ from 'underscore';
import { FileUploadClass } from '../lib/FileUpload';
import '../../ufs/AmazonS3/server.js';
import http from 'http';
import https from 'https';
const get = function(file, req, res) {
const fileUrl = this.store.getRedirectURL(file);
if (fileUrl) {
const storeType = file.store.split(':').pop();
if (RocketChat.settings.get(`FileUpload_S3_Proxy_${ storeType }`)) {
const request = /^https:/.test(fileUrl) ? https : http;
request.get(fileUrl, fileRes => fileRes.pipe(res));
} else {
res.removeHeader('Content-Length');
res.setHeader('Location', fileUrl);
res.writeHead(302);
res.end();
}
} else {
res.end();
}
};
const copy = function(file, out) {
const fileUrl = this.store.getRedirectURL(file);
if (fileUrl) {
const request = /^https:/.test(fileUrl) ? https : http;
request.get(fileUrl, fileRes => fileRes.pipe(out));
} else {
out.end();
}
};
const AmazonS3Uploads = new FileUploadClass({
name: 'AmazonS3:Uploads',
get,
copy
// store setted bellow
});
const AmazonS3Avatars = new FileUploadClass({
name: 'AmazonS3:Avatars',
get,
copy
// store setted bellow
});
const AmazonS3UserDataFiles = new FileUploadClass({
name: 'AmazonS3:UserDataFiles',
get,
copy
// store setted bellow
});
const configure = _.debounce(function() {
const Bucket = RocketChat.settings.get('FileUpload_S3_Bucket');
const Acl = RocketChat.settings.get('FileUpload_S3_Acl');
const AWSAccessKeyId = RocketChat.settings.get('FileUpload_S3_AWSAccessKeyId');
const AWSSecretAccessKey = RocketChat.settings.get('FileUpload_S3_AWSSecretAccessKey');
const URLExpiryTimeSpan = RocketChat.settings.get('FileUpload_S3_URLExpiryTimeSpan');
const Region = RocketChat.settings.get('FileUpload_S3_Region');
const SignatureVersion = RocketChat.settings.get('FileUpload_S3_SignatureVersion');
const ForcePathStyle = RocketChat.settings.get('FileUpload_S3_ForcePathStyle');
// const CDN = RocketChat.settings.get('FileUpload_S3_CDN');
const BucketURL = RocketChat.settings.get('FileUpload_S3_BucketURL');
if (!Bucket || !AWSAccessKeyId || !AWSSecretAccessKey) {
return;
}
const config = {
connection: {
accessKeyId: AWSAccessKeyId,
secretAccessKey: AWSSecretAccessKey,
signatureVersion: SignatureVersion,
s3ForcePathStyle: ForcePathStyle,
params: {
Bucket,
ACL: Acl
},
region: Region
},
URLExpiryTimeSpan
};
if (BucketURL) {
config.connection.endpoint = BucketURL;
}
AmazonS3Uploads.store = FileUpload.configureUploadsStore('AmazonS3', AmazonS3Uploads.name, config);
AmazonS3Avatars.store = FileUpload.configureUploadsStore('AmazonS3', AmazonS3Avatars.name, config);
AmazonS3UserDataFiles.store = FileUpload.configureUploadsStore('AmazonS3', AmazonS3UserDataFiles.name, config);
}, 500);
RocketChat.settings.get(/^FileUpload_S3_/, configure);