-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonedrive.js
204 lines (186 loc) · 7 KB
/
onedrive.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
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
201
202
203
204
// http://msdn.microsoft.com/en-us/office/office365/howto/common-file-tasks-client-library
var fs = require("fs");
var path = require("path");
var o365 = require("office365-js");
var O365Auth = o365.O365Auth;
var O365Discovery = o365.O365Discovery;
var SharePointClient = o365.Microsoft.CoreServices.SharePointClient;
var Folder = o365.Microsoft.FileServices.Folder;
var File = o365.Microsoft.FileServices.File;
var CommonAuthority = "https://login.windows.net/common";
var DiscoveryResourceId = "https://api.office.com/discovery/";
O365Auth.Settings = {
clientId: process.env.O365_CLIENT_ID,
redirectUri: "http://localhost/"
};
// sharepoint task
module.exports = {
_client: null,
getClient: function(clientId, callback) {
if (typeof clientId == "string") {
O365Auth.Settings.clientId = clientId;
} else {
callback = clientId;
}
var self = this;
if (self._client)
callback(self._client);
else
acquireToken(function(token) {
getClient(token, "MyFiles", SharePointClient, function(client) {
callback(self._client = new Wrapper(client));
});
});
}
};
function Wrapper(client) {
this.client = client;
}
function acquireToken(callback) {
console.log("[Auth] OAuth2 " + CommonAuthority);
var ResourceId = DiscoveryResourceId;
var authContext = new O365Auth.Context(CommonAuthority);
authContext.getIdToken(ResourceId).then(function (token) {
console.log("[Auth] Success");
callback(token);
}, function (reason) {
console.log("[Auth] Failure");
console.log(reason);
});
}
function getClient(token, capability, clientClass, callback) {
console.log("[Client] finding service client: " + capability);
discoveryService(token, function (services) {
var service = services.filter(function (s) {
return s.capability == capability;
});
if (service.length > 0 && (service = service[0])) {
var endpoint = service.endpointUri.replace(/(\/v1.0)?$/, "/v1.0"); // sharepoint bug
console.log("[Client] endpointUri: " + endpoint + "\n resourceId: " + service.resourceId);
callback(new clientClass(endpoint, token.getAccessTokenFn(service.resourceId)));
} else {
console.log("[Client] Not Found!");
}
});
}
function discoveryService(token, callback) {
console.log("[Discovery] get services");
var context = new O365Discovery.Context();
context.services(token.getAccessTokenFn(DiscoveryResourceId)).then(function (services) {
console.log("[Discovery] Success");
callback(services);
}, function (reason) {
console.log("[Discovery] Failure");
console.log(reason);
});
}
Wrapper.prototype.downloadFile = function(fileId, filepath, callback) {
console.log("[Files] downloadFile: " + fileId);
this.client.files.getItem(fileId).asFile().content().then(function (data) {
console.log("[Files] saving to " + filepath);
fs.writeFile(filepath, data, callback);
}, callback);
};
Wrapper.prototype.getFile = function(fileId, callback) {
console.log("[Files] getFile: " + fileId);
this.client.files.getItem(fileId).asFile().fetch().then(function (result) {
callback(null, result);
}, callback);
};
Wrapper.prototype.getFolder = function(folderId, callback) {
console.log("[Files] getFolder: " + folderId);
this.client.files.getItem(folderId).asFolder().fetch().then(function (result) {
callback(null, result);
}, callback);
};
Wrapper.prototype.getFiles = function(folderId, callback) {
console.log("[Files] getFiles in folder: " + folderId);
var parent;
if (typeof folderId === "string") {
parent = this.client.files.getItem(folderId).asFolder().children;
} else {
parent = this.client.files;
callback = folderId;
}
parent.getItems().fetch().then(function (result) {
callback(null, result.currentPage);
}, callback);
};
Wrapper.prototype.getFileByName = function(title, folderId, callback) {
console.log("[Files] getFileByName: " + title);
var parent;
if (typeof folderId === "string") {
parent = this.client.files.getItem(folderId).asFolder().children;
} else {
parent = this.client.files;
callback = folderId;
}
parent.getByPath("/" + title).then(function (result) {
callback(null, result);
}, callback);
};
Wrapper.prototype.searchFile = function(query, callback) {
console.log("[Files] searchFile: query = " + JSON.stringify(query));
var titleQuery = query.title;
var type = query.folder != null ? (query.folder ? "Folder" : "File") : null;
this.getFiles(query.parentId, function (err, result) {
if (err != null) {
callback(err);
return;
}
var file = result.filter(function (item) {
return item.name.search(titleQuery) !== -1 && (!type || item.type == type);
});
if (file.length > 0 && (file = file[0])) {
callback(null, file);
} else {
callback("Not found", null);
}
});
};
Wrapper.prototype.createFolder = function(title, callback) {
console.log("[Files] createFolder: " + title);
var folder = new Folder(this.client.context);
folder.name = title;
this.client.files.addItem(folder).then(function (item) {
callback(null, item);
}, callback);
};
Wrapper.prototype.createFile = function(title, folderId, callback) {
console.log("[Files] createFile: " + title);
var file = new File(this.client.context);
file.name = title;
if (typeof folderId == "function") { callback = folderId; folderId = null; }
var parent = folderId ? this.client.files.getItem(folderId).asFolder().children : this.client.files;
parent.addItem(file).then(function (item) {
callback(null, item);
});
};
Wrapper.prototype.uploadContent = function(filepath, fileId, etag, callback) {
console.log("[Files] uploadContent: " + filepath);
var self = this, client = this.client;
fs.readFile(filepath, function (err, data) {
if (err) callback(err, null);
else {
client.files.getItem(fileId).asFile().uploadContent(data).then(function() {
console.log("[Files] uploadFile: file uploaded.");
self.getFile(fileId, callback);
}, callback);
}
});
};
Wrapper.prototype.uploadOfficeFile = function(filepath, fileId, etag, callback) {
console.log("[Files] uploadFile: " + filepath);
var wrapper = this;
if (typeof(fileId) == "function") {
// create
callback = fileId;
wrapper.createFile(path.basename(filepath), function(err, item) {
console.log("[Files] uploadFile: file is created. id: " + item.id);
wrapper.uploadContent(filepath, item.id, item.eTag, callback);
});
} else {
// upload
wrapper.uploadContent(filepath, fileId, etag, callback);
}
};