-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudstore.js
More file actions
200 lines (175 loc) · 7.06 KB
/
Copy pathcloudstore.js
File metadata and controls
200 lines (175 loc) · 7.06 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//=============================================================================
// cloudstore.js v1.0
//=============================================================================
/**
* @desc this class holds functions for communicating with enjine.io cloud storage utility
* It is designed to work with paid subscribers
* @authors copyright 2021 enjine.io, David Hurren david.enjine.io@gmail.com, Ben Hurren, Gareth Wiseman
*/
class CloudStore
{
constructor(key, server)
{
//Properties
if(!server) this.server = "https://enjine.cloud/cloudstore"
else this.server = server;
this.m_apiKey = key;
this.m_maxRate = 3000;
this.m_tlast = 0;
this.m_retry = true;
this.m_version = 1;
}
rateCheck()
{
var n = +new Date();
var tdiff = n - this.m_tlast;
if(tdiff < this.m_maxRate)
{
return false
}
this.m_tlast = n;
return true
}
// SEND DATA THROUGH XMLHTTPREQUEST
async sendData(url,options)
{
try {
if(url === undefined) return Promise.reject("url or options not defined");
if(options !== undefined && options.method === undefined) options.method = 'POST'
const result = await fetch(url,options)
return result.json()
} catch(error) {
console.error(error)
return {error:"Error sending data!"}
}
}
save(file, obj, callback, password)
{
try {
if( !this.rateCheck() ) {
setTimeout( ()=>{ this.save(file,obj,callback,password) }, this.m_maxRate + 500 )
return
}
const json = {key: this.m_apiKey, file: file, options: null, id: "_data", value: obj, password: password}
const url = this.server + "/store/save"
const options = {body:JSON.stringify(json),headers:{"Content-type":"application/json; charset=UTF-8"}}
this.sendData(url,options).then((response) => { if(callback) callback(response); })
} catch(error) {
console.error(error)
if(callback) callback({error:"Error saving data!"})
}
}
merge(file, obj, callback, password)
{
try {
if( !this.rateCheck() ) {
setTimeout( ()=>{ this.merge(file,obj,callback,password) }, this.m_maxRate + 500 )
return
}
const json = { key: this.m_apiKey, file: file, options: "merge", id: "_data", value: obj, password: password}
const url = this.server + "/store/save"
const options = { body:JSON.stringify(json), headers:{"Content-type":"application/json; charset=UTF-8"} }
this.sendData(url,options).then((response) => { if(callback) callback(response); })
} catch(error) {
console.error(error)
if(callback) callback({error:"Error merging data!"})
}
}
delete(file, callback, password)
{
try {
if( !this.rateCheck() ) {
setTimeout( ()=>{ this.delete(file,callback,password) }, this.m_maxRate + 500 )
return
}
const json = { key: this.m_apiKey, file: file, options: "delete", id: "_data", value: null, password: password}
const url = this.server + "/store/save"
const options = { body:JSON.stringify(json), headers:{"Content-type":"application/json; charset=UTF-8"} }
this.sendData(url,options).then((response) => { if(callback) callback(response); })
} catch(error) {
console.error(error)
if(callback) callback({error:"Error deleting data!"})
}
}
load(file, callback, password)
{
try {
if( !this.rateCheck() ) {
setTimeout( ()=>{ this.load(file,callback,password) }, this.m_maxRate + 500 )
return
}
var json = { key: this.m_apiKey, file: file, options: null, id: "_data", password: password };
const url = this.server + "/store/load"
const options = { body:JSON.stringify(json), headers:{"Content-type":"application/json; charset=UTF-8"}}
this.sendData(url,options).then((response) => {
response.data = response.message || "";
if(callback) callback(response);
})
} catch(error) {
console.error(error)
if(callback) callback({error:"Error loading data!"})
}
}
list(file, callback)
{
try {
if( !this.rateCheck() ) {
setTimeout( ()=>{ this.list(file,callback) }, this.m_maxRate + 500 )
return
}
if(!file) file = "";
var json = { key: this.m_apiKey, file: file, options: "list", id: "_data" };
const url = this.server + "/store/load"
const options = { body:JSON.stringify(json), headers:{"Content-type":"application/json; charset=UTF-8"}}
this.sendData(url,options).then((response) => {
response.data = response.message || "";
if(callback) callback(response); }
)
} catch(error) {
console.error(error)
if(callback) callback({error:"Error listing data!"})
}
}
upload(data, name, type, callback, password)
{
try {
if( !this.rateCheck() ) {
//setTimeout( ()=>{ this.upload(data, name, type, callback, password) }, this.m_maxRate + 500 )
//we don't want to keep trying to upload the same file so we'll just return
return
}
var formData = new FormData();
const blob = typeof data=="string" ? this.b64toBlob(data, name, type) : data
formData.append("file", blob);
formData.append("key", this.m_apiKey);
formData.append("password", password);
const url = this.server + "/upload-2"
const options = { body:formData }
this.sendData(url,options).then((response) => { if(callback) callback(response); })
} catch(error) {
console.error(error)
if(callback) callback({error:"Error uploading the file!"})
}
}
b64toBlob(b64Data, fileName='',contentType='', sliceSize=512) {
try {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
console.log(byteArrays);
const blob = new File(byteArrays, fileName, {type: contentType});
return blob;
} catch(error) {
console.error(error)
return ""
}
}
}