-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-s3-upload.html
83 lines (72 loc) · 1.78 KB
/
api-s3-upload.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
<link rel="import" href="../iron-ajax/iron-ajax.html">
<dom-module id="api-s3-upload">
<template>
<iron-ajax id="get" method="GET" url="{{url}}" params="{{params}}"
last-response="{{response_get}}" on-response="on_response_get">
</iron-ajax>
<iron-ajax id="put" method="PUT" url="{{response_get.signed_url}}"
last-response="{{response_put}}" on-response="on_response_put">
</iron-ajax>
</template>
</dom-module>
<script>
Polymer({
is: 'api-s3-upload',
properties: {
file: {
type: Object
},
collection: {
type: String
},
folder: {
type: String,
value: ''
},
fileName: {
type: String
},
response: {
type: Object,
notify: true
},
error: {
type: Object,
notify: true
}
},
send: function (file) {
if (file) {
this.file = file;
}
if (!this.file) {
return;
}
this._compute_url();
this._compute_params();
this.$.get.generateRequest();
},
_compute_url: function () {
var url = '/api/' + this.collection + '/signed_put';
url = url.replace(/\/\/+/, '/');
this.url = url;
},
_compute_params: function () {
var params = {};
params.file_name = this.folder + this.fileName;
params.file_type = this.file.type;
this.params = params;
},
on_response_get: function (event) {
event.stopPropagation();
this.$.put.body = this.file;
this.$.put.generateRequest();
},
on_response_put: function (event) {
event.stopPropagation();
this.response = this.response_put;
this.fire('response', this.response);
this.fire('upload', this.response);
}
});
</script>