-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathshare.html
144 lines (125 loc) · 3.67 KB
/
share.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<html>
<head>
<title>pCloud SDK: Examples / Upload</title>
<style>
body {
margin: 0;
padding: 20px;
}
.setup {
position: fixed;
top: 20px;
right: 20px;
}
* {
font-family: arial;
font-size: 13px;
}
input[type=text], input[type=number] {
line-height: 22px;
padding: 0 0 0 4px;
}
</style>
</head>
<body>
<div class="setup">
<table cellpadding="3">
<tr>
<td>Folderid:</td>
<td><input type="number" size="5" maxlength="20" value="" id="folderid" /></td>
</tr>
<tr>
<td>Auth token:</td>
<td><input type="text" size="40" id="auth" value="" /></td>
</tr>
<tr>
<td colspan="2" align="center"><button id="save">Save Account</button></td>
</tr>
<tr><td> </td></tr>
<tr>
<td>OAuth: </td>
<td><button id="gettoken" disabled>Get Token</button></td>
</tr>
</table>
</div>
<input type="file" id="file" disabled="disabled">
<script type="text/javascript" src="examples/pcloudsdk.js"></script>
<script>
'use strict';
var clientid = "p1WznE2dEPm";
var access_token = false;
var locationid = "";
var client = false;
var clientUserId = false;
var authClient = false;
var sharefolder = false;
el("file").addEventListener('change', function(e) {
if (!client) {
this.value = '';
console.error('no token, click the button to get token.');
return;
}
var file = this.files[0];
authClient.appshare(sharefolder, clientUserId, clientid, "edit").then(function() {
uploadFile(file).then(function() {
client.listfolder(sharefolder).then(function(metadata) {
console.log("Client listfolder");
metadata.contents.forEach(item => console.log(item));
});
authClient.listfolder(sharefolder).then(function(metadata) {
console.log("Sharer listfolder");
metadata.contents.forEach(item => console.log(item));
});
});
});
function uploadFile(file) {
return client.upload(file, sharefolder, {
onBegin: function() {
console.log('Upload started.');
},
onProgress: function(progress) {
console.log(progress.direction, progress.loaded, progress.total, Math.round(progress.loaded / progress.total * 100, 1));
},
onFinish: function(uploadData) {
console.log(uploadData);
}
});
}
}, false);
function el(id) {
return document.getElementById(id);
}
el('gettoken').addEventListener('click', function(e) {
pCloudSdk.oauth.initOauthToken({
client_id: clientid,
redirect_uri: 'http://127.0.0.1:8080/oauth.html',
receiveToken: function(token, id) {
console.log(token, id);
access_token = token;
locationid = id || 1;
client = pCloudSdk.createClient(token);
client.userinfo().then(function (userinfo) {
clientUserId = userinfo.userid;
el('file').disabled = false;
});
}
});
}, false);
el("save").addEventListener("click", function(e) {
var token = el("auth").value;
var folderid = el("folderid").value;
if (!token || !folderid) {
return;
}
var tryClient = pCloudSdk.createClient(token, "pcloud", false);
tryClient.api("stat", { params: { folderid: folderid }}).then(function(ret) {
if (ret.metadata) {
sharefolder = ret.metadata.folderid;
authClient = tryClient;
el("gettoken").disabled = false;
}
});
});
</script>
</body>
</html>