This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
/
s3-bucket.js
127 lines (101 loc) · 3.65 KB
/
s3-bucket.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import $ from 'jquery';
import { A } from '@ember/array';
import EmberObject, { computed, observer } from '@ember/object';
import S3File from './s3-file';
export default EmberObject.extend({
isLoading: false,
// Setup these as default values.
// They can be overridden on create.
queryUseSSL: true,
objectUseSSL: false,
delimiter: '/',
bucket: 'builds.emberjs.com',
endpoint: 's3.amazonaws.com',
delimiterParameter: computed('delimiter', function(){
let delimiter = this.getWithDefault('delimiter','').toString();
return (delimiter) ? 'delimiter=' + delimiter : '';
}),
maxKeysParameter: computed('maxKeys', function(){
return 'max-keys=' + this.getWithDefault('maxKeys','').toString();
}),
prefixParameter: computed('prefix', function(){
return 'prefix=' + this.getWithDefault('prefix','').toString();
}),
queryProtocol: computed('queryUseSSL', function() {
return this.get('queryUseSSL') ? 'https://' : 'http://';
}),
queryBaseUrl: computed('queryProtocol', 'endpoint', 'bucket', function(){
return this.get('queryProtocol') + this.get('endpoint') + '/' + this.get('bucket');
}),
objectProtocol: computed('objectUseSSL', function() {
return this.get('objectUseSSL') ? 'https://' : 'http://';
}),
objectBaseUrl: computed('objectProtocol', 'bucket', function(){
return this.get('objectProtocol') + this.get('bucket');
}),
queryParams: computed('delimiterParameter', 'maxKeysParameter', 'prefixParameter', function(){
return this.get('delimiterParameter') + '&' +
this.get('maxKeysParameter') + '&' +
this.get('prefixParameter');
}),
queryUrl: computed('queryBaseUrl', 'queryParams', function(){
return this.get('queryBaseUrl') + '?' + this.get('queryParams');
}),
filesPresent: computed('files.[]', function(){
return this.get('files').length;
}),
init() {
this._super(...arguments);
this.files = [];
this.load();
},
filterFiles(filter, ignoreFiles){
let files = this.get('files');
return files.filter(function(e) {
let name = e.get('name');
let ignored = A(ignoreFiles).any(f => name.indexOf(f) >= 0);
let selected = filter.any(f => name.match(f));
return !ignored && selected;
});
},
load: observer('queryUrl', function() {
let self = this;
this.set('isLoading', true);
this.loadAllPages('', []).then(function(files) {
self.set('isLoading', false);
self.set('files', files.sort(function(a, b) {
return b.lastModified - a.lastModified;
}));
});
}),
loadAllPages(marker, files) {
let self = this;
let baseUrl = this.get('objectBaseUrl');
return $.get(this.get('queryUrl') + '&marker=' + marker).then(function(data) {
let contents = data.getElementsByTagName('Contents');
let isTruncated = data.getElementsByTagName('IsTruncated')[0].firstChild.data === "true";
let length = contents.length;
self.set('response', data);
for (let i = 0; i < length; i++) {
let size = contents[i].getElementsByTagName('Size')[0].firstChild.data;
let name = contents[i].getElementsByTagName('Key')[0].firstChild.data;
let lastModified = new Date(contents[i].getElementsByTagName('LastModified')[0].firstChild.data);
files.push(
S3File.create({
name: name,
size: size,
lastModified: lastModified,
relativePath: name,
baseUrl: baseUrl
})
);
}
if (isTruncated) {
let lastFile = files[files.length - 1];
return self.loadAllPages(lastFile.get('name'), files);
} else {
return files;
}
});
}
});