|
| 1 | +<!-- |
| 2 | +@license |
| 3 | +Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +Use of this source code is governed by a BSD-style |
| 5 | +license that can be found in the LICENSE file or at |
| 6 | +https://github.com/firebase/polymerfire/blob/master/LICENSE |
| 7 | +--> |
| 8 | + |
| 9 | +<link rel="import" href="../../polymer/polymer.html"> |
| 10 | +<link rel="import" href="../../paper-progress/paper-progress.html"> |
| 11 | +<link rel="import" href="../../paper-toast/paper-toast.html"> |
| 12 | +<link rel="import" href="../polymerfire.html"> |
| 13 | + |
| 14 | +<dom-module id="firebase-storage-multiupload-auto"> |
| 15 | + <template> |
| 16 | + <firebase-app |
| 17 | + auth-domain="polymerfire-test-6abc0.firebaseapp.com" |
| 18 | + database-url="https://polymerfire-test-6abc0.firebaseio.com" |
| 19 | + api-key="AIzaSyBxZI1BAgX9obocHQw_yhmHV9BIOpaNWZo" |
| 20 | + storage-bucket="polymerfire-test-6abc0.appspot.com"> |
| 21 | + </firebase-app> |
| 22 | + |
| 23 | + <firebase-auth |
| 24 | + id="auth" |
| 25 | + user="{{user}}" |
| 26 | + provider="google" |
| 27 | + on-error="catchError"> |
| 28 | + </firebase-auth> |
| 29 | + |
| 30 | + <button on-tap="signIn">Sign In</button> <br> |
| 31 | + |
| 32 | + <template is="dom-if" if="{{user}}"> |
| 33 | + <firebase-storage-multiupload |
| 34 | + id="fs" |
| 35 | + path="/users/{{user.uid}}/files" |
| 36 | + files="[[files]]" |
| 37 | + upload-tasks="{{uploadedFiles}}" |
| 38 | + on-error="catchError" |
| 39 | + force-unique |
| 40 | + auto> |
| 41 | + </firebase-storage-multiupload> |
| 42 | + |
| 43 | + <input id="file-uploader" type="file" on-change="onFileUpload" multiple /> <br> |
| 44 | + |
| 45 | + <hr> |
| 46 | + |
| 47 | + <template is="dom-repeat" items="[[uploadedFiles]]"> |
| 48 | + <div style="padding: 20px"> |
| 49 | + <firebase-storage-upload-task |
| 50 | + task="[[item]]" |
| 51 | + id="task-[[index]]" |
| 52 | + bytes-transferred="{{item.bytesTransferred}}" |
| 53 | + total-bytes="{{item.totalBytes}}" |
| 54 | + state="{{item.state}}" |
| 55 | + download-url="{{item.downloadUrl}}" |
| 56 | + metadata="{{item.metadata}}" |
| 57 | + path="{{item.path}}"> |
| 58 | + </firebase-storage-upload-task> |
| 59 | + |
| 60 | + path from snapshot: [[item.snapshot.ref.fullPath]] <br> |
| 61 | + path from upload-task: [[item.path]] <br> |
| 62 | + bytes transferred: [[item.bytesTransferred]] <br> |
| 63 | + storage uri: [[gsUri]] <br> |
| 64 | + state: [[item.state]] <br> |
| 65 | + metadata-contentType: [[item.metadata.contentType]] <br> |
| 66 | + |
| 67 | + <br> |
| 68 | + |
| 69 | + <template is="dom-if" if="[[!isEqual(item.state, 'success')]]"> |
| 70 | + <paper-progress |
| 71 | + value="{{item.bytesTransferred}}" |
| 72 | + min="0" |
| 73 | + max="{{item.totalBytes}}"> |
| 74 | + </paper-progress> |
| 75 | + </template> |
| 76 | + |
| 77 | + <br> |
| 78 | + |
| 79 | + <template is="dom-if" if="{{isEqual(item.state, 'success')}}"> |
| 80 | + <firebase-storage-ref |
| 81 | + path="{{item.path}}" |
| 82 | + storage-uri="{{gsUri}}" |
| 83 | + id="ref-[[index]]" |
| 84 | + metadata="{{item.refMetadata}}" |
| 85 | + download-url="{{item.refDownloadUrl}}"> |
| 86 | + </firebase-storage-ref> |
| 87 | + |
| 88 | + <a href="{{item.refDownloadUrl}}">{{item.refDownloadUrl}}</a> <br> |
| 89 | + |
| 90 | + <button on-tap="download" value="{{index}}">Download</button> |
| 91 | + |
| 92 | + <button on-tap="deleteFile" value="{{index}}">Delete</button> |
| 93 | + </template> |
| 94 | + |
| 95 | + <template is="dom-if" if="[[!isEqual(item.state, 'success')]]"> |
| 96 | + <template is="dom-if" if="[[isEqual(item.state, 'running')]]"> |
| 97 | + <button on-tap="cancel" value="{{index}}">Cancel</button> |
| 98 | + <button on-tap="pause" value="{{index}}">Pause</button> |
| 99 | + </template> |
| 100 | + |
| 101 | + <template is="dom-if" if="[[isEqual(item.state, 'paused')]]"> |
| 102 | + <button on-tap="resume" value="{{index}}">Resume</button> |
| 103 | + </template> |
| 104 | + </template> |
| 105 | + |
| 106 | + <br> |
| 107 | + </div> |
| 108 | + </template> |
| 109 | + </template> |
| 110 | + |
| 111 | + <paper-toast id="toaster"></paper-toast> |
| 112 | + </template> |
| 113 | + |
| 114 | + <script> |
| 115 | + (function() { |
| 116 | + 'use strict'; |
| 117 | + |
| 118 | + Polymer({ |
| 119 | + is: 'firebase-storage-multiupload-auto', |
| 120 | + |
| 121 | + properties: { |
| 122 | + user: Object, |
| 123 | + |
| 124 | + files: { |
| 125 | + type: Array, |
| 126 | + notify: true, |
| 127 | + value: [], |
| 128 | + }, |
| 129 | + |
| 130 | + uploadTasks: { |
| 131 | + type: Array, |
| 132 | + observer: '_uploadTasksChanged' |
| 133 | + }, |
| 134 | + |
| 135 | + uploadedFiles: { |
| 136 | + type: Array, |
| 137 | + } |
| 138 | + }, |
| 139 | + |
| 140 | + catchError(e) { |
| 141 | + this.$$('#toaster').show({ |
| 142 | + text: e.detail.message |
| 143 | + }); |
| 144 | + }, |
| 145 | + |
| 146 | + cancel(e) { |
| 147 | + this.$$('#task-' + e.target.value).cancel(); |
| 148 | + }, |
| 149 | + |
| 150 | + resume(e) { |
| 151 | + this.$$('#task-' + e.target.value).resume(); |
| 152 | + }, |
| 153 | + |
| 154 | + pause(e) { |
| 155 | + this.$$('#task-' + e.target.value).pause(); |
| 156 | + }, |
| 157 | + |
| 158 | + download(e) { |
| 159 | + this.$$('#ref-' + e.target.value).getDownloadURL().then(function(d) { |
| 160 | + console.log(d) |
| 161 | + window.open(d, '_blank') |
| 162 | + }) |
| 163 | + }, |
| 164 | + |
| 165 | + deleteFile(e) { |
| 166 | + this.$$('#ref-' + e.target.value).delete().then(function(d) { |
| 167 | + console.log(d) |
| 168 | + }) |
| 169 | + }, |
| 170 | + |
| 171 | + _uploadTasksChanged(uploadTasks) { |
| 172 | + console.log(uploadTasks); |
| 173 | + }, |
| 174 | + |
| 175 | + _uploadedFilesChanged(uploadedFiles) { |
| 176 | + console.log(uploadedFiles); |
| 177 | + }, |
| 178 | + |
| 179 | + onFileUpload(e) { |
| 180 | + this.files = e.target.files; |
| 181 | + console.log(this.files) |
| 182 | + }, |
| 183 | + |
| 184 | + isEqual(a, b) { |
| 185 | + return a === b; |
| 186 | + }, |
| 187 | + |
| 188 | + signIn: function() { |
| 189 | + this.$.auth.signInAnonymously(); |
| 190 | + } |
| 191 | + }); |
| 192 | + })(); |
| 193 | + </script> |
| 194 | +</dom-module> |
0 commit comments