forked from FirebaseExtended/polymerfire
-
Notifications
You must be signed in to change notification settings - Fork 2
/
firebase-storage-ref.html
217 lines (190 loc) · 5.42 KB
/
firebase-storage-ref.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
212
213
214
215
216
217
<!--
@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-ref 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-ref
* path="{{path}}"
* metadata="{{metadata}}"
* storage-uri="{{gsUri}}"
* download-url="{{downloadUrl}}">
* </firebase-storage-ref>
*
* This fetches file associated within the `path` attribute from the firebase storage
* and produces the metadata and the download url associated with it.
* It also exposes several firebase storage methods to manipulate and get
* additional data from it.
*
* `<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-ref',
properties: {
/**
* The url of the file for download
*/
downloadUrl: {
type: String,
notify: true
},
/**
* The metadata of the file
*/
metadata: {
type: Object,
notify: true
},
/**
* The Cloud Storage URI string of this object in the form `gs://<bucket>/<path>/<to>/<object>`
*/
storageUri: {
type: String,
notify: true
},
/**
* The upload task of the file when you use the put method.
*/
uploadTask: {
type: Object,
notify: true
}
},
behaviors: [
Polymer.FirebaseStorageBehavior
],
observers: [
'__pathChanged(path, storage)'
],
/**
* @override
*/
get isNew() {
return !this.path;
},
/**
* @override
*/
get zeroValue() {
return [];
},
__pathChanged: function(path) {
if (this.storage) {
this.getDownloadURL(path).then(function(downloadUrl) {
this.downloadUrl = downloadUrl;
}.bind(this)).catch(function(error) {
this.fire('error', error, { bubble: false});
}.bind(this));
this.getMetadata(path).then(function(metadata) {
this.metadata = metadata;
}.bind(this)).catch(function(error) {
this.fire('error', error, { bubble: false});
}.bind(this));
this.storageUri = this.toGsString(path);
}
},
/**
* Resets this element's path
*/
reset: function() {
this.path = null;
return Promise.resolve();
},
/**
* Sets the path from url
*/
setPathFromUrl: function(url) {
if (url) {
this.path = this.getPathFromUrl(url);
return new Promise.resolve();
}
return new Promise.resolve();
},
/**
* Get's the path from url
*/
getPathFromUrl: function(url) {
return url ? this.storage.refFromURL(url) : null;
},
/**
* Deletes the file associated in the firebase storage path
*/
delete: function() {
return this.__put().then(function() {
return this.reset();
}.bind(this));
},
/**
* Stores a new single file inside this path
*/
put: function(file, metadata) {
this.uploadTask = this.__put(null, file, metadata);
return this.uploadTask;
},
/**
* Stores a string in a given format
*/
putString: function(data, format, metadata) {
this.uploadTask = this.__putString(null, data, format, metadata);
return this.uploadTask;
},
/**
* Get the download url of the file
*/
getDownloadURL: function(path) {
if (path) {
return this.storage.ref(path).getDownloadURL();
} else if (this.ref) {
return this.ref.getDownloadURL();
}
return new Promise(function(resolve, reject) {resolve();});
},
/**
* Get the metadata of the file
*/
getMetadata: function(path) {
if (path) {
return this.storage.ref(path).getMetadata();
} else if (this.ref) {
return this.ref.getMetadata();
}
return new Promise(function(resolve, reject) {resolve();});
},
/**
* Returns a gs:// URL for this object in the form gs://<bucket>/<path>/<to>/<object>
*/
toGsString: function(path) {
if (path) {
return this.storage.ref(path).toString();
} else if (this.ref) {
return this.ref.toString();
}
},
/**
* Sets the metadata of the file
*/
setMetadata: function(metadata, path) {
if (path) {
return this.storage.ref(path).updateMetadata(metadata);
} else if (this.ref) {
return this.ref.updateMetadata(metadata);
}
return new Promise(function(resolve, reject) {resolve();});
}
});
})()
</script>