-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode.js
113 lines (97 loc) · 3.66 KB
/
code.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
/**
* Watch Google Drive Files
* Author: [email protected]
* Web: https://digitalinspiration.com/
* MIT License
* */
// Enter the email address for email notification
const EMAIL = '[email protected]';
// How frequently should the watcher run? Put 2 for running every 2 days.
const RUNNING_FREQUENCY = 1;
// At what hour of the day should the script run?
const RUN_AT_HOUR = 10;
const enableDriveWatch = () => {
const triggerName = 'watchGoogleDrive';
const [trigger = null] = ScriptApp.getProjectTriggers().filter((t) => t.getHandlerFunction() === triggerName);
if (trigger === null) {
ScriptApp.newTrigger(triggerName).timeBased().everyDays(1).atHour(RUN_AT_HOUR).create();
}
};
const disableDriveWatch = () => {
ScriptApp.getProjectTriggers().forEach((trigger) => {
ScriptApp.deleteTrigger(trigger);
});
};
const checkChangedFiles_ = (items) => {
const cacheStore = CacheService.getScriptCache();
const processed = (cacheStore.get('cache') || '').split(',');
cacheStore.put('cache', items.map(({ id }) => id).join(','), 21600);
return items
.map(({ file }) => file)
.filter(({ id, alternateLink, title }) => id && alternateLink && title)
.filter(({ ownedByMe }) => ownedByMe)
.filter(({ id }) => processed.indexOf(id) === -1)
.filter(({ labels: { trashed = null } = {} }) => trashed === true)
.map((file) => {
const {
iconLink = '',
alternateLink = '',
title = '',
lastModifyingUser = {},
createdDate = '',
fileSize = '',
} = file;
const { emailAddress = '', displayName = '', picture: { url = '' } = {} } = lastModifyingUser;
const fileDate = createdDate ? Utilities.formatDate(new Date(createdDate), 'IST', 'MMMMM dd, YYYY') : '';
return [
iconLink ? `<img src="${iconLink}" height=16 />` : '',
`<a href="${alternateLink}">${title}</a>`,
fileSize ? `(${Math.round(fileSize / 1000)} Kb)` : '',
fileDate ? `Created: ${fileDate}` : '',
`${displayName || emailAddress}`,
url ? `<img src="${url}" height=16 />` : '',
];
});
};
const sendEmail_ = (rows = []) => {
const { length } = rows;
if (length === 0) return;
const html = [
`<table border="0" cellpadding="8" cellspacing="4" style="font-size:12px">`,
rows
.map((row) => row.map((td) => `<td>${td}</td>`).join(''))
.map((tr) => `<tr>${tr}</tr>`)
.join(''),
`</table>`,
`<p style="background:#ffffe0; padding:12px;font-size:12px;display:inline-block">`,
`<a href="https://www.labnol.org/google-drive-monitor-201026">Drive Watch</a> is developed by`,
` <a href="https://digitalinspiration.com/">Digital Inspiration</a></p>`,
];
MailApp.sendEmail({
to: EMAIL,
name: 'Drive Watch',
subject: `[Drive Watch] ${rows.length} files were deleted in your Google Drive`,
htmlBody: html.join(''),
});
};
const watchGoogleDrive = () => {
const propertyStore = PropertiesService.getScriptProperties();
const pageToken =
propertyStore.getProperty('token') || Drive.Changes.getStartPageToken({ supportsAllDrives: true }).startPageToken;
const fields =
'newStartPageToken,items(file(id,title,labels(trashed),iconLink,mimeType,createdDate,ownedByMe,lastModifyingUser(emailAddress,displayName,picture(url)),alternateLink, fileSize))';
const { newStartPageToken, items = [] } = Drive.Changes.list({
fields,
pageToken,
includeItemsFromAllDrives: true,
pageSize: 100,
supportsAllDrives: true,
});
if (newStartPageToken) {
propertyStore.setProperty('token', newStartPageToken);
}
if (items.length) {
const deletedItems = checkChangedFiles_(items);
sendEmail_(deletedItems);
}
};