-
Notifications
You must be signed in to change notification settings - Fork 3
/
scitran.js
179 lines (163 loc) · 4.92 KB
/
scitran.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
import request from './request';
import config from '../config';
import fs from 'fs';
import crypto from 'crypto';
import files from './files';
/**
* Scitran
*
* A library for interactions with the
* scitran API.
*/
export default {
/**
* Verify User
*
* Checks if the currently logged in users
* in in the scitran system and returns a
* user object.
*/
verifyUser (req, res) {
request.getProxy(config.scitran.url + 'users/self', {headers: req.headers, status: 200}, res);
},
/**
* Is Super User
*/
isSuperUser(accessToken, callback) {
request.get(config.scitran.url + 'users/self', {
headers: {
Authorization: accessToken
}
}, (err, res) => {
callback(!!res.body.wheel);
});
},
/**
* Get User
*/
getUser (userId, callback) {
request.get(config.scitran.url + 'users/' + userId, {}, callback);
},
/**
* Get User by Token
*/
getUserByToken(accessToken, callback) {
request.get(config.scitran.url + 'users/self', {
headers: {
Authorization: accessToken
}
}, callback);
},
/**
* Create User
*/
createUser(user, callback) {
request.post(config.scitran.url + 'users', {body: user}, () => {
this.createGroup(user._id, user._id, callback);
});
},
/**
* Create Group
*
* Takes a groupName and a userId and
* creates a group with that user as the
* admin.
*/
createGroup (groupName, userId, callback) {
let body = {
_id: groupName,
name: groupName
};
request.post(config.scitran.url + 'groups', {body: body}, () => {
this.addRole('groups', groupName, {_id: groupName, access: 'admin', site: 'local'}, callback);
});
},
/**
* Get Project
*
*/
getProject (projectId, callback, options) {
let modifier = options && options.snapshot ? 'snapshots/' : '';
request.get(config.scitran.url + modifier + 'projects/' + projectId, {}, callback);
},
/**
* Get Project Snapshots
*/
getProjectSnapshots (projectId, callback) {
request.get(config.scitran.url + 'projects/' + projectId + '/snapshots', {
query: {public: true}
}, callback);
},
/**
* Update Project
*
*/
updateProject (projectId, body, callback) {
request.put(config.scitran.url + 'projects/' + projectId, {body}, (err, res) => {
callback(err, res);
});
},
/**
* Add Tag
*/
addTag (containerType, containerId, tag, callback) {
request.post(config.scitran.url + containerType + '/' + containerId + '/tags', {
body: {value: tag}
}, callback);
},
/**
* Remove Tag
*/
removeTag (containerType, containerId, tag, callback) {
request.del(config.scitran.url + containerType + '/' + containerId + '/tags/' + tag, {}, callback);
},
/**
* Add Role
*/
addRole(container, id, role, callback) {
request.post(config.scitran.url + container + '/' + id + '/roles', {body: role}, callback);
},
/**
* Download Symlink Dataset
*
* Downloads a tar archive of symlinks to reconstruct a
* BIDS dataset. Stores it under a hash id in a local
* file store and updates all symlinks to point to the
* correct files in scitran's file store.
*/
downloadSymlinkDataset(datasetId, callback, options) {
let modifier = options && options.snapshot ? 'snapshots/' : '';
request.post(config.scitran.url + modifier + 'download', {
query: {format: 'bids', query: true},
body: {
nodes: [
{
_id: datasetId,
level: 'project'
}
],
optional: false
}
}, (err, res) => {
if (!res.body.ticket) {
callback({status: 404, message: 'Dataset not found.'});
return;
}
let ticket = res.body.ticket;
request.get(config.scitran.url + 'download', {query: {symlinks: true, ticket: ticket}}, (err2, res2) => {
if (!err2) {
let hash = crypto.createHash('md5').update(res2.body).digest('hex');
fs.readdir(config.location + '/persistent/datasets/', (err3, contents) => {
if (contents && contents.indexOf(hash) > -1) {
callback(err, hash);
} else {
files.saveSymlinks(hash, res2.body, () => {
callback(err, hash);
});
}
});
}
});
});
}
};