-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathCloudFile.js
More file actions
176 lines (149 loc) · 4.44 KB
/
Copy pathCloudFile.js
File metadata and controls
176 lines (149 loc) · 4.44 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
/*
CloudFiles
*/
CB.CloudFile = CB.CloudFile || function(file) {
if(!file)
throw "File is null.";
if (Object.prototype.toString.call(file) === '[object File]' || Object.prototype.toString.call(file) === '[object Blob]' ) {
this.fileObj = file;
this.document = {
_type: 'file',
name: (file && file.name && file.name !== "") ? file.name : 'unknown',
size: file.size,
url: '',
contentType : (typeof file.type !== "undefined" && file.type !== "") ? file.type : 'unknown'
};
} else if(typeof file === "string") {
if (file.match(/(((http|ftp|https):\/\/)|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#!]*[\w\-\@?^=%&/~\+#])?/g)) {
this.document = {
_type: 'file',
name: '',
size: '',
url: file,
contentType : ''
};
} else {
throw "Invalid File. It should be of type file or blob";
}
}
else{
throw "Invalid File. It should be of type file or blob";
}
};
Object.defineProperty(CB.CloudFile.prototype, 'type', {
get: function() {
return this.document.contentType;
},
set: function(type) {
this.document.contentType = type;
}
});
Object.defineProperty(CB.CloudFile.prototype, 'url', {
get: function() {
return this.document.url;
},
set: function(url) {
this.document.url = url;
}
});
Object.defineProperty(CB.CloudFile.prototype, 'size', {
get: function() {
return this.document.size;
},
set: function(size) {
this.document.size = size;
}
});
Object.defineProperty(CB.CloudFile.prototype, 'name', {
get: function() {
return this.document.name;
},
set: function(name) {
this.document.name = name;
}
});
CB.CloudFile.prototype.save = function(callback) {
var def;
if (!callback) {
def = new CB.Promise();
}
var thisObj = this;
var formdata = new FormData();
if(!this.fileObj)
throw "You cannot save a file which is null";
formdata.append("fileToUpload", this.fileObj);
formdata.append("key", CB.appKey);
var xmlhttp = CB._loadXml();
var params=formdata;
url = CB.serverUrl+'/file/' + CB.appId + '/upload' ;
xmlhttp.open('POST',url,true);
if (CB._isNode) {
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
xmlhttp.setRequestHeader("User-Agent",
"CB/" + CB.version +
" (NodeJS " + process.versions.node + ")");
}
var ssid = localStorage.getItem('sessionID');
if(ssid != null)
xmlhttp.setRequestHeader('sessionID', ssid);
xmlhttp.send(params);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == xmlhttp.DONE) {
if (xmlhttp.status == 200) {
thisObj.url = JSON.parse(xmlhttp.responseText)._url;
var sessionID = xmlhttp.getResponseHeader('sessionID');
if(sessionID)
localStorage.setItem('sessionID', sessionID);
else
localStorage.removeItem('sessionID');
if (callback) {
callback.success(thisObj);
} else {
def.resolve(thisObj);
}
} else {
if (callback) {
callback.error(xmlhttp.responseText);
} else {
def.reject(xmlhttp.responseText);
}
}
}
}
if (!callback) {
return def;
}
}
CB.CloudFile.prototype.delete = function(callback) {
var def;
if(!this.url) {
throw "You cannot delete a file which does not have an URL";
}
if (!callback) {
def = new CB.Promise();
}
var thisObj = this;
var params=JSON.stringify({
url: thisObj.url,
key: CB.appKey
});
url = CB.serverUrl+'/file/' + CB.appId + '/delete' ;
CB._request('POST',url,params).then(function(response){
thisObj.url = null;
if (callback) {
callback.success(thisObj);
} else {
def.resolve(thisObj);
}
},function(err){
if(callback){
callback.error(err);
}else {
def.reject(err);
}
});
if (!callback) {
return def;
}
}