-
Notifications
You must be signed in to change notification settings - Fork 142
/
firebase-storage-multiupload.html
211 lines (195 loc) · 6.17 KB
/
firebase-storage-multiupload.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!--
@license
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://github.com/firebase/polymerfire/blob/master/LICENSE
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="firebase-storage-behavior.html">
<script>
(function(){
'use strict';
/**
* The firebase-storage-multiupload element is an easy way to upload files by
* expose the firebase storage api to the Polymer databinding system.
*
* For example:
*
* <firebase-storage-multiupload
* path="/users/{{userId}}/files/{{filepath}}"
* files="[[fileArray]]"
* upload-tasks="{{uploadTasks}}">
* </firebase-storage-multiupload>
*
* This fetches the `fileArray` object, which is usually an array of Files,
* or a FileList, which are then automatically uploaded to
* `/users/${userId}/files/${filepath}` and then creates an array of upload
* tasks that are exposed through the Polymer databinding system via the
* `uploadTasks`. Changes to `fileArray` will likewise create a new set of
* uploads, which creates a new set of tasks, which are appended to the
* `uploadTasks`.
*
* You can then use `<firebase-storage-upload-task>` to cancel, pause or resume the upload.
* There are two ways to do this. You can encapsulate `firebase-storage-upload-task` in another
* element to have a local scope of the upload task's state:
*
* ```
* file-uploader
*
* <firebase-storage-multiupload
* path="/users/{{userId}}/files/{{filepath}}"
* files="[[fileArray]]"
* upload-tasks="{{uploadTasks}}">
* </firebase-storage-multiupload>
*
* <template is="dom-repeat" items="[[uploadTasks]]">
* <file-task item="[[item]]"></file-task>
* </template>
*
*
* file-task
*
* <firebase-storage-upload-task
* task="[[item]]"
* bytes-transferred="{{bytesTransferred}}"
* total-bytes="{{totalBytes}}"
* state="{{state}}"
* download-url="{{downloadUrl}}"
* metadata="{{metadata}}"
* path="{{path}}"></firebase-storage-upload-task>
*
* ```
*
* or you can just add the states in the uploadTasks list
*
* ```
* <firebase-storage-multiupload
* path="/users/{{userId}}/files/{{filepath}}"
* files="[[fileArray]]"
* upload-tasks="{{uploadTasks}}">
* </firebase-storage-multiupload>
*
* <template is="dom-repeat" items="[[uploadTasks]]">
* <firebase-storage-upload-task
* task="[[item]]"
* bytes-transferred="{{item.bytesTransferred}}"
* total-bytes="{{item.totalBytes}}"
* state="{{item.state}}"
* download-url="{{item.downloadUrl}}"
* metadata="{{item.metadata}}"
* path="{{item.path}}"></firebase-storage-upload-task>
* </template>
* ```
*
* `<firebase-storage>` needs some information about how to talk to Firebase.
* Set this configuration by adding a `<firebase-app>` element anywhere in your
* app.
*/
Polymer({
is: 'firebase-storage-multiupload',
properties: {
/**
* The files to be uploaded.
*/
files: {
type: Array
},
/**
* The upload tasks after invoking the Firebase storage put method
*
*/
uploadTasks: {
type: Array,
notify: true,
value: []
},
/**
* Uploads the files automatically when the file list has been changed/updated
*
*/
auto: {
type: Boolean,
value: false
}
},
behaviors: [
Polymer.FirebaseStorageBehavior
],
/**
* @override
*/
get isNew() {
return !this.path;
},
/**
* @override
*/
get zeroValue() {
return [];
},
observers: [
'__filesChanged(files, auto)'
],
/**
* Upload files, update the path and write this.files to that new location.
*
* Important note: `this.path` is updated asynchronously.
*
* @param {Array} Array of Files to be uploaded
* @param {string} path of the new firebase location to write to.
* @return {Promise} A promise that resolves once this.files has been
* written to the new path.
* @override
*/
upload: function(files, path) {
if (!this.app) {
this.fire('error', new Error('No app configured!'))
}
this._putMultipleFirebaseFiles(path || this.path, files && files.length ? files : this.files);
if (path) {
this.path = path;
}
},
/**
* Resets the firebase-storage-multiupload instance
*
*/
reset: function() {
this.path = null;
this.clearTasks();
return Promise.resolve();
},
/**
* Resets the upload tasks
*
*/
clearTasks: function() {
this.uploadTasks = [];
},
_putMultipleFirebaseFiles: function(path, files) {
this._log('Putting Multiple Firebase files at', path ? this.path + '/' + path : this.path);
files = files && typeof files === 'object' && files.name ? [files] : files;
if (files && files.length > 0) {
for (var i = 0; i < files.length; i++) {
var uploadTask = this.__put(path, files[i], this.metadata ? this.metadata : files[i].metadata);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot) {}, function(error) {
this.fire('error', error, { bubble: false});
}.bind(this));
this.push('uploadTasks', uploadTask);
}
}
},
__filesChanged: function(files, auto) {
if (auto && files && files.length) {
this.upload(files)
}
},
__pathChanged: function(path, oldPath) {
if (oldPath != null) {
this.clearTasks();
}
},
});
})()
</script>