forked from out386/aria-telegram-mirror-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs-walk.js
More file actions
118 lines (111 loc) · 3.03 KB
/
Copy pathfs-walk.js
File metadata and controls
118 lines (111 loc) · 3.03 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
const fs = require('fs');
const mime = require('mime-types');
const gdrive = require('./drive/drive-upload.js');
/**
* Recursively uploads a directory or a file to Google Drive. Also makes this upload
* visible to everyone on Drive, then calls a callback with the public link to this upload.
* @param {string} path The path of the file or directory to upload
* @param {string} parent The ID of the Drive folder to upload into
* @param {function} callback A function to call with an error or the public Drive link
*/
function uploadRecursive (path, parent, callback) {
fs.stat(path, (err, stat) => {
if (err) {
callback(err);
return;
}
if (stat.isDirectory()) {
gdrive.uploadFileOrFolder(path, 'application/vnd.google-apps.folder', parent, (err, fileId) => {
if (err) {
callback(err);
} else {
walkSubPath(path, fileId, (err) => {
if (err) {
callback(err);
} else {
getSharableLink(fileId, true, callback);
}
});
}
});
} else {
processFileOrDir(path, parent, (err, fileId) => {
if (err) {
callback(err);
} else {
getSharableLink(fileId, false, callback);
}
});
}
});
}
function getSharableLink (fileId, isFolder, callback) {
gdrive.getSharableLink(fileId, isFolder, (err, link) => {
if (err) {
callback(err);
} else {
callback(null, link);
}
});
}
function walkSubPath (path, parent, callback) {
fs.readdir(path, (err, files) => {
if (err) {
callback(err);
} else {
walkSingleDir(path, files, parent, callback);
}
});
}
function walkSingleDir (path, files, parent, callback) {
if (files.length === 0) {
callback(null);
return;
}
var uploadNext = function (position) {
processFileOrDir(path + '/' + files[position], parent, (err, fileId) => {
if (err) {
callback(err);
} else {
if (++position < files.length) {
uploadNext(position);
} else {
callback(null);
}
}
});
};
uploadNext(0);
}
function processFileOrDir (path, parent, callback) {
console.log('processFileOrDir: ' + path);
fs.stat(path, (err, stat) => {
if (err) {
callback(err);
return;
}
if (stat.isDirectory()) {
// path is a directory. Do not call the callback until path has been completely traversed.
gdrive.uploadFileOrFolder(path, 'application/vnd.google-apps.folder', parent, (err, fileId) => {
if (err) {
callback(err);
} else {
walkSubPath(path, fileId, callback);
}
});
} else {
var mimeType = mime.lookup(path);
if (!mimeType) {
mimeType = 'application/octet-stream';
}
gdrive.uploadFileOrFolder(path, mimeType, parent, (err, fileId) => {
if (err) {
callback(err);
} else {
callback(null, fileId);
}
});
}
});
}
module.exports.uploadRecursive = uploadRecursive;