forked from FirebaseExtended/polymerfire
-
Notifications
You must be signed in to change notification settings - Fork 2
/
firebase-storage-behavior.html
118 lines (103 loc) · 3.11 KB
/
firebase-storage-behavior.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
<!--
@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-common-behavior.html">
<link rel="import" href="firebase-storage-script.html">
<script>
(function() {
'use strict';
/** @polymerBehavior Polymer.FirebaseStorageBehavior */
Polymer.FirebaseStorageBehaviorImpl = {
properties: {
/**
* Firebase storage instance
*
*/
storage : {
type: Object,
computed: '__computeStorage(app)'
},
/**
* Firebase storage ref instance
*
*/
ref: {
type: Object,
computed: '__computeRef(storage, path)'
},
/**
* Path to a Firebase storage root or endpoint. N.B. `path` is case sensitive.
*/
path: {
type: String,
observer: '__pathChanged',
value: null
},
/**
* Forces every upload to be a unique file by adding a date of upload at the start of the file.
*
*/
forceUnique: {
type: Boolean,
value: false
},
/**
* When true, will perform detailed logging.
*/
log: {
type: Boolean,
value: false
}
},
get zeroValue() {
return [];
},
__put: function(path, file, metadata) {
this._log('Putting Firebase file at', path ? this.path + '/' + path : this.path);
if (file) {
var newFilename = this.forceUnique ? Date.now().toString() + '-' + file.name : file.name;
return path ? this.ref.root.child(path + '/' + newFilename).put(file, metadata) : this.ref.child(newFilename).put(file, metadata);
}
return path ? this.ref.child(path).delete() : this.ref.delete();
},
__putString: function(path, data, format, metadata) {
this._log('Putting Firebase file at', path ? this.path + '/' + path : this.path);
if (data) {
var ref = path ? this.storage.ref().child(path) : this.ref;
return ref.putString(data, format, metadata);
}
return path ? this.ref.child(path).delete() : this.ref.delete();
},
__computeStorage: function(app) {
return app ? app.storage() : null;
},
__computeRef: function(storage, path) {
if (storage == null ||
path == null ||
path.split('/').slice(1).indexOf('') >= 0) {
return null;
}
return storage.ref(path);
},
__pathChanged: function(path) {},
/**
* A wrapper around `console.log`.
*/
_log: function() {
if (this.log) {
console.log.apply(console, arguments);
}
}
};
/** @polymerBehavior */
Polymer.FirebaseStorageBehavior = [
Polymer.FirebaseCommonBehavior,
Polymer.FirebaseStorageBehaviorImpl
];
})();
</script>