-
Notifications
You must be signed in to change notification settings - Fork 5
/
file-fire-fetch.html
77 lines (74 loc) · 2.13 KB
/
file-fire-fetch.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymerfire/firebase.html">
<dom-module id="file-fire-fetch">
<script>
/**
* An element that provides the url to a file based on the FireBase storage path
* Example:
* ```
* <firebase-app
* name="demo"
* api-key="API_KEY"
* auth-domain="AUTH_DOMAIN"
* database-url="DATABASE_URL"
* storage-bucket="convoofire.appspot.com">
* </firebase-app>
* <!-- A file-fire-fetch element allowing image upload to firebase storage -->
* <file-fire-fetch
* app-name="demo"
* path="/my/path/to/file.jpg"
* file-url="{{myFileURL}}"
* ></file-fire-fetch>
* ```
*
*
* @demo demo/file-fire-fetch.html
*/
Polymer({
is: 'file-fire-fetch',
properties: {
/**
* `appName` represents the name of the firebase-app that was instantiated
*/
appName: {
type: String,
},
/**
* `path` represents where the file should be saved, if the path doesnt exist it will be created
*/
path: {
type: String,
observer: "_updateFile"
},
/**
* `fileUrl` represents the file's direct url'
*/
fileUrl: {
type: String,
notify: true,
reflectToAttribute: true
},
fileExists: {
type: Boolean,
value: false,
notify: true
}
},
_updateFile: function(newVal){
if(newVal){
var storage = firebase.storage(firebase.app(this.appName));
var storageRef = storage.ref();
storageRef.child(this.path).getDownloadURL().then(function(url) {
this.fileUrl = url;
this.fileExists = true;
}.bind(this))
.catch(function(error) {
this.fire("no-file", {message:"No file exists at this location", error: error});
this.fileExists = false;
// console.log(error);
}.bind(this));
}
}
});
</script>
</dom-module>