drivekit 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +300 -86
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +293 -82
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +327 -112
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +62 -10
- package/dist/index.d.ts +62 -10
- package/dist/index.js +325 -111
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -40,14 +40,39 @@ var init_cjs_shims = __esm({
|
|
|
40
40
|
// src/server/webdav.ts
|
|
41
41
|
var webdav_exports = {};
|
|
42
42
|
__export(webdav_exports, {
|
|
43
|
+
escapeXml: () => escapeXml,
|
|
43
44
|
startWebDAVServer: () => startWebDAVServer
|
|
44
45
|
});
|
|
46
|
+
function escapeXml(unsafe) {
|
|
47
|
+
return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
48
|
+
}
|
|
45
49
|
async function startWebDAVServer(drive2, options = {}) {
|
|
46
50
|
const port = options.port || 8080;
|
|
47
51
|
const host = options.host || "127.0.0.1";
|
|
48
52
|
await drive2.init();
|
|
49
53
|
const server = import_node_http2.default.createServer(async (req, res) => {
|
|
50
54
|
try {
|
|
55
|
+
if (options.auth?.username && options.auth?.password) {
|
|
56
|
+
const authHeader = req.headers["authorization"];
|
|
57
|
+
if (!authHeader || !authHeader.startsWith("Basic ")) {
|
|
58
|
+
res.writeHead(401, {
|
|
59
|
+
"WWW-Authenticate": 'Basic realm="DriveKit WebDAV"',
|
|
60
|
+
"Content-Type": "text/plain"
|
|
61
|
+
});
|
|
62
|
+
res.end("Authentication required");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const credentials = Buffer.from(authHeader.replace("Basic ", ""), "base64").toString("utf8");
|
|
66
|
+
const [user, pass] = credentials.split(":");
|
|
67
|
+
if (user !== options.auth.username || pass !== options.auth.password) {
|
|
68
|
+
res.writeHead(401, {
|
|
69
|
+
"WWW-Authenticate": 'Basic realm="DriveKit WebDAV"',
|
|
70
|
+
"Content-Type": "text/plain"
|
|
71
|
+
});
|
|
72
|
+
res.end("Invalid credentials");
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
51
76
|
const parsedUrl = import_node_url.default.parse(req.url || "/", true);
|
|
52
77
|
const decodedPath = decodeURIComponent(parsedUrl.pathname || "/");
|
|
53
78
|
const virtualPath = decodedPath === "" ? "/" : decodedPath;
|
|
@@ -56,7 +81,8 @@ async function startWebDAVServer(drive2, options = {}) {
|
|
|
56
81
|
res.writeHead(200, {
|
|
57
82
|
DAV: "1, 2",
|
|
58
83
|
Allow: "OPTIONS, GET, HEAD, POST, PUT, DELETE, MKCOL, MOVE, COPY, PROPFIND",
|
|
59
|
-
"MS-Author-Via": "DAV"
|
|
84
|
+
"MS-Author-Via": "DAV",
|
|
85
|
+
"Accept-Ranges": "bytes"
|
|
60
86
|
});
|
|
61
87
|
res.end();
|
|
62
88
|
return;
|
|
@@ -74,7 +100,7 @@ async function startWebDAVServer(drive2, options = {}) {
|
|
|
74
100
|
case "HEAD": {
|
|
75
101
|
if (virtualPath === "/" || virtualPath === "") {
|
|
76
102
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
77
|
-
res.end("<h1>DriveKit WebDAV Server Running</h1><p>Connect via Finder or WebDAV client.</p>");
|
|
103
|
+
res.end("<h1>DriveKit WebDAV Server Running</h1><p>Connect via Finder, File Explorer, or WebDAV client.</p>");
|
|
78
104
|
return;
|
|
79
105
|
}
|
|
80
106
|
try {
|
|
@@ -84,15 +110,42 @@ async function startWebDAVServer(drive2, options = {}) {
|
|
|
84
110
|
res.end("Cannot GET a directory");
|
|
85
111
|
return;
|
|
86
112
|
}
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
"
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
113
|
+
const fileSize = file.size ? parseInt(file.size, 10) : void 0;
|
|
114
|
+
const rangeHeader = req.headers["range"];
|
|
115
|
+
if (rangeHeader && rangeHeader.startsWith("bytes=") && fileSize !== void 0) {
|
|
116
|
+
const parts = rangeHeader.replace("bytes=", "").split("-");
|
|
117
|
+
const start = parseInt(parts[0], 10);
|
|
118
|
+
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
|
|
119
|
+
if (isNaN(start) || start >= fileSize || end !== void 0 && end >= fileSize) {
|
|
120
|
+
res.writeHead(416, { "Content-Range": `bytes */${fileSize}` });
|
|
121
|
+
res.end();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const stream = await drive2.createReadStream(virtualPath, { start, end });
|
|
125
|
+
const chunkLength = end - start + 1;
|
|
126
|
+
res.writeHead(206, {
|
|
127
|
+
"Content-Type": file.mimeType || "application/octet-stream",
|
|
128
|
+
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
|
|
129
|
+
"Content-Length": chunkLength,
|
|
130
|
+
"Accept-Ranges": "bytes"
|
|
131
|
+
});
|
|
132
|
+
if (req.method === "HEAD") {
|
|
133
|
+
res.end();
|
|
134
|
+
} else {
|
|
135
|
+
stream.pipe(res);
|
|
136
|
+
}
|
|
94
137
|
} else {
|
|
95
|
-
stream.
|
|
138
|
+
const stream = await drive2.createReadStream(virtualPath);
|
|
139
|
+
res.writeHead(200, {
|
|
140
|
+
"Content-Type": file.mimeType || "application/octet-stream",
|
|
141
|
+
"Content-Length": file.size || void 0,
|
|
142
|
+
"Accept-Ranges": "bytes"
|
|
143
|
+
});
|
|
144
|
+
if (req.method === "HEAD") {
|
|
145
|
+
res.end();
|
|
146
|
+
} else {
|
|
147
|
+
stream.pipe(res);
|
|
148
|
+
}
|
|
96
149
|
}
|
|
97
150
|
} catch (e) {
|
|
98
151
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
@@ -101,27 +154,29 @@ async function startWebDAVServer(drive2, options = {}) {
|
|
|
101
154
|
return;
|
|
102
155
|
}
|
|
103
156
|
case "PUT": {
|
|
104
|
-
const remoteDir =
|
|
105
|
-
const fileName = import_node_path5.default.basename(virtualPath);
|
|
157
|
+
const remoteDir = import_node_path6.default.dirname(virtualPath);
|
|
106
158
|
await drive2.mkdirp(remoteDir);
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
await drive2.upload({
|
|
113
|
-
source: buffer,
|
|
114
|
-
filename: fileName,
|
|
115
|
-
parent: remoteDir,
|
|
116
|
-
overwrite: true
|
|
117
|
-
});
|
|
118
|
-
res.writeHead(201, { "Content-Type": "text/plain" });
|
|
119
|
-
res.end("Created");
|
|
120
|
-
} catch (err) {
|
|
121
|
-
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
122
|
-
res.end(err.message || "Upload failed");
|
|
159
|
+
let existed = false;
|
|
160
|
+
try {
|
|
161
|
+
const existingFile = await drive2.getFile(virtualPath);
|
|
162
|
+
if (existingFile && existingFile.id) {
|
|
163
|
+
existed = true;
|
|
123
164
|
}
|
|
124
|
-
}
|
|
165
|
+
} catch {
|
|
166
|
+
existed = false;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const uploadStream = await drive2.createWriteStream(virtualPath, {
|
|
170
|
+
overwrite: true
|
|
171
|
+
});
|
|
172
|
+
req.pipe(uploadStream);
|
|
173
|
+
await uploadStream.done;
|
|
174
|
+
res.writeHead(existed ? 204 : 201, { "Content-Type": "text/plain" });
|
|
175
|
+
res.end(existed ? "" : "Created");
|
|
176
|
+
} catch (err) {
|
|
177
|
+
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
178
|
+
res.end(err.message || "Upload failed");
|
|
179
|
+
}
|
|
125
180
|
return;
|
|
126
181
|
}
|
|
127
182
|
case "MKCOL": {
|
|
@@ -153,14 +208,24 @@ async function startWebDAVServer(drive2, options = {}) {
|
|
|
153
208
|
res.end("Destination header missing");
|
|
154
209
|
return;
|
|
155
210
|
}
|
|
156
|
-
const destUrl = import_node_url.default.parse(destination);
|
|
157
|
-
const destPath = decodeURIComponent(destUrl.pathname || "");
|
|
158
|
-
const newName = import_node_path5.default.basename(destPath);
|
|
159
|
-
const newParent = import_node_path5.default.dirname(destPath);
|
|
160
211
|
try {
|
|
161
|
-
await drive2.
|
|
162
|
-
if (
|
|
163
|
-
|
|
212
|
+
const file = await drive2.getFile(virtualPath);
|
|
213
|
+
if (!file?.id) {
|
|
214
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
215
|
+
res.end("Source file not found");
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const destUrl = import_node_url.default.parse(destination);
|
|
219
|
+
const destPath = decodeURIComponent(destUrl.pathname || "");
|
|
220
|
+
const newName = import_node_path6.default.basename(destPath);
|
|
221
|
+
const newParentDir = import_node_path6.default.dirname(destPath);
|
|
222
|
+
const newParentFolderId = await drive2.mkdirp(newParentDir);
|
|
223
|
+
const currentParentDir = import_node_path6.default.dirname(virtualPath);
|
|
224
|
+
if (newParentDir !== currentParentDir) {
|
|
225
|
+
await drive2.move(file.id, newParentFolderId);
|
|
226
|
+
}
|
|
227
|
+
if (newName !== import_node_path6.default.basename(virtualPath)) {
|
|
228
|
+
await drive2.rename(file.id, newName);
|
|
164
229
|
}
|
|
165
230
|
res.writeHead(201);
|
|
166
231
|
res.end("Moved");
|
|
@@ -170,6 +235,34 @@ async function startWebDAVServer(drive2, options = {}) {
|
|
|
170
235
|
}
|
|
171
236
|
return;
|
|
172
237
|
}
|
|
238
|
+
case "COPY": {
|
|
239
|
+
const destination = req.headers["destination"];
|
|
240
|
+
if (!destination || typeof destination !== "string") {
|
|
241
|
+
res.writeHead(400, { "Content-Type": "text/plain" });
|
|
242
|
+
res.end("Destination header missing");
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const file = await drive2.getFile(virtualPath);
|
|
247
|
+
if (!file?.id) {
|
|
248
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
249
|
+
res.end("Source file not found");
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const destUrl = import_node_url.default.parse(destination);
|
|
253
|
+
const destPath = decodeURIComponent(destUrl.pathname || "");
|
|
254
|
+
const newName = import_node_path6.default.basename(destPath);
|
|
255
|
+
const newParentDir = import_node_path6.default.dirname(destPath);
|
|
256
|
+
const newParentFolderId = await drive2.mkdirp(newParentDir);
|
|
257
|
+
await drive2.copy(file.id, { parent: newParentFolderId, name: newName });
|
|
258
|
+
res.writeHead(201);
|
|
259
|
+
res.end("Copied");
|
|
260
|
+
} catch (err) {
|
|
261
|
+
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
262
|
+
res.end(err.message);
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
173
266
|
default: {
|
|
174
267
|
res.writeHead(501, { "Content-Type": "text/plain" });
|
|
175
268
|
res.end("Method Not Implemented");
|
|
@@ -239,13 +332,14 @@ ${responses.join("\n")}
|
|
|
239
332
|
}
|
|
240
333
|
function renderWebDAVResponse(href, isDir, size, modifiedTime) {
|
|
241
334
|
const encodedHref = encodeURI(href);
|
|
335
|
+
const displayName = escapeXml(import_node_path6.default.basename(href) || "root");
|
|
242
336
|
const resourceType = isDir ? "<D:collection/>" : "";
|
|
243
337
|
const dateFormatted = new Date(modifiedTime).toUTCString();
|
|
244
338
|
return ` <D:response>
|
|
245
339
|
<D:href>${encodedHref}</D:href>
|
|
246
340
|
<D:propstat>
|
|
247
341
|
<D:prop>
|
|
248
|
-
<D:displayname>${
|
|
342
|
+
<D:displayname>${displayName}</D:displayname>
|
|
249
343
|
<D:resourcetype>${resourceType}</D:resourcetype>
|
|
250
344
|
<D:getcontentlength>${isDir ? "0" : size}</D:getcontentlength>
|
|
251
345
|
<D:getlastmodified>${dateFormatted}</D:getlastmodified>
|
|
@@ -255,14 +349,14 @@ function renderWebDAVResponse(href, isDir, size, modifiedTime) {
|
|
|
255
349
|
</D:propstat>
|
|
256
350
|
</D:response>`;
|
|
257
351
|
}
|
|
258
|
-
var import_node_http2, import_node_url,
|
|
352
|
+
var import_node_http2, import_node_url, import_node_path6;
|
|
259
353
|
var init_webdav = __esm({
|
|
260
354
|
"src/server/webdav.ts"() {
|
|
261
355
|
"use strict";
|
|
262
356
|
init_cjs_shims();
|
|
263
357
|
import_node_http2 = __toESM(require("http"), 1);
|
|
264
358
|
import_node_url = __toESM(require("url"), 1);
|
|
265
|
-
|
|
359
|
+
import_node_path6 = __toESM(require("path"), 1);
|
|
266
360
|
}
|
|
267
361
|
});
|
|
268
362
|
|
|
@@ -300,6 +394,7 @@ __export(index_exports, {
|
|
|
300
394
|
createDrive: () => createDrive,
|
|
301
395
|
createWebhookPlugin: () => createWebhookPlugin,
|
|
302
396
|
default: () => index_default,
|
|
397
|
+
escapeXml: () => escapeXml,
|
|
303
398
|
formatBytes: () => formatBytes,
|
|
304
399
|
formatDuration: () => formatDuration,
|
|
305
400
|
formatSpeed: () => formatSpeed,
|
|
@@ -321,7 +416,7 @@ init_cjs_shims();
|
|
|
321
416
|
// src/core/client.ts
|
|
322
417
|
init_cjs_shims();
|
|
323
418
|
var import_node_fs6 = __toESM(require("fs"), 1);
|
|
324
|
-
var
|
|
419
|
+
var import_node_path7 = __toESM(require("path"), 1);
|
|
325
420
|
var import_drive3 = require("@googleapis/drive");
|
|
326
421
|
|
|
327
422
|
// src/auth/authenticator.ts
|
|
@@ -597,8 +692,8 @@ var PathResolver = class {
|
|
|
597
692
|
/**
|
|
598
693
|
* Gets cache key for a path
|
|
599
694
|
*/
|
|
600
|
-
getCacheKey(
|
|
601
|
-
return `${this.rootFolderId}:${this.normalizePath(
|
|
695
|
+
getCacheKey(path8) {
|
|
696
|
+
return `${this.rootFolderId}:${this.normalizePath(path8)}`;
|
|
602
697
|
}
|
|
603
698
|
/**
|
|
604
699
|
* Clears the path cache
|
|
@@ -612,8 +707,8 @@ var PathResolver = class {
|
|
|
612
707
|
/**
|
|
613
708
|
* Invalidate a single path from cache
|
|
614
709
|
*/
|
|
615
|
-
invalidatePath(
|
|
616
|
-
const norm = this.normalizePath(
|
|
710
|
+
invalidatePath(path8) {
|
|
711
|
+
const norm = this.normalizePath(path8);
|
|
617
712
|
const key = this.getCacheKey(norm);
|
|
618
713
|
this.cache.delete(key);
|
|
619
714
|
if (this.metadataCache) {
|
|
@@ -724,13 +819,14 @@ var PathResolver = class {
|
|
|
724
819
|
file = sharedResponse.data.files?.[0];
|
|
725
820
|
}
|
|
726
821
|
if (file && file.id) {
|
|
822
|
+
const segmentParentId = currentParentId;
|
|
727
823
|
currentParentId = file.id;
|
|
728
824
|
if (this.metadataCache) {
|
|
729
825
|
this.metadataCache.upsertFile({
|
|
730
826
|
id: file.id,
|
|
731
827
|
name: file.name || segment,
|
|
732
828
|
mimeType: file.mimeType || "",
|
|
733
|
-
parentId:
|
|
829
|
+
parentId: segmentParentId,
|
|
734
830
|
path: currentPath,
|
|
735
831
|
trashed: false
|
|
736
832
|
});
|
|
@@ -768,13 +864,14 @@ var PathResolver = class {
|
|
|
768
864
|
if (!newFolder.id) {
|
|
769
865
|
throw new Error(`Failed to create folder "${segment}" under parent "${currentParentId}"`);
|
|
770
866
|
}
|
|
867
|
+
const segmentParentId = currentParentId;
|
|
771
868
|
currentParentId = newFolder.id;
|
|
772
869
|
if (this.metadataCache) {
|
|
773
870
|
this.metadataCache.upsertFile({
|
|
774
871
|
id: newFolder.id,
|
|
775
872
|
name: segment,
|
|
776
873
|
mimeType: GOOGLE_WORKSPACE_MIME_TYPES.FOLDER,
|
|
777
|
-
parentId:
|
|
874
|
+
parentId: segmentParentId,
|
|
778
875
|
path: currentPath,
|
|
779
876
|
trashed: false
|
|
780
877
|
});
|
|
@@ -2112,12 +2209,39 @@ var DriveFolderRef = class {
|
|
|
2112
2209
|
|
|
2113
2210
|
// src/core/plugins.ts
|
|
2114
2211
|
init_cjs_shims();
|
|
2212
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
2115
2213
|
var PluginManager = class {
|
|
2116
2214
|
plugins = [];
|
|
2215
|
+
/**
|
|
2216
|
+
* Register a plugin. If a plugin with the same name already exists,
|
|
2217
|
+
* it replaces the old one to avoid duplicate side-effects.
|
|
2218
|
+
*/
|
|
2117
2219
|
use(plugin) {
|
|
2220
|
+
this.plugins = this.plugins.filter((p) => p.name !== plugin.name);
|
|
2118
2221
|
this.plugins.push(plugin);
|
|
2119
2222
|
return this;
|
|
2120
2223
|
}
|
|
2224
|
+
/**
|
|
2225
|
+
* Unregister a plugin by its name.
|
|
2226
|
+
* Returns true if a plugin was found and removed, false otherwise.
|
|
2227
|
+
*/
|
|
2228
|
+
unuse(pluginName) {
|
|
2229
|
+
const initialLen = this.plugins.length;
|
|
2230
|
+
this.plugins = this.plugins.filter((p) => p.name !== pluginName);
|
|
2231
|
+
return this.plugins.length < initialLen;
|
|
2232
|
+
}
|
|
2233
|
+
/**
|
|
2234
|
+
* Check if a plugin with given name is currently active
|
|
2235
|
+
*/
|
|
2236
|
+
has(pluginName) {
|
|
2237
|
+
return this.plugins.some((p) => p.name === pluginName);
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* Returns all currently registered plugins
|
|
2241
|
+
*/
|
|
2242
|
+
getPlugins() {
|
|
2243
|
+
return [...this.plugins];
|
|
2244
|
+
}
|
|
2121
2245
|
async runBeforeUpload(options) {
|
|
2122
2246
|
let current = { ...options };
|
|
2123
2247
|
for (const plugin of this.plugins) {
|
|
@@ -2161,45 +2285,62 @@ var PluginManager = class {
|
|
|
2161
2285
|
};
|
|
2162
2286
|
function createWebhookPlugin(options) {
|
|
2163
2287
|
const events = options.events || ["upload"];
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2288
|
+
const timeoutMs = options.timeout ?? 5e3;
|
|
2289
|
+
const maxRetries = options.retries ?? 0;
|
|
2290
|
+
const sendWebhook = async (event, payload) => {
|
|
2291
|
+
let lastError = null;
|
|
2292
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
2293
|
+
let timeoutId;
|
|
2294
|
+
const controller = new AbortController();
|
|
2295
|
+
timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
2168
2296
|
try {
|
|
2169
|
-
await fetch(options.url, {
|
|
2297
|
+
const res = await fetch(options.url, {
|
|
2170
2298
|
method: "POST",
|
|
2171
2299
|
headers: { "Content-Type": "application/json", ...options.headers || {} },
|
|
2172
|
-
body: JSON.stringify(
|
|
2173
|
-
|
|
2174
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2175
|
-
file: {
|
|
2176
|
-
id: file.id,
|
|
2177
|
-
name: file.name,
|
|
2178
|
-
size: file.size,
|
|
2179
|
-
mimeType: file.mimeType
|
|
2180
|
-
}
|
|
2181
|
-
})
|
|
2300
|
+
body: JSON.stringify(payload),
|
|
2301
|
+
signal: controller.signal
|
|
2182
2302
|
});
|
|
2183
|
-
|
|
2303
|
+
clearTimeout(timeoutId);
|
|
2304
|
+
if (!res.ok) {
|
|
2305
|
+
throw new Error(`Webhook returned HTTP ${res.status}: ${res.statusText}`);
|
|
2306
|
+
}
|
|
2307
|
+
return;
|
|
2308
|
+
} catch (err) {
|
|
2309
|
+
clearTimeout(timeoutId);
|
|
2310
|
+
lastError = err;
|
|
2311
|
+
if (attempt === maxRetries) {
|
|
2312
|
+
if (options.onError) {
|
|
2313
|
+
options.onError(lastError instanceof Error ? lastError : new Error(String(lastError)), event);
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2184
2316
|
}
|
|
2317
|
+
}
|
|
2318
|
+
};
|
|
2319
|
+
return {
|
|
2320
|
+
name: "drivekit-official-webhook",
|
|
2321
|
+
async afterUpload(file) {
|
|
2322
|
+
if (!events.includes("upload")) return;
|
|
2323
|
+
await sendWebhook("upload", {
|
|
2324
|
+
event: "drivekit:upload",
|
|
2325
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2326
|
+
file: {
|
|
2327
|
+
id: file.id,
|
|
2328
|
+
name: file.name,
|
|
2329
|
+
size: file.size,
|
|
2330
|
+
mimeType: file.mimeType
|
|
2331
|
+
}
|
|
2332
|
+
});
|
|
2185
2333
|
},
|
|
2186
2334
|
async afterDownload(result) {
|
|
2187
2335
|
if (!events.includes("download")) return;
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
name: result.name,
|
|
2197
|
-
size: result.size
|
|
2198
|
-
}
|
|
2199
|
-
})
|
|
2200
|
-
});
|
|
2201
|
-
} catch (e) {
|
|
2202
|
-
}
|
|
2336
|
+
await sendWebhook("download", {
|
|
2337
|
+
event: "drivekit:download",
|
|
2338
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2339
|
+
file: {
|
|
2340
|
+
name: result.name,
|
|
2341
|
+
size: result.size
|
|
2342
|
+
}
|
|
2343
|
+
});
|
|
2203
2344
|
}
|
|
2204
2345
|
};
|
|
2205
2346
|
}
|
|
@@ -2209,13 +2350,11 @@ function createAutoNamePlugin(options) {
|
|
|
2209
2350
|
beforeUpload(opts) {
|
|
2210
2351
|
let name = opts.filename;
|
|
2211
2352
|
if (!name && typeof opts.source === "string") {
|
|
2212
|
-
|
|
2213
|
-
name = pathModule2.basename(opts.source);
|
|
2353
|
+
name = import_node_path4.default.basename(opts.source);
|
|
2214
2354
|
}
|
|
2215
2355
|
if (!name) return opts;
|
|
2216
|
-
const
|
|
2217
|
-
const
|
|
2218
|
-
const base = pathModule.basename(name, ext);
|
|
2356
|
+
const ext = import_node_path4.default.extname(name);
|
|
2357
|
+
const base = import_node_path4.default.basename(name, ext);
|
|
2219
2358
|
let finalName = base;
|
|
2220
2359
|
if (options.prefix) finalName = `${options.prefix}${finalName}`;
|
|
2221
2360
|
if (options.appendTimestamp) {
|
|
@@ -2234,7 +2373,7 @@ function createAutoNamePlugin(options) {
|
|
|
2234
2373
|
// src/core/cache.ts
|
|
2235
2374
|
init_cjs_shims();
|
|
2236
2375
|
var import_node_os = __toESM(require("os"), 1);
|
|
2237
|
-
var
|
|
2376
|
+
var import_node_path5 = __toESM(require("path"), 1);
|
|
2238
2377
|
var import_node_fs5 = __toESM(require("fs"), 1);
|
|
2239
2378
|
var SqliteMetadataCache = class {
|
|
2240
2379
|
db = null;
|
|
@@ -2245,14 +2384,14 @@ var SqliteMetadataCache = class {
|
|
|
2245
2384
|
if (customPath) {
|
|
2246
2385
|
this.dbPath = customPath;
|
|
2247
2386
|
} else {
|
|
2248
|
-
const dir =
|
|
2387
|
+
const dir = import_node_path5.default.join(import_node_os.default.homedir(), ".drivekit");
|
|
2249
2388
|
if (!import_node_fs5.default.existsSync(dir)) {
|
|
2250
2389
|
try {
|
|
2251
2390
|
import_node_fs5.default.mkdirSync(dir, { recursive: true });
|
|
2252
2391
|
} catch {
|
|
2253
2392
|
}
|
|
2254
2393
|
}
|
|
2255
|
-
this.dbPath =
|
|
2394
|
+
this.dbPath = import_node_path5.default.join(dir, "cache.db");
|
|
2256
2395
|
}
|
|
2257
2396
|
}
|
|
2258
2397
|
init() {
|
|
@@ -2270,7 +2409,7 @@ var SqliteMetadataCache = class {
|
|
|
2270
2409
|
CREATE TABLE IF NOT EXISTS files (
|
|
2271
2410
|
id TEXT PRIMARY KEY,
|
|
2272
2411
|
name TEXT NOT NULL,
|
|
2273
|
-
mimeType TEXT
|
|
2412
|
+
mimeType TEXT,
|
|
2274
2413
|
size INTEGER,
|
|
2275
2414
|
parentId TEXT,
|
|
2276
2415
|
path TEXT,
|
|
@@ -2356,7 +2495,7 @@ var SqliteMetadataCache = class {
|
|
|
2356
2495
|
stmt.run(
|
|
2357
2496
|
entry.id,
|
|
2358
2497
|
entry.name,
|
|
2359
|
-
entry.mimeType,
|
|
2498
|
+
entry.mimeType ?? null,
|
|
2360
2499
|
entry.size ?? null,
|
|
2361
2500
|
entry.parentId ?? null,
|
|
2362
2501
|
entry.path ?? null,
|
|
@@ -2392,6 +2531,30 @@ var SqliteMetadataCache = class {
|
|
|
2392
2531
|
const stmt = this.db.prepare("DELETE FROM files WHERE id = ?");
|
|
2393
2532
|
stmt.run(id);
|
|
2394
2533
|
}
|
|
2534
|
+
pruneMissing(activeIds) {
|
|
2535
|
+
if (this.fallbackMemory) {
|
|
2536
|
+
return this.fallbackMemory.pruneMissing(activeIds);
|
|
2537
|
+
}
|
|
2538
|
+
if (!this.initialized || !this.db || activeIds.length === 0) return 0;
|
|
2539
|
+
const idSet = new Set(activeIds);
|
|
2540
|
+
const rows = this.db.prepare("SELECT id FROM files").all();
|
|
2541
|
+
let pruned = 0;
|
|
2542
|
+
this.db.exec("BEGIN TRANSACTION;");
|
|
2543
|
+
try {
|
|
2544
|
+
const deleteStmt = this.db.prepare("DELETE FROM files WHERE id = ?");
|
|
2545
|
+
for (const row of rows) {
|
|
2546
|
+
if (!idSet.has(row.id)) {
|
|
2547
|
+
deleteStmt.run(row.id);
|
|
2548
|
+
pruned++;
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
this.db.exec("COMMIT;");
|
|
2552
|
+
return pruned;
|
|
2553
|
+
} catch (e) {
|
|
2554
|
+
this.db.exec("ROLLBACK;");
|
|
2555
|
+
throw e;
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2395
2558
|
clear() {
|
|
2396
2559
|
if (this.fallbackMemory) {
|
|
2397
2560
|
this.fallbackMemory.clear();
|
|
@@ -2522,6 +2685,17 @@ var MemoryMetadataCache = class {
|
|
|
2522
2685
|
deleteFile(id) {
|
|
2523
2686
|
this.files.delete(id);
|
|
2524
2687
|
}
|
|
2688
|
+
pruneMissing(activeIds) {
|
|
2689
|
+
const idSet = new Set(activeIds);
|
|
2690
|
+
let pruned = 0;
|
|
2691
|
+
for (const id of Array.from(this.files.keys())) {
|
|
2692
|
+
if (!idSet.has(id)) {
|
|
2693
|
+
this.files.delete(id);
|
|
2694
|
+
pruned++;
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
return pruned;
|
|
2698
|
+
}
|
|
2525
2699
|
clear() {
|
|
2526
2700
|
this.files.clear();
|
|
2527
2701
|
this.meta.clear();
|
|
@@ -2688,8 +2862,9 @@ var DriveKit = class _DriveKit {
|
|
|
2688
2862
|
* Lists contents of a folder (by folder ID or path)
|
|
2689
2863
|
*/
|
|
2690
2864
|
async ls(folderIdOrPath = "/") {
|
|
2691
|
-
if (folderIdOrPath === "/" || folderIdOrPath === "root") {
|
|
2692
|
-
|
|
2865
|
+
if (folderIdOrPath === "/" || folderIdOrPath === "root" || folderIdOrPath === this.rootFolderId) {
|
|
2866
|
+
const parentFilter = this.rootFolderId === "root" ? "'root' in parents or sharedWithMe = true" : `'${this.rootFolderId}' in parents`;
|
|
2867
|
+
return await this.query().raw(parentFilter).notTrashed().orderBy("folder,name").list();
|
|
2693
2868
|
}
|
|
2694
2869
|
const folderId = await this.resolveId(folderIdOrPath);
|
|
2695
2870
|
return await this.query().inFolder(folderId).notTrashed().orderBy("folder,name").list();
|
|
@@ -2732,7 +2907,7 @@ var DriveKit = class _DriveKit {
|
|
|
2732
2907
|
}
|
|
2733
2908
|
}
|
|
2734
2909
|
if (existingId) {
|
|
2735
|
-
const fileName = targetName ||
|
|
2910
|
+
const fileName = targetName || import_node_path7.default.basename(opts.source);
|
|
2736
2911
|
const raw = await this.getRawClient();
|
|
2737
2912
|
const copyRes = await withRetry(async () => {
|
|
2738
2913
|
return await raw.files.copy({
|
|
@@ -3019,8 +3194,8 @@ var DriveKit = class _DriveKit {
|
|
|
3019
3194
|
const entries = await import_node_fs6.default.promises.readdir(dir, { withFileTypes: true });
|
|
3020
3195
|
const files = [];
|
|
3021
3196
|
for (const entry of entries) {
|
|
3022
|
-
const fullPath =
|
|
3023
|
-
const relPath =
|
|
3197
|
+
const fullPath = import_node_path7.default.join(dir, entry.name);
|
|
3198
|
+
const relPath = import_node_path7.default.join(base, entry.name);
|
|
3024
3199
|
if (entry.isDirectory()) {
|
|
3025
3200
|
const sub = await collectFiles(fullPath, relPath);
|
|
3026
3201
|
files.push(...sub);
|
|
@@ -3038,11 +3213,11 @@ var DriveKit = class _DriveKit {
|
|
|
3038
3213
|
if (remoteDirCache.has(relDir)) {
|
|
3039
3214
|
return remoteDirCache.get(relDir);
|
|
3040
3215
|
}
|
|
3041
|
-
const parts = relDir.split(
|
|
3216
|
+
const parts = relDir.split(import_node_path7.default.sep).filter(Boolean);
|
|
3042
3217
|
let currentParent = targetFolderId;
|
|
3043
3218
|
let currentRel = "";
|
|
3044
3219
|
for (const part of parts) {
|
|
3045
|
-
currentRel = currentRel ?
|
|
3220
|
+
currentRel = currentRel ? import_node_path7.default.join(currentRel, part) : part;
|
|
3046
3221
|
if (remoteDirCache.has(currentRel)) {
|
|
3047
3222
|
currentParent = remoteDirCache.get(currentRel);
|
|
3048
3223
|
continue;
|
|
@@ -3071,8 +3246,8 @@ var DriveKit = class _DriveKit {
|
|
|
3071
3246
|
};
|
|
3072
3247
|
await pool.map(allFiles, async (fileItem) => {
|
|
3073
3248
|
try {
|
|
3074
|
-
const relDir =
|
|
3075
|
-
const fileName =
|
|
3249
|
+
const relDir = import_node_path7.default.dirname(fileItem.relPath);
|
|
3250
|
+
const fileName = import_node_path7.default.basename(fileItem.relPath);
|
|
3076
3251
|
const parentFolderId = await ensureRemoteSubdir(relDir === "." ? "" : relDir);
|
|
3077
3252
|
if (options.onFileStart) {
|
|
3078
3253
|
options.onFileStart(fileItem.fullPath, fileItem.relPath);
|
|
@@ -3208,8 +3383,15 @@ var DriveKit = class _DriveKit {
|
|
|
3208
3383
|
await this.init();
|
|
3209
3384
|
const { PassThrough } = await import("stream");
|
|
3210
3385
|
const passThrough = new PassThrough();
|
|
3211
|
-
|
|
3212
|
-
|
|
3386
|
+
let resolveDone;
|
|
3387
|
+
let rejectDone;
|
|
3388
|
+
const donePromise = new Promise((resolve, reject) => {
|
|
3389
|
+
resolveDone = resolve;
|
|
3390
|
+
rejectDone = reject;
|
|
3391
|
+
});
|
|
3392
|
+
passThrough.done = donePromise;
|
|
3393
|
+
const remoteDir = import_node_path7.default.dirname(remotePath);
|
|
3394
|
+
const fileName = options.name || options.filename || import_node_path7.default.basename(remotePath);
|
|
3213
3395
|
const targetParentId = remoteDir === "." || remoteDir === "/" ? this.rootFolderId : await this.mkdirp(remoteDir);
|
|
3214
3396
|
const mimeType = options.mimeType || "application/octet-stream";
|
|
3215
3397
|
const raw = this.driveClient;
|
|
@@ -3226,9 +3408,10 @@ var DriveKit = class _DriveKit {
|
|
|
3226
3408
|
fields: "id, name, mimeType, size, md5Checksum",
|
|
3227
3409
|
supportsAllDrives: true
|
|
3228
3410
|
}).then((res) => {
|
|
3229
|
-
|
|
3411
|
+
const file = res.data;
|
|
3412
|
+
if (file.id && this.metadataCacheInstance) {
|
|
3230
3413
|
this.metadataCacheInstance.upsertFile({
|
|
3231
|
-
id:
|
|
3414
|
+
id: file.id,
|
|
3232
3415
|
name: fileName,
|
|
3233
3416
|
mimeType,
|
|
3234
3417
|
parentId: targetParentId,
|
|
@@ -3236,8 +3419,10 @@ var DriveKit = class _DriveKit {
|
|
|
3236
3419
|
trashed: false
|
|
3237
3420
|
});
|
|
3238
3421
|
}
|
|
3422
|
+
resolveDone(file);
|
|
3239
3423
|
}).catch((err) => {
|
|
3240
3424
|
passThrough.destroy(err);
|
|
3425
|
+
rejectDone(err);
|
|
3241
3426
|
});
|
|
3242
3427
|
return passThrough;
|
|
3243
3428
|
}
|
|
@@ -3278,6 +3463,7 @@ var DriveKit = class _DriveKit {
|
|
|
3278
3463
|
const client = this.driveClient;
|
|
3279
3464
|
let pageToken = void 0;
|
|
3280
3465
|
let totalScanned = 0;
|
|
3466
|
+
const rawFiles = [];
|
|
3281
3467
|
do {
|
|
3282
3468
|
const res = await withRetry(async () => {
|
|
3283
3469
|
return await client.files.list({
|
|
@@ -3291,21 +3477,49 @@ var DriveKit = class _DriveKit {
|
|
|
3291
3477
|
});
|
|
3292
3478
|
});
|
|
3293
3479
|
const files = res.data.files || [];
|
|
3294
|
-
const
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
size: f.size ? parseInt(f.size, 10) : void 0,
|
|
3299
|
-
parentId: f.parents?.[0],
|
|
3300
|
-
md5Checksum: f.md5Checksum || void 0,
|
|
3301
|
-
trashed: !!f.trashed,
|
|
3302
|
-
modifiedTime: f.modifiedTime || void 0
|
|
3303
|
-
}));
|
|
3304
|
-
this.metadataCacheInstance.upsertFiles(entries);
|
|
3305
|
-
totalScanned += entries.length;
|
|
3480
|
+
for (const f of files) {
|
|
3481
|
+
if (f.id) rawFiles.push(f);
|
|
3482
|
+
}
|
|
3483
|
+
totalScanned += files.length;
|
|
3306
3484
|
options?.onProgress?.(totalScanned);
|
|
3307
3485
|
pageToken = res.data.nextPageToken || void 0;
|
|
3308
3486
|
} while (pageToken);
|
|
3487
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
3488
|
+
for (const f of rawFiles) {
|
|
3489
|
+
fileMap.set(f.id, f);
|
|
3490
|
+
}
|
|
3491
|
+
const pathMemo = /* @__PURE__ */ new Map();
|
|
3492
|
+
const rootId = this.rootFolderId;
|
|
3493
|
+
function computePath(id, visited = /* @__PURE__ */ new Set()) {
|
|
3494
|
+
if (pathMemo.has(id)) return pathMemo.get(id);
|
|
3495
|
+
if (visited.has(id)) return "";
|
|
3496
|
+
visited.add(id);
|
|
3497
|
+
const f = fileMap.get(id);
|
|
3498
|
+
if (!f || !f.name) return "";
|
|
3499
|
+
const parentId = f.parents?.[0];
|
|
3500
|
+
if (!parentId || parentId === "root" || parentId === rootId) {
|
|
3501
|
+
const p = `/${f.name}`;
|
|
3502
|
+
pathMemo.set(id, p);
|
|
3503
|
+
return p;
|
|
3504
|
+
}
|
|
3505
|
+
const parentPath = computePath(parentId, visited);
|
|
3506
|
+
const fullPath = parentPath ? `${parentPath}/${f.name}` : `/${f.name}`;
|
|
3507
|
+
pathMemo.set(id, fullPath);
|
|
3508
|
+
return fullPath;
|
|
3509
|
+
}
|
|
3510
|
+
const entries = rawFiles.map((f) => ({
|
|
3511
|
+
id: f.id,
|
|
3512
|
+
name: f.name || "untitled",
|
|
3513
|
+
mimeType: f.mimeType || "",
|
|
3514
|
+
size: f.size ? parseInt(f.size, 10) : void 0,
|
|
3515
|
+
parentId: f.parents?.[0],
|
|
3516
|
+
path: computePath(f.id),
|
|
3517
|
+
md5Checksum: f.md5Checksum || void 0,
|
|
3518
|
+
trashed: !!f.trashed,
|
|
3519
|
+
modifiedTime: f.modifiedTime || void 0
|
|
3520
|
+
}));
|
|
3521
|
+
this.metadataCacheInstance.upsertFiles(entries);
|
|
3522
|
+
this.metadataCacheInstance.pruneMissing(rawFiles.map((f) => f.id));
|
|
3309
3523
|
this.metadataCacheInstance.setMeta("last_sync_timestamp", (/* @__PURE__ */ new Date()).toISOString());
|
|
3310
3524
|
return this.metadataCacheInstance.getStats();
|
|
3311
3525
|
}
|
|
@@ -3613,6 +3827,7 @@ var index_default = DriveKit;
|
|
|
3613
3827
|
createAutoNamePlugin,
|
|
3614
3828
|
createDrive,
|
|
3615
3829
|
createWebhookPlugin,
|
|
3830
|
+
escapeXml,
|
|
3616
3831
|
formatBytes,
|
|
3617
3832
|
formatDuration,
|
|
3618
3833
|
formatSpeed,
|