-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
57 lines (50 loc) · 1.48 KB
/
util.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
"use strict";
/**
* @desc various utility functions needed by the server
*/
var fs = require('fs-extra');
var async = require('async');
var Zip = require('zip-zip-top');
/**
* @desc checks if a path exists, and creates it if not
* @param path: String or Array of Strings of path(s) to create
* @param callback: node style callback
*/
exports.createPath = function(path, callback) {
if (path instanceof Array)
async.map(path, fs.mkdirs, callback);
else
fs.mkdirs(path, callback);
};
/**
* @desc creates the necessary paths for a new paper
* @param parentPath: String path to parent of the new paper (eg. /data/papers/)
* @param UID: String of the new paper
* @param callback: node style callback
*/
exports.newPaperDir = function(parentPath, UID, callback) {
var paths = [
parentPath + '/' + UID + '/tex',
parentPath + '/' + UID + '/html',
parentPath + '/' + UID + '/datasets'
];
exports.createPath(paths, callback);
};
/*
* @desc zips the folder where uploaded files are stored
* @param id the id of publication
* @param callback node style callback
*/
exports.zipPaper = function(parentPath, id, callback){
// set path of local folders
var localpath = parentPath + '/' + id;
// set target path for .zip
var zippath = parentPath + '/' + id + '.zip';
// zip the whole paper folder
var zip = new Zip();
zip.zipFolder(localpath, function(err){
if (err) return callback(err);
// write zip to target path
zip.writeToFile(zippath, callback);
});
};