-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathfirebase-storage.html
187 lines (168 loc) · 4.55 KB
/
firebase-storage.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
<!--
@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>
'use strict';
/**
* The firebase-storage element is an easy way to interact with a firebase
* storage as an object and expose it to the Polymer databinding system.
*
* For example:
*
* <firebase-storage
* path="/users/{{userId}}/files/{{filepath}}"
* files="[[fileArray]]"
* upload-tasks="{{uploadTasks}}">
* </firebase-storage>
*
* 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`.
*
* `<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',
behaviors: [
Polymer.FirebaseStorageBehavior
],
/**
* @override
*/
get isNew() {
return !this.path;
},
/**
* @override
*/
get zeroValue() {
return [];
},
/**
* Update the path and write this.files to that new location.
*
* Important note: `this.path` is updated asynchronously.
*
* @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(path) {
return new Promise(function(resolve, reject) {
if (!this.app) {
reject(new Error('No app configured!'));
}
if (path) {
resolve(this._putMultipleFirebaseFiles(path, this.files));
} else if (this.path) {
resolve(this._putMultipleFirebaseFiles(null, this.files));
} else {
resolve(this._putMultipleFirebaseFiles('/', this.files));
}
this.path = path ? this.path + '/' + path : this.path;
}.bind(this));
},
/**
* @override
*/
cancel: function(i) {
return new Promise(function(resolve, reject){
console.log(i)
var x = isNaN(parseInt(i, 10)) ? 0 : parseInt(i, 10);
resolve(this.tasks[x].cancel());
}.bind(this));
},
/**
* @override
*/
resume: function(i) {
return new Promise(function(resolve, reject){
var x = isNaN(parseInt(i, 10)) ? 0 : parseInt(i, 10);
resolve(this.tasks[x].resume());
}.bind(this));
},
/**
* @override
*/
pause: function(i) {
return new Promise(function(resolve, reject){
var x = isNaN(parseInt(i, 10)) ? 0 : parseInt(i, 10);
resolve(this.tasks[x].pause());
}.bind(this));
},
/**
* @override
*/
reset: function() {
this.path = null;
this.files = this.zeroValue;
this.uploadedFiles = this.zeroValue;
return Promise.resolve();
},
/**
* @override
*/
clearTasks: function() {
this.files = this.zeroValue;
this.uploadedFiles = this.zeroValue;
},
/**
* @override
*/
destroy: function() {
return this._putFirebaseFile().then(function() {
return this.reset();
}.bind(this));
},
/**
* @override
*/
getDownloadUrl: function(path) {
if (path) {
return this.storage.ref(path).getDownloadURL();
}
return this.storage.ref.getDownloadURL();
},
/**
* @override
*/
setPathFromUrl: function(url) {
if (url) {
this.path = this.getPathFromUrl(url);
return new Promise.resolve();
}
return new Promise.resolve();
},
/**
* @override
*/
getPathFromUrl: function(url) {
return url ? this.storage.refFromURL(url) : null;
},
/**
* @override
*/
getMetadata: function() {
return this.storage.ref.getMetadata();
},
/**
* @override
*/
setMetadata: function(metadata) {
return this.storage.ref.updateMetadata(metadata);
}
});
</script>