@muhgholy/next-drive 3.2.1 → 3.2.2

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/README.md CHANGED
@@ -339,6 +339,87 @@ await ffmpeg(path).format("mp4").save("output.mp4");
339
339
 
340
340
  > Google Drive files are automatically downloaded to local cache.
341
341
 
342
+ ### List Files and Folders
343
+
344
+ List files and folders in a directory:
345
+
346
+ ```typescript
347
+ import { driveList } from "@muhgholy/next-drive/server";
348
+
349
+ // List root folder
350
+ const items = await driveList({ key: { userId: "123" } });
351
+
352
+ // List specific folder
353
+ const items = await driveList({
354
+ key: { userId: "123" },
355
+ folderId: "folderIdHere",
356
+ limit: 50,
357
+ });
358
+
359
+ // Pagination
360
+ const items = await driveList({
361
+ key: { userId: "123" },
362
+ folderId: "root",
363
+ limit: 20,
364
+ afterId: "lastItemId",
365
+ });
366
+ ```
367
+
368
+ **Options:**
369
+
370
+ | Option | Type | Required | Description |
371
+ | ----------- | ------------------------- | -------- | ---------------------------------------------- |
372
+ | `key` | `Record<string, unknown>` | Yes | Owner key (must match authenticated user) |
373
+ | `folderId` | `string \| null` | No | Folder ID to list (null or 'root' for root) |
374
+ | `accountId` | `string` | No | Storage account ID ('LOCAL' for local storage) |
375
+ | `limit` | `number` | No | Maximum items to return (default: 100) |
376
+ | `afterId` | `string` | No | Last item ID for pagination |
377
+
378
+ ### Delete File or Folder
379
+
380
+ Permanently delete a file or folder from the drive system:
381
+
382
+ ```typescript
383
+ import { driveDelete } from "@muhgholy/next-drive/server";
384
+
385
+ // Delete a file
386
+ await driveDelete("694f5013226de007be94fcc0");
387
+
388
+ // Delete a folder recursively (default behavior)
389
+ await driveDelete(folderId, { recurse: true });
390
+
391
+ // Delete only if folder is empty
392
+ try {
393
+ await driveDelete(folderId, { recurse: false });
394
+ } catch (error) {
395
+ // Throws error if folder contains items
396
+ console.error("Cannot delete non-empty folder");
397
+ }
398
+
399
+ // Delete using database document
400
+ const drive = await Drive.findById(fileId);
401
+ await driveDelete(drive);
402
+
403
+ // Delete using TDatabaseDrive object
404
+ const items = await driveList({ key: { userId: "123" } });
405
+ await driveDelete(items[0]);
406
+ ```
407
+
408
+ **Parameters:**
409
+
410
+ | Parameter | Type | Description |
411
+ | --------- | ---------------------------------------------------- | -------------------------------------- |
412
+ | `source` | `string \| IDatabaseDriveDocument \| TDatabaseDrive` | File/folder ID or object to delete |
413
+ | `options` | `{ recurse?: boolean }` | Delete options (default: recurse=true) |
414
+
415
+ **Options:**
416
+
417
+ | Option | Type | Default | Description |
418
+ | --------- | --------- | ------- | ----------------------------------------------------------------------------------------- |
419
+ | `recurse` | `boolean` | `true` | If true, deletes folder and all children. If false, throws error if folder contains items |
420
+
421
+ > **Note:** This permanently deletes the file/folder. For soft deletion (trash), use the `trash` API action instead.
422
+
342
423
  ---
343
424
 
344
425
  ## Configuration Options
@@ -946,6 +946,65 @@ var driveFilePath = async (file) => {
946
946
  }
947
947
  throw new Error(`Unsupported provider: ${providerType}`);
948
948
  };
949
+ var driveList = async (options) => {
950
+ const { key, folderId, accountId, limit = 100, afterId } = options;
951
+ let providerName = "LOCAL";
952
+ if (accountId && accountId !== "LOCAL") {
953
+ const account = await drive_default.db.model("StorageAccount").findOne({ _id: accountId, owner: key });
954
+ if (!account) {
955
+ throw new Error("Invalid Storage Account");
956
+ }
957
+ if (account.metadata.provider === "GOOGLE") {
958
+ providerName = "GOOGLE";
959
+ }
960
+ }
961
+ const query = {
962
+ owner: key,
963
+ "provider.type": providerName,
964
+ storageAccountId: accountId || null,
965
+ parentId: folderId === "root" || !folderId ? null : folderId,
966
+ trashedAt: null
967
+ };
968
+ if (afterId) {
969
+ query._id = { $lt: afterId };
970
+ }
971
+ const items = await drive_default.find(query, {}, { sort: { order: 1, _id: -1 }, limit });
972
+ return await Promise.all(items.map((item) => item.toClient()));
973
+ };
974
+ var driveDelete = async (source, options) => {
975
+ const { recurse = true } = options || {};
976
+ let drive;
977
+ let driveId;
978
+ if (typeof source === "string") {
979
+ const doc = await drive_default.findById(source);
980
+ if (!doc) throw new Error(`File not found: ${source}`);
981
+ drive = doc;
982
+ driveId = source;
983
+ } else if ("toClient" in source) {
984
+ drive = source;
985
+ driveId = String(drive._id);
986
+ } else {
987
+ const doc = await drive_default.findById(source.id);
988
+ if (!doc) throw new Error(`File not found: ${source.id}`);
989
+ drive = doc;
990
+ driveId = source.id;
991
+ }
992
+ if (drive.information.type === "FOLDER" && !recurse) {
993
+ const owner2 = drive.owner;
994
+ const childCount = await drive_default.countDocuments({
995
+ owner: owner2,
996
+ parentId: driveId,
997
+ trashedAt: null
998
+ });
999
+ if (childCount > 0) {
1000
+ throw new Error(`Cannot delete folder: it contains ${childCount} item(s). Use recurse: true to delete folder and all its contents.`);
1001
+ }
1002
+ }
1003
+ const provider = drive.provider?.type === "GOOGLE" ? GoogleDriveProvider : LocalStorageProvider;
1004
+ const accountId = drive.storageAccountId?.toString();
1005
+ const owner = drive.owner;
1006
+ await provider.delete([driveId], owner, accountId);
1007
+ };
949
1008
  var driveUpload = async (source, key, options) => {
950
1009
  const config = getDriveConfig();
951
1010
  let provider = LocalStorageProvider;
@@ -1657,6 +1716,6 @@ var driveAPIHandler = async (req, res) => {
1657
1716
  }
1658
1717
  };
1659
1718
 
1660
- export { driveAPIHandler, driveConfiguration, driveFilePath, driveFileSchemaZod, driveGetUrl, driveReadFile, driveUpload, getDriveConfig, getDriveInformation };
1661
- //# sourceMappingURL=chunk-F5ZVJGYN.js.map
1662
- //# sourceMappingURL=chunk-F5ZVJGYN.js.map
1719
+ export { driveAPIHandler, driveConfiguration, driveDelete, driveFilePath, driveFileSchemaZod, driveGetUrl, driveList, driveReadFile, driveUpload, getDriveConfig, getDriveInformation };
1720
+ //# sourceMappingURL=chunk-WSOFHT7J.js.map
1721
+ //# sourceMappingURL=chunk-WSOFHT7J.js.map