-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathstorage.ts
More file actions
236 lines (227 loc) · 7.45 KB
/
storage.ts
File metadata and controls
236 lines (227 loc) · 7.45 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { GenericId } from "../values/index.js";
/**
* A reference to a file in storage.
*
* This is used in the {@link StorageReader} and {@link StorageWriter} which are accessible in
* Convex queries and mutations via {@link QueryCtx} and {@link MutationCtx} respectively.
*
* @public
*/
export type StorageId = string;
export type FileStorageId = GenericId<"_storage"> | StorageId;
/**
* Metadata for a single file as returned by {@link StorageReader.getMetadata | storage.getMetadata}.
*
* @public
*/
export type FileMetadata = {
/**
* ID for referencing the file (eg. via {@link StorageReader.getUrl | storage.getUrl})
*/
storageId: StorageId;
/**
* Hex encoded sha256 checksum of file contents
*/
sha256: string;
/**
* Size of the file in bytes
*/
size: number;
/**
* ContentType of the file if it was provided on upload
*/
contentType: string | null;
};
/**
* An interface to read files from storage within Convex query functions.
*
* Available as `ctx.storage` in queries (read-only), mutations, and actions.
*
* @example
* ```typescript
* // Get a URL for a stored file:
* const url = await ctx.storage.getUrl(storageId);
* if (url) {
* // Use the URL (e.g., return it to the client)
* }
*
* // Get file metadata via the system table (preferred over deprecated getMetadata):
* const metadata = await ctx.db.system.get("_storage", storageId);
* // metadata: { _id, _creationTime, sha256, size, contentType? }
* ```
*
* @see https://docs.convex.dev/file-storage
* @public
*/
export interface StorageReader {
/**
* Get the URL for a file in storage by its `Id<"_storage">`.
*
* The GET response includes a standard HTTP Digest header with a sha256 checksum.
*
* @example
* ```typescript
* const url = await ctx.storage.getUrl(storageId);
* ```
*
* @param storageId - The `Id<"_storage">` of the file to fetch from Convex storage.
* @returns - A URL which fetches the file via an HTTP GET, or `null` if the file no longer exists.
*/
getUrl(storageId: GenericId<"_storage">): Promise<string | null>;
/**
* @deprecated Passing a string is deprecated, use `storage.getUrl(Id<"_storage">)` instead.
*
* Get the URL for a file in storage by its {@link StorageId}.
*
* The GET response includes a standard HTTP Digest header with a sha256 checksum.
*
* @param storageId - The {@link StorageId} of the file to fetch from Convex storage.
* @returns - A url which fetches the file via an HTTP GET, or `null` if it no longer exists.
*/
getUrl<T extends StorageId>(
storageId: T extends { __tableName: any } ? never : T,
): Promise<string | null>;
/**
* @deprecated Use `ctx.db.system.get("_storage", storageId)` instead, which returns
* equivalent metadata from the `_storage` system table (with a slightly different shape):
* ```typescript
* const metadata = await ctx.db.system.get("_storage", storageId);
* // { _id, _creationTime, sha256, size, contentType? }
* ```
*
* Get metadata for a file.
*
* @param storageId - The `Id<"_storage">` of the file.
* @returns - A {@link FileMetadata} object if found or `null` if not found.
*/
getMetadata(storageId: GenericId<"_storage">): Promise<FileMetadata | null>;
/**
* @deprecated Use `ctx.db.system.get("_storage", storageId)` instead.
*
* Get metadata for a file.
*
* @param storageId - The {@link StorageId} of the file.
* @returns - A {@link FileMetadata} object if found or `null` if not found.
*/
getMetadata<T extends StorageId>(
storageId: T extends { __tableName: any } ? never : T,
): Promise<FileMetadata | null>;
}
/**
* An interface to write files to storage within Convex mutation functions.
*
* Available as `ctx.storage` in mutations.
*
* @see https://docs.convex.dev/file-storage
* @public
*/
export interface StorageWriter extends StorageReader {
/**
* Generate a short-lived URL for uploading a file into storage.
*
* The client should make a POST request to this URL with the file as the
* body. The response will be a JSON object containing a newly allocated
* `Id<"_storage">` (`{ storageId: "..." }`).
*
* @example
* ```typescript
* // In a mutation, generate the upload URL:
* export const generateUploadUrl = mutation({
* args: {},
* returns: v.string(),
* handler: async (ctx) => {
* return await ctx.storage.generateUploadUrl();
* },
* });
*
* // On the client, upload the file:
* // const uploadUrl = await generateUploadUrl();
* // const result = await fetch(uploadUrl, { method: "POST", body: file });
* // const { storageId } = await result.json();
* ```
*
* @returns - A short-lived URL for uploading a file via HTTP POST.
*/
generateUploadUrl(): Promise<string>;
/**
* Delete a file from Convex storage.
*
* Once a file is deleted, any URLs previously generated by {@link StorageReader.getUrl} will return 404s.
*
* @example
* ```typescript
* await ctx.storage.delete(storageId);
* ```
*
* @param storageId - The `Id<"_storage">` of the file to delete from Convex storage.
*/
delete(storageId: GenericId<"_storage">): Promise<void>;
/**
* @deprecated Passing a string is deprecated, use `storage.delete(Id<"_storage">)` instead.
*
* Delete a file from Convex storage.
*
* Once a file is deleted, any URLs previously generated by {@link StorageReader.getUrl} will return 404s.
*
* @param storageId - The {@link StorageId} of the file to delete from Convex storage.
*/
delete<T extends StorageId>(
storageId: T extends { __tableName: any } ? never : T,
): Promise<void>;
}
/**
* An interface to read and write files to storage within Convex actions and HTTP actions.
*
* In actions, `ctx.storage` has additional methods not available in mutations:
* `get()` to download a file as a Blob, and `store()` to upload a Blob directly.
*
* @example
* ```typescript
* // In an action, download and re-upload a file:
* const blob = await ctx.storage.get(storageId);
* if (blob) {
* const newStorageId = await ctx.storage.store(blob);
* }
* ```
*
* @public
*/
export interface StorageActionWriter extends StorageWriter {
/**
* Download a file from storage as a Blob.
*
* Only available in actions and HTTP actions (not in mutations or queries).
*
* @param storageId - The `Id<"_storage">` of the file.
* @returns A Blob containing the file contents, or `null` if the file doesn't exist.
*/
get(storageId: GenericId<"_storage">): Promise<Blob | null>;
/**
* @deprecated Passing a string is deprecated, use `storage.get(Id<"_storage">)` instead.
*
* Get a Blob containing the file associated with the provided {@link StorageId}, or `null` if there is no file.
*/
get<T extends StorageId>(
storageId: T extends { __tableName: any } ? never : T,
): Promise<Blob | null>;
/**
* Upload a Blob directly to storage.
*
* Only available in actions and HTTP actions. For client-side uploads from
* mutations, use `generateUploadUrl()` instead.
*
* @example
* ```typescript
* const storageId = await ctx.storage.store(blob);
* // Save storageId to the database via ctx.runMutation
* ```
*
* @param blob - The Blob to store.
* @param options - Optional settings. Pass `sha256` to verify the file integrity.
* @returns The `Id<"_storage">` of the newly stored file.
*/
store(
blob: Blob,
options?: { sha256?: string },
): Promise<GenericId<"_storage">>;
}