-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.gs
170 lines (133 loc) · 4.48 KB
/
code.gs
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
function doGet() {
setMainTrigger ();
ScriptApp.newTrigger('main')
.timeBased()
.after(10)
.create();
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function main() {
var VERSION = 1;
var PRE_VER = PropertiesService.getUserProperties().getProperty("version");
if (PRE_VER != VERSION) {
onUpdate(VERSION);
}
var time = new Date();
var startTime = time.getTime();
var user = Session.getActiveUser().getEmail();
var root = DriveApp.getRootFolder();
var folderToken = PropertiesService.getUserProperties().getProperty("folderToken");
var fileToken = PropertiesService.getUserProperties().getProperty("fileToken");
var lostfoundid = PropertiesService.getUserProperties().getProperty("lostfoundid");
var lostfound;
try {
lostfound = DriveApp.getFolderById(lostfoundid);
lostfound.setTrashed(false);
}
catch (e) {
lostfound = DriveApp.createFolder("Lost+Found");
lostfound.setStarred(true);
root.removeFolder(lostfound);
lostfoundid = lostfound.getId();
PropertiesService.getUserProperties().setProperty("lostfoundid", lostfoundid);
}
lostfound.setStarred(true);
var folderIterator;
var fileIterator;
try {
folderIterator = DriveApp.continueFolderIterator(folderToken);
}
catch (e) {
folderIterator = DriveApp.getFolders();
folderToken = folderIterator.getContinuationToken();
PropertiesService.getUserProperties().setProperty("folderToken", folderToken);
}
try {
fileIterator = DriveApp.continueFileIterator(fileToken);
}
catch (e) {
fileIterator = DriveApp.getFiles();
fileToken = fileIterator.getContinuationToken();
PropertiesService.getUserProperties().setProperty("fileToken", fileToken);
}
while (folderIterator.hasNext()){
check(folderIterator.next().getId(), false, root, lostfound, user);
var time = new Date();
var now = time.getTime();
if (now-startTime > 275000 ){
folderToken = folderIterator.getContinuationToken();
PropertiesService.getUserProperties().setProperty("folderToken", folderToken);
return;
}
}
folderToken = folderIterator.getContinuationToken();
PropertiesService.getUserProperties().setProperty("folderToken", folderToken);
while (fileIterator.hasNext()){
check(fileIterator.next().getId(), true, root, lostfound, user);
var time = new Date();
var now = time.getTime();
if (now-startTime > 275000 ){
fileToken = fileIterator.getContinuationToken();
PropertiesService.getUserProperties().setProperty("fileToken", fileToken);
return;
}
}
PropertiesService.getUserProperties().setProperty("folderToken", 0);
PropertiesService.getUserProperties().setProperty("fileToken", 0);
}
function check(id, isFile, root, lostfound, user) {
// This is root or Lost+Found folder of us. They cannot be orphaned.
if (id == root.getId() || id == lostfound.getId()) {
return false;
}
var f;
if (isFile)
f = DriveApp.getFileById(id);
else
f = DriveApp.getFolderById(id);
var parents = f.getParents();
//It does not have parent, it may be orphan
if (!parents.hasNext()) {
//It is trashed, we cannot rescue it.
if (f.isTrashed())
return false;
//Else it will be added to Lost+Found
}
//It has parents, but it may be orphan
else {
while(parents.hasNext()) {
var parent = parents.next();
// Its parent is not trashed, thus the fate of this file/folder is dependent on its parent.
if (!parent.isTrashed())
return false;
// The parent is trashed and also owned by me.
// It means the parent intentionaly deleted by me.
// It is not lost, its deleted also.
else if (parent.getOwner().getEmail() == user)
return false;
// Parent is not owned by me and it is trashed.
// It means it is not deleted by me. Maybe I don't want it to be deleted.
// This folder will be rescued.
}
}
if(isFile)
lostfound.addFile(f);
else
lostfound.addFolder(f);
return true;
}
function setMainTrigger () {
var triggers = ScriptApp.getProjectTriggers();
for (var count = 0; count < triggers.length; count++){
ScriptApp.deleteTrigger(triggers[count]);
}
ScriptApp.newTrigger('main')
.timeBased()
.everyHours(2)
.create();
}
function onUpdate (ver) {
setMainTrigger();
PropertiesService.getUserProperties().setProperty("version", ver);
}