Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding firebase storage capability (w/ multiple files) #69

Merged
merged 34 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3850ca3
Added firebase-storage-behavior.html
tjmonsi Jul 7, 2016
72c1d5c
Added firebase-storage and fixed firebase-storage-behavior
tjmonsi Jul 7, 2016
24fc775
Fixed a bug in firebase-storage and added it in polymerfire
tjmonsi Jul 7, 2016
1074832
Fixed some bugs on the multiple uploads and added a demo
tjmonsi Jul 8, 2016
8d30ef5
Fixed delete
tjmonsi Jul 8, 2016
edbab3a
Current stable version done
tjmonsi Jul 8, 2016
d059a33
Added fix on firebase-storage-behavior.html
tjmonsi Jul 9, 2016
d2b3b6a
Updated firebase-storage-behavior and firebase-storage
tjmonsi Feb 14, 2017
b6e3bc7
Removed conflict
tjmonsi Feb 14, 2017
4a4143b
Resolved conflicts for PR
tjmonsi Feb 14, 2017
4f51cf7
Merge branch 'master' into adding-firebase-storage
tjmonsi Feb 14, 2017
cebbc8f
Reverting (adding new line)
tjmonsi Feb 14, 2017
a574f7c
changed to firebase-storage-multiupload
tjmonsi Apr 20, 2017
498c745
Merge
tjmonsi Apr 20, 2017
f884838
added ref and task
tjmonsi Apr 21, 2017
d238acd
Be consistent
tjmonsi Apr 21, 2017
9f2646e
being consistent
tjmonsi Apr 21, 2017
80abcf6
added putstring
tjmonsi Apr 21, 2017
31b45e2
renamed to uploadTask
tjmonsi Apr 21, 2017
9334f48
added documentation
tjmonsi May 4, 2017
41feaf6
Merge remote-tracking branch 'refs/remotes/firebase/master' into addi…
tjmonsi May 24, 2017
208b61e
fixing conflicts
tjmonsi May 24, 2017
03fb708
fixed the __pathChanged to check if storage has been initialized
tjmonsi May 24, 2017
feb8b05
fixed demo
tjmonsi May 24, 2017
12c0baa
added toString for gs
tjmonsi May 24, 2017
b466ab8
Add Cloud Storage URI to demo
FluorescentHallucinogen May 25, 2017
5d79cf1
Add paper-progress and paper-toast to devDependencies
FluorescentHallucinogen May 25, 2017
41b65ce
Add Cloud Storage URI to docs
FluorescentHallucinogen May 25, 2017
5603e0b
Fix 'string' property description
FluorescentHallucinogen May 25, 2017
b213bc9
Rename 'string' property to 'storage-uri'
FluorescentHallucinogen May 25, 2017
11e02e8
Fix mistake
FluorescentHallucinogen May 25, 2017
dcd3695
Merge pull request #4 from FluorescentHallucinogen/patch-1
tjmonsi May 25, 2017
b873894
Merge branch 'master' into adding-firebase-storage
tjmonsi May 25, 2017
58462a6
added fixes
tjmonsi May 26, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"devDependencies": {
"iron-component-page": "PolymerElements/iron-component-page#1 - 2",
"webcomponentsjs": "webcomponents/webcomponentsjs#^1.0.0",
"web-component-tester": "Polymer/web-component-tester#^6.0.0"
"web-component-tester": "Polymer/web-component-tester#^6.0.0",
"paper-progress": "PolymerElements/paper-progress#1 - 2",
"paper-toast": "PolymerElements/paper-toast#1 - 2"
},
"variants": {
"1.x": {
Expand All @@ -37,7 +39,9 @@
"devDependencies": {
"iron-component-page": "PolymerElements/iron-component-page#^1.0",
"web-component-tester": "^4.0.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0",
"paper-progress": "PolymerElements/paper-progress#^1.0",
"paper-toast": "PolymerElements/paper-toast#^1.0"
},
"resolutions": {
"webcomponentsjs": "^0.7"
Expand Down
201 changes: 201 additions & 0 deletions demo/firebase-storage-multiupload-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<!--
@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="../../paper-progress/paper-progress.html">
<link rel="import" href="../../paper-toast/paper-toast.html">
<link rel="import" href="../polymerfire.html">

<dom-module id="firebase-storage-multiupload-demo">
<template>
<firebase-app
auth-domain="polymerfire-test-6abc0.firebaseapp.com"
database-url="https://polymerfire-test-6abc0.firebaseio.com"
api-key="AIzaSyBxZI1BAgX9obocHQw_yhmHV9BIOpaNWZo"
storage-bucket="polymerfire-test-6abc0.appspot.com"></firebase-app>
<firebase-auth id="auth" user="{{user}}" provider="google" on-error="catchError"></firebase-auth>

<button on-tap="signIn">Sign In</button><br/>

<template is="dom-if" if="{{user}}">
<firebase-storage-multiupload
id="fs"
path="/users/{{user.uid}}/files"
files="[[files]]"
upload-tasks="{{uploadedFiles}}"
on-error="catchError"
force-unique
auto>
</firebase-storage-multiupload>

<input id="file-uploader" type="file" on-change="onFileUpload" multiple /> <br/>
<hr/>
<template is="dom-repeat" items="[[uploadedFiles]]">
<div style="padding: 20px">
<firebase-storage-upload-task
task="[[item]]"
id="task-[[index]]"
bytes-transferred="{{item.bytesTransferred}}"
total-bytes="{{item.totalBytes}}"
state="{{item.state}}"
download-url="{{item.downloadUrl}}"
metadata="{{item.metadata}}"
path="{{item.path}}"></firebase-storage-upload-task>



path from snapshot: [[item.snapshot.ref.fullPath]] <br/>
path from upload-task: [[item.path]] <br/>
storage uri: [[gsUri]] <br/>
state: [[item.state]] <br/>
metadata-contentType: [[item.metadata.contentType]] <br/>
<br/>

<template is="dom-if" if="[[!isEqual(item.state, 'success')]]">
<paper-progress value="{{item.bytesTransferred}}" min="0" max="{{item.totalBytes}}"></paper-progress>
</template>

<br/>


<template is="dom-if" if="{{isEqual(item.state, 'success')}}">

<firebase-storage-ref
path="{{item.path}}"
storage-uri="{{gsUri}}"
id="ref-[[index]]"
metadata="{{item.refMetadata}}"
download-url="{{item.refDownloadUrl}}"></firebase-storage-ref>

<a href="{{item.refDownloadUrl}}">
{{item.refDownloadUrl}}
</a><br/>
<button on-tap="download" value="{{index}}">Download</button>
<button on-tap="deleteFile" value="{{index}}">Delete</button>
</template>

<template is="dom-if" if="[[!isEqual(item.state, 'success')]]">


<template is="dom-if" if="[[isEqual(item.state, 'running')]]">
<button on-tap="cancel" value="{{index}}">Cancel</button>
<button on-tap="pause" value="{{index}}">pause</button>
</template>

<template is="dom-if" if="[[isEqual(item.state, 'paused')]]">
<button on-tap="resume" value="{{index}}">resume</button>
</template>
</template>
<br/>
</div>
</template>
</template>
<paper-toast id="toaster"></paper-toast>
</template>

<script>
(function() {
'use strict';

Polymer({
is: 'firebase-storage-multiupload-demo',

properties: {
user: Object,
files: {
type: Array,
notify: true,
value: [],
},
uploadTasks: {
type: Array,
observer: '_uploadTasksChanged'
},
uploadedFiles: {
type: Array,
}
},

catchError(e) {
this.$$('#toaster').show({
text: e.detail.message
});
},

cancel(e) {
this.$$('#task-' + e.target.value).cancel();
},

resume(e) {
this.$$('#task-' + e.target.value).resume();
},

pause(e) {
this.$$('#task-' + e.target.value).pause();
},

download(e) {
this.$$('#ref-' + e.target.value).getDownloadURL().then(function(d) {
console.log(d)
window.open(d, '_blank')
})
},

deleteFile(e) {
this.$$('#ref-' + e.target.value).delete().then(function(d) {
console.log(d)
})
},

// deleteFile(e) {
// this.$$('#fsd').path = e.target.value;
// this.$$('#fsd').destroy().then(function(x){
// this.$$('#toaster').show({
// text: 'Successfully deleted ' + e.target.value
// });
// }.bind(this)).catch(function(error){
// console.log(error);
// this.$$('#toaster').show({
// text: error.message
// });
// })
// },

_uploadTasksChanged(uploadTasks) {
console.log(uploadTasks);
},

_uploadedFilesChanged(uploadedFiles) {
console.log(uploadedFiles);
},

onFileUpload(e) {
this.files = e.target.files;
console.log(this.files)
},

onFileUpload2(e) {
this.file = e.target.files[0];
this.$$('#fs2').upload();
},

isImage(c) {
return c && c.indexOf('image') === 0;
},

isEqual(a, b) {
return a === b;
},

signIn: function() {
this.$.auth.signInAnonymously();
}
});
})();
</script>
</dom-module>
22 changes: 22 additions & 0 deletions demo/firebase-storage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<!--
@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
-->

<html>
<head>
<meta charset="utf-8">
<title>firebase-storage-multiupload demo</title>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="./firebase-storage-multiupload-demo.html">

</head>
<body>
<firebase-storage-multiupload-demo></firebase-storage-multiupload-demo>
</body>
</html>
101 changes: 101 additions & 0 deletions firebase-storage-behavior.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!--
@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
}
},

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;
var newPath = path ? path + '/' + newFilename: newFilename;
return this.ref.child(newPath).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) {}
};

/** @polymerBehavior */
Polymer.FirebaseStorageBehavior = [
Polymer.FirebaseCommonBehavior,
Polymer.FirebaseStorageBehaviorImpl
];
})();
</script>
Loading