-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
164 lines (119 loc) · 4.22 KB
/
background.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var uploads = new Map();
// browser.cloudFile.onAccountAdded.addListener(async (account) => {
// //console.log("Account Added", account.id)
// })
async function getAccountInfo(accountId) {
let accountInfo = await browser.storage.local.get([accountId]);
if (!accountInfo[accountId] || !("webdavUrl" in accountInfo[accountId])) {
throw new Error("No Accounts found.");
}
return accountInfo[accountId];
}
browser.cloudFile.onFileUpload.addListener(async (account, params) => {
let { id, name, data } = params;
name = "" + Date.now() + "_" + name;
console.log("onFileUpload", id, account, name);
let accountInfo = await getAccountInfo(account.id);
//console.log("accountInfo", accountInfo);
let uploadInfo = {
id,
name,
abortController: new AbortController(),
};
uploads.set(id, uploadInfo);
let {webdavUrl, username, token, path} = accountInfo;
const authHeader = "Basic " + btoa(username + ":" + token);
let url = webdavUrl + path + encodeURIComponent(name);
let headers = {
"Content-Type": "application/octet-stream",
Authorization: authHeader
};
let fetchInfo = {
method: "PUT",
headers,
body: data,
signal: uploadInfo.abortController.signal,
};
//console.log("uploading to ", url, fetchInfo);
let response = await fetch(url, fetchInfo);
//console.log("file upload response", response);
delete uploadInfo.abortController;
if (response.status > 299) {
throw new Error("response was not ok: server status code: " + response.status + ", response message: " + response.statusText);
}
const serverUrl = webdavUrl.substr(0, webdavUrl.indexOf("remote.php"));
const shareUrl = serverUrl + "ocs/v1.php/apps/files_sharing/api/v1/shares?format=json";
uploadInfo.abortController = new AbortController();
uploads.set(id, uploadInfo);
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
Authorization: authHeader,
"OCS-APIRequest": true
};
fetchInfo = {
method: "POST",
headers,
body: "shareType=3&path=" + encodeURIComponent(path + name),
signal: uploadInfo.abortController.signal,
};
console.log("requesting public link", shareUrl, fetchInfo);
response = await fetch(shareUrl, fetchInfo);
//console.log("public link response", response);
if(response.ok)
{
let respJson = await response.json();
uploadInfo.shareID = respJson.ocs.data.id;
uploads.set(id, uploadInfo);
return {url: respJson.ocs.data.url + "/download"};
}
else
return {aborted: true}
});
browser.cloudFile.onFileUploadAbort.addListener((account, id) => {
//console.log("aborting upload", id);
let uploadInfo = uploads.get(id);
if (uploadInfo && uploadInfo.abortController) {
uploadInfo.abortController.abort();
}
});
browser.cloudFile.onFileDeleted.addListener(async (account, id) => {
//console.log("delete upload", id);
let uploadInfo = uploads.get(id);
if (!uploadInfo) {
return;
}
// FIXME how do we get a confirmation popup in TB MailExtensions?
// let wishDelete = confirm("Do you wish to delete the file on the server?");
// if(!wishDelete){
// return;
// }
let accountInfo = await getAccountInfo(account.id);
let {shareID} = uploadInfo;
let {webdavUrl, username, token} = accountInfo;
const authHeader = "Basic " + btoa(username + ":" + token);
const serverUrl = webdavUrl.substr(0, webdavUrl.indexOf("remote.php"));
const shareUrl = serverUrl + "ocs/v1.php/apps/files_sharing/api/v1/shares/" + shareID;
let headers = {
Authorization: authHeader,
"OCS-APIRequest": true
};
let fetchInfo = {
headers,
method: "DELETE",
};
//console.log("sending delete", url, fetchInfo);
let response = await fetch(shareUrl, fetchInfo);
//console.log("delete response", response);
uploads.delete(id);
if (response.status > 299) {
throw new Error("response was not ok: server status code: " + response.status + ", response message: " + response.statusText);
}
});
browser.cloudFile.getAllAccounts().then(async (accounts) => {
let allAccountsInfo = await browser.storage.local.get();
for (let account of accounts) {
await browser.cloudFile.updateAccount(account.id, {
configured: account.id in allAccountsInfo,
});
}
});