@digibuffer/file-manager 1.0.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/README.md +213 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +3 -0
- package/dist/client-DTkToHgY.js +347 -0
- package/dist/index-CHmsNSsb.d.ts +1 -0
- package/dist/index-pUAG6PPj.d.ts +132 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/router-CD_RQnlx.d.ts +159 -0
- package/dist/server/adapters/next.d.ts +20 -0
- package/dist/server/adapters/next.js +63 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +3 -0
- package/dist/server-CZrPOP0h.js +490 -0
- package/dist/types-Brk9aR7c.d.ts +177 -0
- package/package.json +70 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { a as FileManagerConfig, c as ManagedFile, d as StorageFile, i as DownloadUrlOptions, l as PaginatedResponse, r as DeleteResult, s as ListFilesOptions, t as BatchDeleteResult } from "./types-Brk9aR7c.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/server/file-manager.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* File Manager for managing files in R2/S3
|
|
7
|
+
*/
|
|
8
|
+
declare class FileManager<TFile extends ManagedFile = ManagedFile> {
|
|
9
|
+
private client;
|
|
10
|
+
private bucketName;
|
|
11
|
+
private database?;
|
|
12
|
+
private defaultLimit;
|
|
13
|
+
private maxLimit;
|
|
14
|
+
constructor(config: FileManagerConfig<TFile>);
|
|
15
|
+
/**
|
|
16
|
+
* List files from storage with pagination
|
|
17
|
+
*/
|
|
18
|
+
listFromStorage(options?: ListFilesOptions): Promise<PaginatedResponse<StorageFile>>;
|
|
19
|
+
/**
|
|
20
|
+
* List files from database with pagination
|
|
21
|
+
*/
|
|
22
|
+
listFromDatabase(options?: ListFilesOptions): Promise<PaginatedResponse<TFile>>;
|
|
23
|
+
/**
|
|
24
|
+
* List files from both database and storage, merging results
|
|
25
|
+
*/
|
|
26
|
+
listFromBoth(options?: ListFilesOptions): Promise<PaginatedResponse<TFile>>;
|
|
27
|
+
/**
|
|
28
|
+
* List files with automatic source selection
|
|
29
|
+
*/
|
|
30
|
+
list(options?: ListFilesOptions): Promise<PaginatedResponse<TFile | StorageFile>>;
|
|
31
|
+
/**
|
|
32
|
+
* Delete a single file
|
|
33
|
+
*/
|
|
34
|
+
delete(key: string): Promise<DeleteResult>;
|
|
35
|
+
/**
|
|
36
|
+
* Delete multiple files in batch
|
|
37
|
+
*/
|
|
38
|
+
deleteBatch(keys: string[]): Promise<BatchDeleteResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Generate a presigned download URL
|
|
41
|
+
*/
|
|
42
|
+
getDownloadUrl(key: string, options?: DownloadUrlOptions): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a file exists
|
|
45
|
+
*/
|
|
46
|
+
exists(key: string): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Get file metadata from storage
|
|
49
|
+
*/
|
|
50
|
+
getMetadata(key: string): Promise<StorageFile | null>;
|
|
51
|
+
/**
|
|
52
|
+
* Build URL helper - supports both path-style and virtual-hosted-style
|
|
53
|
+
*/
|
|
54
|
+
private buildUrl;
|
|
55
|
+
/**
|
|
56
|
+
* Sort files helper
|
|
57
|
+
*/
|
|
58
|
+
private sortFiles;
|
|
59
|
+
/**
|
|
60
|
+
* Create a FileManagerError
|
|
61
|
+
*/
|
|
62
|
+
private createError;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/server/router.d.ts
|
|
66
|
+
declare const FileManagerRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
67
|
+
action: z.ZodLiteral<"list">;
|
|
68
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
69
|
+
cursor: z.ZodOptional<z.ZodString>;
|
|
70
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
71
|
+
delimiter: z.ZodOptional<z.ZodString>;
|
|
72
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
73
|
+
storage: "storage";
|
|
74
|
+
database: "database";
|
|
75
|
+
both: "both";
|
|
76
|
+
}>>;
|
|
77
|
+
sortBy: z.ZodOptional<z.ZodEnum<{
|
|
78
|
+
lastModified: "lastModified";
|
|
79
|
+
size: "size";
|
|
80
|
+
name: "name";
|
|
81
|
+
}>>;
|
|
82
|
+
sortOrder: z.ZodOptional<z.ZodEnum<{
|
|
83
|
+
asc: "asc";
|
|
84
|
+
desc: "desc";
|
|
85
|
+
}>>;
|
|
86
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
87
|
+
action: z.ZodLiteral<"delete">;
|
|
88
|
+
key: z.ZodString;
|
|
89
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
90
|
+
action: z.ZodLiteral<"batchDelete">;
|
|
91
|
+
keys: z.ZodArray<z.ZodString>;
|
|
92
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
93
|
+
action: z.ZodLiteral<"getUrl">;
|
|
94
|
+
key: z.ZodString;
|
|
95
|
+
expiresIn: z.ZodOptional<z.ZodNumber>;
|
|
96
|
+
filename: z.ZodOptional<z.ZodString>;
|
|
97
|
+
disposition: z.ZodOptional<z.ZodEnum<{
|
|
98
|
+
attachment: "attachment";
|
|
99
|
+
inline: "inline";
|
|
100
|
+
}>>;
|
|
101
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
102
|
+
action: z.ZodLiteral<"exists">;
|
|
103
|
+
key: z.ZodString;
|
|
104
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
105
|
+
action: z.ZodLiteral<"getMetadata">;
|
|
106
|
+
key: z.ZodString;
|
|
107
|
+
}, z.core.$strip>], "action">;
|
|
108
|
+
type FileManagerRequest = z.infer<typeof FileManagerRequestSchema>;
|
|
109
|
+
/**
|
|
110
|
+
* Route configuration
|
|
111
|
+
*/
|
|
112
|
+
type RouteConfig<TFile extends ManagedFile = ManagedFile> = {
|
|
113
|
+
fileManager: FileManager<TFile>;
|
|
114
|
+
/**
|
|
115
|
+
* Authorization check - return true to allow, false to deny
|
|
116
|
+
* Throw an error with a message for custom error responses
|
|
117
|
+
*/
|
|
118
|
+
onAuthorize?: (request: FileManagerRequest, context?: any) => Promise<boolean> | boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Before list hook - can modify options or deny access
|
|
121
|
+
*/
|
|
122
|
+
onBeforeList?: (options: ListFilesOptions, context?: any) => Promise<ListFilesOptions | false> | ListFilesOptions | false;
|
|
123
|
+
/**
|
|
124
|
+
* Before delete hook - return false to deny deletion
|
|
125
|
+
*/
|
|
126
|
+
onBeforeDelete?: (key: string, context?: any) => Promise<boolean> | boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Before batch delete hook - can filter keys or deny access
|
|
129
|
+
*/
|
|
130
|
+
onBeforeBatchDelete?: (keys: string[], context?: any) => Promise<string[] | false> | string[] | false;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Router for file management operations
|
|
134
|
+
*/
|
|
135
|
+
declare class FileManagerRouter<TFile extends ManagedFile = ManagedFile> {
|
|
136
|
+
private routes;
|
|
137
|
+
/**
|
|
138
|
+
* Register a route
|
|
139
|
+
*/
|
|
140
|
+
route(path: string, config: RouteConfig<TFile>): this;
|
|
141
|
+
/**
|
|
142
|
+
* Handle a request
|
|
143
|
+
*/
|
|
144
|
+
handle(path: string, body: unknown, context?: any): Promise<Response>;
|
|
145
|
+
/**
|
|
146
|
+
* Success response helper
|
|
147
|
+
*/
|
|
148
|
+
private successResponse;
|
|
149
|
+
/**
|
|
150
|
+
* Error response helper
|
|
151
|
+
*/
|
|
152
|
+
private errorResponse;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Create a file manager router
|
|
156
|
+
*/
|
|
157
|
+
declare function createFileManagerRouter<TFile extends ManagedFile = ManagedFile>(): FileManagerRouter<TFile>;
|
|
158
|
+
//#endregion
|
|
159
|
+
export { FileManager as a, createFileManagerRouter as i, FileManagerRouter as n, RouteConfig as r, FileManagerRequest as t };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { c as ManagedFile } from "../../types-Brk9aR7c.js";
|
|
2
|
+
import { n as FileManagerRouter } from "../../router-CD_RQnlx.js";
|
|
3
|
+
|
|
4
|
+
//#region src/server/adapters/next.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Convert FileManagerRouter to Next.js App Router handler
|
|
8
|
+
*/
|
|
9
|
+
declare function toRouteHandler<TFile extends ManagedFile = ManagedFile>(router: FileManagerRouter<TFile>, options?: {
|
|
10
|
+
/** Route path to handle (default: 'default') */
|
|
11
|
+
path?: string;
|
|
12
|
+
/** Extract context from request (e.g., auth, user info) */
|
|
13
|
+
getContext?: (request: Request) => Promise<any> | any;
|
|
14
|
+
}): (request: Request) => Promise<Response>;
|
|
15
|
+
/**
|
|
16
|
+
* CORS headers helper for Next.js
|
|
17
|
+
*/
|
|
18
|
+
declare function withCors(handler: (request: Request) => Promise<Response>, allowedOrigins?: string[]): (request: Request) => Promise<Response>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { toRouteHandler, withCors };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//#region src/server/adapters/next.ts
|
|
2
|
+
/**
|
|
3
|
+
* Convert FileManagerRouter to Next.js App Router handler
|
|
4
|
+
*/
|
|
5
|
+
function toRouteHandler(router, options = {}) {
|
|
6
|
+
const path = options.path ?? "default";
|
|
7
|
+
return async function handler(request) {
|
|
8
|
+
if (request.method !== "POST") return new Response(JSON.stringify({
|
|
9
|
+
success: false,
|
|
10
|
+
error: { message: "Method not allowed" }
|
|
11
|
+
}), {
|
|
12
|
+
status: 405,
|
|
13
|
+
headers: { "Content-Type": "application/json" }
|
|
14
|
+
});
|
|
15
|
+
try {
|
|
16
|
+
const body = await request.json();
|
|
17
|
+
let context;
|
|
18
|
+
if (options.getContext) context = await options.getContext(request);
|
|
19
|
+
return await router.handle(path, body, context);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return new Response(JSON.stringify({
|
|
22
|
+
success: false,
|
|
23
|
+
error: { message: error instanceof Error ? error.message : "Failed to parse request body" }
|
|
24
|
+
}), {
|
|
25
|
+
status: 400,
|
|
26
|
+
headers: { "Content-Type": "application/json" }
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* CORS headers helper for Next.js
|
|
33
|
+
*/
|
|
34
|
+
function withCors(handler, allowedOrigins = ["*"]) {
|
|
35
|
+
return async function corsHandler(request) {
|
|
36
|
+
const origin = request.headers.get("origin") ?? "";
|
|
37
|
+
const allowed = allowedOrigins.includes("*") || allowedOrigins.includes(origin);
|
|
38
|
+
if (request.method === "OPTIONS") return new Response(null, {
|
|
39
|
+
status: 204,
|
|
40
|
+
headers: {
|
|
41
|
+
"Access-Control-Allow-Origin": allowed ? origin || "*" : "",
|
|
42
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
43
|
+
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
|
44
|
+
"Access-Control-Max-Age": "86400"
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
const response = await handler(request);
|
|
48
|
+
if (allowed) {
|
|
49
|
+
const headers = new Headers(response.headers);
|
|
50
|
+
headers.set("Access-Control-Allow-Origin", origin || "*");
|
|
51
|
+
headers.set("Access-Control-Allow-Credentials", "true");
|
|
52
|
+
return new Response(response.body, {
|
|
53
|
+
status: response.status,
|
|
54
|
+
statusText: response.statusText,
|
|
55
|
+
headers
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return response;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { toRouteHandler, withCors };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { a as FileManagerConfig, c as ManagedFile, d as StorageFile, i as DownloadUrlOptions, l as PaginatedResponse, n as DatabaseCallbacks, o as FileManagerError, r as DeleteResult, s as ListFilesOptions, t as BatchDeleteResult, u as PaginationOptions } from "../types-Brk9aR7c.js";
|
|
2
|
+
import { a as FileManager, i as createFileManagerRouter, n as FileManagerRouter, r as RouteConfig, t as FileManagerRequest } from "../router-CD_RQnlx.js";
|
|
3
|
+
import "../index-CHmsNSsb.js";
|
|
4
|
+
export { BatchDeleteResult, DatabaseCallbacks, DeleteResult, DownloadUrlOptions, FileManager, FileManagerConfig, FileManagerError, FileManagerRequest, FileManagerRouter, ListFilesOptions, ManagedFile, PaginatedResponse, PaginationOptions, RouteConfig, StorageFile, createFileManagerRouter };
|