alepha 0.10.5 → 0.10.7

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/api/files.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import { BucketDescriptor } from "alepha/bucket";
2
2
  import * as _alepha_core1 from "alepha";
3
3
  import { Alepha, FileLike, Static } from "alepha";
4
+ import "alepha/server/security";
4
5
  import * as _alepha_postgres50 from "alepha/postgres";
5
6
  import { Page } from "alepha/postgres";
7
+ import * as _alepha_server0 from "alepha/server";
6
8
  import { Ok } from "alepha/server";
7
9
  import { DateTimeProvider, DurationLike } from "alepha/datetime";
8
10
  import * as _alepha_logger0 from "alepha/logger";
@@ -68,6 +70,11 @@ declare const fileQuerySchema: typebox68.TObject<{
68
70
  sort: typebox68.TOptional<typebox68.TString>;
69
71
  bucket: typebox68.TOptional<typebox68.TString>;
70
72
  tags: typebox68.TOptional<typebox68.TArray<typebox68.TString>>;
73
+ name: typebox68.TOptional<typebox68.TString>;
74
+ mimeType: typebox68.TOptional<typebox68.TString>;
75
+ creator: typebox68.TOptional<typebox68.TString>;
76
+ createdAfter: typebox68.TOptional<typebox68.TString>;
77
+ createdBefore: typebox68.TOptional<typebox68.TString>;
71
78
  }>;
72
79
  type FileQuery = Static<typeof fileQuerySchema>;
73
80
  //#endregion
@@ -91,6 +98,33 @@ declare const fileResourceSchema: typebox68.TObject<{
91
98
  }>;
92
99
  type FileResource = Static<typeof fileResourceSchema>;
93
100
  //#endregion
101
+ //#region src/schemas/storageStatsSchema.d.ts
102
+ declare const bucketStatsSchema: typebox68.TObject<{
103
+ bucket: typebox68.TString;
104
+ totalSize: typebox68.TNumber;
105
+ fileCount: typebox68.TNumber;
106
+ }>;
107
+ declare const mimeTypeStatsSchema: typebox68.TObject<{
108
+ mimeType: typebox68.TString;
109
+ fileCount: typebox68.TNumber;
110
+ }>;
111
+ declare const storageStatsSchema: typebox68.TObject<{
112
+ totalSize: typebox68.TNumber;
113
+ totalFiles: typebox68.TNumber;
114
+ byBucket: typebox68.TArray<typebox68.TObject<{
115
+ bucket: typebox68.TString;
116
+ totalSize: typebox68.TNumber;
117
+ fileCount: typebox68.TNumber;
118
+ }>>;
119
+ byMimeType: typebox68.TArray<typebox68.TObject<{
120
+ mimeType: typebox68.TString;
121
+ fileCount: typebox68.TNumber;
122
+ }>>;
123
+ }>;
124
+ type BucketStats = Static<typeof bucketStatsSchema>;
125
+ type MimeTypeStats = Static<typeof mimeTypeStatsSchema>;
126
+ type StorageStats = Static<typeof storageStatsSchema>;
127
+ //#endregion
94
128
  //#region src/services/FileService.d.ts
95
129
  declare class FileService {
96
130
  protected readonly alepha: Alepha;
@@ -148,22 +182,289 @@ declare class FileService {
148
182
  protected readonly defaultBucket: BucketDescriptor;
149
183
  protected onUploadFile: _alepha_core1.HookDescriptor<"bucket:file:uploaded">;
150
184
  protected onDeleteBucketFile: _alepha_core1.HookDescriptor<"bucket:file:deleted">;
185
+ /**
186
+ * Calculates SHA-256 checksum of a file.
187
+ *
188
+ * @param file - The file to calculate checksum for
189
+ * @returns Hexadecimal string representation of the SHA-256 hash
190
+ * @protected
191
+ */
192
+ protected calculateChecksum(file: FileLike): Promise<string>;
193
+ /**
194
+ * Gets a bucket descriptor by name.
195
+ *
196
+ * @param bucketName - The name of the bucket to retrieve (defaults to "default")
197
+ * @returns The bucket descriptor
198
+ * @throws {NotFoundError} If the bucket is not found
199
+ */
151
200
  bucket(bucketName?: string): BucketDescriptor;
201
+ /**
202
+ * Finds files matching the given query criteria with pagination support.
203
+ * Supports filtering by bucket, tags, name, mimeType, creator, and date range.
204
+ *
205
+ * @param q - Query parameters including bucket, tags, name, mimeType, creator, date range, pagination, and sorting
206
+ * @returns Paginated list of file entities
207
+ */
152
208
  findFiles(q?: FileQuery): Promise<Page<FileEntity>>;
209
+ /**
210
+ * Finds files that have expired based on their expiration date.
211
+ * Limited to 1000 files per call to prevent memory issues.
212
+ *
213
+ * @returns Array of expired file entities
214
+ */
153
215
  findExpiredFiles(): Promise<FileEntity[]>;
216
+ /**
217
+ * Calculates an expiration date based on a TTL (time to live) duration.
218
+ *
219
+ * @param ttl - Duration like "1 day", "2 hours", etc.
220
+ * @returns ISO string representation of the expiration date, or undefined if no TTL provided
221
+ * @protected
222
+ */
154
223
  protected getExpirationDate(ttl?: DurationLike): string | undefined;
224
+ /**
225
+ * Uploads a file to a bucket and creates a database record with metadata.
226
+ * Automatically calculates and stores the file checksum (SHA-256).
227
+ *
228
+ * @param file - The file to upload
229
+ * @param options - Upload options including bucket, expiration, user, and tags
230
+ * @param options.bucket - Target bucket name (defaults to "default")
231
+ * @param options.expirationDate - When the file should expire
232
+ * @param options.user - User performing the upload (for audit trail)
233
+ * @param options.tags - Tags to associate with the file
234
+ * @returns The created file entity with all metadata
235
+ * @throws {NotFoundError} If the specified bucket doesn't exist
236
+ */
155
237
  uploadFile(file: FileLike, options?: {
156
238
  expirationDate?: string | Date;
157
239
  bucket?: string;
158
240
  user?: UserAccountToken;
159
241
  tags?: string[];
160
242
  }): Promise<FileEntity>;
243
+ /**
244
+ * Streams a file from storage by its database ID.
245
+ *
246
+ * @param id - The database ID (UUID) of the file to stream
247
+ * @returns The file object ready for streaming/downloading
248
+ * @throws {NotFoundError} If the file doesn't exist in the database
249
+ * @throws {FileNotFoundError} If the file exists in database but not in storage
250
+ */
161
251
  streamFile(id: string): Promise<FileLike>;
252
+ /**
253
+ * Updates file metadata (name, tags, expiration date).
254
+ * Does not modify the actual file content in storage.
255
+ *
256
+ * @param id - The database ID (UUID) of the file to update
257
+ * @param data - Partial file data to update
258
+ * @param data.name - New file name
259
+ * @param data.tags - New tags array
260
+ * @param data.expirationDate - New expiration date
261
+ * @returns The updated file entity
262
+ * @throws {NotFoundError} If the file doesn't exist in the database
263
+ */
264
+ updateFile(id: string, data: {
265
+ name?: string;
266
+ tags?: string[];
267
+ expirationDate?: string | Date;
268
+ }): Promise<FileEntity>;
269
+ /**
270
+ * Deletes a file from both storage and database.
271
+ * Handles cases where file is already deleted from storage gracefully.
272
+ * Always ensures database record is removed even if storage deletion fails.
273
+ *
274
+ * @param id - The database ID (UUID) of the file to delete
275
+ * @returns Success response with the deleted file ID
276
+ * @throws {NotFoundError} If the file doesn't exist in the database
277
+ */
162
278
  deleteFile(id: string): Promise<Ok>;
279
+ /**
280
+ * Retrieves a file entity by its ID.
281
+ * If already an entity object, returns it as-is (convenience method).
282
+ *
283
+ * @param id - Either a UUID string or an existing FileEntity object
284
+ * @returns The file entity
285
+ * @throws {NotFoundError} If the file doesn't exist in the database
286
+ */
163
287
  getFileById(id: string | FileEntity): Promise<FileEntity>;
288
+ /**
289
+ * Gets storage statistics including total size, file count, and breakdowns by bucket and MIME type.
290
+ *
291
+ * @returns Storage statistics with aggregated data
292
+ */
293
+ getStorageStats(): Promise<StorageStats>;
294
+ /**
295
+ * Converts a file entity to a file resource (API response format).
296
+ * Currently a pass-through, but allows for future transformation logic.
297
+ *
298
+ * @param entity - The file entity to convert
299
+ * @returns The file resource for API responses
300
+ */
164
301
  entityToResource(entity: FileEntity): FileResource;
165
302
  }
166
303
  //#endregion
304
+ //#region src/controllers/FileController.d.ts
305
+ /**
306
+ * REST API controller for file management operations.
307
+ * Provides endpoints for uploading, downloading, listing, and deleting files.
308
+ */
309
+ declare class FileController {
310
+ protected readonly url = "/files";
311
+ protected readonly group = "files";
312
+ protected readonly fileService: FileService;
313
+ /**
314
+ * GET /files - Lists files with optional filtering and pagination.
315
+ * Supports filtering by bucket and tags.
316
+ */
317
+ readonly findFiles: _alepha_server0.ActionDescriptorFn<{
318
+ query: typebox68.TObject<{
319
+ page: typebox68.TOptional<typebox68.TInteger>;
320
+ size: typebox68.TOptional<typebox68.TInteger>;
321
+ sort: typebox68.TOptional<typebox68.TString>;
322
+ bucket: typebox68.TOptional<typebox68.TString>;
323
+ tags: typebox68.TOptional<typebox68.TArray<typebox68.TString>>;
324
+ name: typebox68.TOptional<typebox68.TString>;
325
+ mimeType: typebox68.TOptional<typebox68.TString>;
326
+ creator: typebox68.TOptional<typebox68.TString>;
327
+ createdAfter: typebox68.TOptional<typebox68.TString>;
328
+ createdBefore: typebox68.TOptional<typebox68.TString>;
329
+ }>;
330
+ response: _alepha_postgres50.TPage<typebox68.TObject<{
331
+ id: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_PRIMARY_KEY>, typeof _alepha_postgres50.PG_DEFAULT>;
332
+ version: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TInteger, typeof _alepha_postgres50.PG_VERSION>, typeof _alepha_postgres50.PG_DEFAULT>;
333
+ createdAt: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_CREATED_AT>, typeof _alepha_postgres50.PG_DEFAULT>;
334
+ updatedAt: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_UPDATED_AT>, typeof _alepha_postgres50.PG_DEFAULT>;
335
+ blobId: typebox68.TString;
336
+ creator: typebox68.TOptional<typebox68.TString>;
337
+ creatorRealm: typebox68.TOptional<typebox68.TString>;
338
+ creatorName: typebox68.TOptional<typebox68.TString>;
339
+ bucket: typebox68.TString;
340
+ expirationDate: typebox68.TOptional<typebox68.TString>;
341
+ name: typebox68.TString;
342
+ size: typebox68.TNumber;
343
+ mimeType: typebox68.TString;
344
+ tags: typebox68.TOptional<typebox68.TArray<typebox68.TString>>;
345
+ checksum: typebox68.TOptional<typebox68.TString>;
346
+ }>>;
347
+ }>;
348
+ /**
349
+ * DELETE /files/:id - Deletes a file from both storage and database.
350
+ * Removes the file from the bucket and cleans up the database record.
351
+ */
352
+ readonly deleteFile: _alepha_server0.ActionDescriptorFn<{
353
+ params: typebox68.TObject<{
354
+ id: typebox68.TString;
355
+ }>;
356
+ response: typebox68.TObject<{
357
+ ok: typebox68.TBoolean;
358
+ id: typebox68.TOptional<typebox68.TUnion<[typebox68.TString, typebox68.TInteger]>>;
359
+ count: typebox68.TOptional<typebox68.TNumber>;
360
+ }>;
361
+ }>;
362
+ /**
363
+ * POST /files - Uploads a new file to storage.
364
+ * Creates a database record with metadata and calculates checksum.
365
+ * Optionally specify bucket and expiration date.
366
+ */
367
+ readonly uploadFile: _alepha_server0.ActionDescriptorFn<{
368
+ body: typebox68.TObject<{
369
+ file: _alepha_core1.TFile;
370
+ }>;
371
+ query: typebox68.TObject<{
372
+ expirationDate: typebox68.TOptional<typebox68.TString>;
373
+ bucket: typebox68.TOptional<typebox68.TString>;
374
+ }>;
375
+ response: typebox68.TObject<{
376
+ id: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_PRIMARY_KEY>, typeof _alepha_postgres50.PG_DEFAULT>;
377
+ version: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TInteger, typeof _alepha_postgres50.PG_VERSION>, typeof _alepha_postgres50.PG_DEFAULT>;
378
+ createdAt: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_CREATED_AT>, typeof _alepha_postgres50.PG_DEFAULT>;
379
+ updatedAt: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_UPDATED_AT>, typeof _alepha_postgres50.PG_DEFAULT>;
380
+ blobId: typebox68.TString;
381
+ creator: typebox68.TOptional<typebox68.TString>;
382
+ creatorRealm: typebox68.TOptional<typebox68.TString>;
383
+ creatorName: typebox68.TOptional<typebox68.TString>;
384
+ bucket: typebox68.TString;
385
+ expirationDate: typebox68.TOptional<typebox68.TString>;
386
+ name: typebox68.TString;
387
+ size: typebox68.TNumber;
388
+ mimeType: typebox68.TString;
389
+ tags: typebox68.TOptional<typebox68.TArray<typebox68.TString>>;
390
+ checksum: typebox68.TOptional<typebox68.TString>;
391
+ }>;
392
+ }>;
393
+ /**
394
+ * PATCH /files/:id - Updates file metadata.
395
+ * Allows updating name, tags, and expiration date without modifying file content.
396
+ */
397
+ readonly updateFile: _alepha_server0.ActionDescriptorFn<{
398
+ params: typebox68.TObject<{
399
+ id: typebox68.TString;
400
+ }>;
401
+ body: typebox68.TObject<{
402
+ name: typebox68.TOptional<typebox68.TString>;
403
+ tags: typebox68.TOptional<typebox68.TArray<typebox68.TString>>;
404
+ expirationDate: typebox68.TOptional<typebox68.TString>;
405
+ }>;
406
+ response: typebox68.TObject<{
407
+ id: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_PRIMARY_KEY>, typeof _alepha_postgres50.PG_DEFAULT>;
408
+ version: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TInteger, typeof _alepha_postgres50.PG_VERSION>, typeof _alepha_postgres50.PG_DEFAULT>;
409
+ createdAt: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_CREATED_AT>, typeof _alepha_postgres50.PG_DEFAULT>;
410
+ updatedAt: _alepha_postgres50.PgAttr<_alepha_postgres50.PgAttr<typebox68.TString, typeof _alepha_postgres50.PG_UPDATED_AT>, typeof _alepha_postgres50.PG_DEFAULT>;
411
+ blobId: typebox68.TString;
412
+ creator: typebox68.TOptional<typebox68.TString>;
413
+ creatorRealm: typebox68.TOptional<typebox68.TString>;
414
+ creatorName: typebox68.TOptional<typebox68.TString>;
415
+ bucket: typebox68.TString;
416
+ expirationDate: typebox68.TOptional<typebox68.TString>;
417
+ name: typebox68.TString;
418
+ size: typebox68.TNumber;
419
+ mimeType: typebox68.TString;
420
+ tags: typebox68.TOptional<typebox68.TArray<typebox68.TString>>;
421
+ checksum: typebox68.TOptional<typebox68.TString>;
422
+ }>;
423
+ }>;
424
+ /**
425
+ * GET /files/:id - Streams/downloads a file by its ID.
426
+ * Returns the file content with appropriate Content-Type header.
427
+ * Cached with ETag support for 1 year (immutable).
428
+ */
429
+ readonly streamFile: _alepha_server0.ActionDescriptorFn<{
430
+ params: typebox68.TObject<{
431
+ id: typebox68.TString;
432
+ }>;
433
+ response: _alepha_core1.TFile;
434
+ }>;
435
+ }
436
+ //#endregion
437
+ //#region src/controllers/StorageStatsController.d.ts
438
+ /**
439
+ * REST API controller for storage analytics and statistics.
440
+ * Provides endpoints for viewing storage usage metrics.
441
+ */
442
+ declare class StorageStatsController {
443
+ protected readonly url = "/files/stats";
444
+ protected readonly group = "files";
445
+ protected readonly fileService: FileService;
446
+ /**
447
+ * GET /files/stats - Gets storage statistics.
448
+ * Returns aggregated data including total size, file count,
449
+ * and breakdowns by bucket and MIME type.
450
+ */
451
+ readonly getStats: _alepha_server0.ActionDescriptorFn<{
452
+ response: typebox68.TObject<{
453
+ totalSize: typebox68.TNumber;
454
+ totalFiles: typebox68.TNumber;
455
+ byBucket: typebox68.TArray<typebox68.TObject<{
456
+ bucket: typebox68.TString;
457
+ totalSize: typebox68.TNumber;
458
+ fileCount: typebox68.TNumber;
459
+ }>>;
460
+ byMimeType: typebox68.TArray<typebox68.TObject<{
461
+ mimeType: typebox68.TString;
462
+ fileCount: typebox68.TNumber;
463
+ }>>;
464
+ }>;
465
+ }>;
466
+ }
467
+ //#endregion
167
468
  //#region src/index.d.ts
168
469
  declare module "alepha/bucket" {
169
470
  interface BucketFileOptions {
@@ -197,5 +498,5 @@ declare module "alepha/bucket" {
197
498
  */
198
499
  declare const AlephaApiFiles: _alepha_core1.Service<_alepha_core1.Module<{}>>;
199
500
  //#endregion
200
- export { AlephaApiFiles, FileEntity, FileService, files };
501
+ export { AlephaApiFiles, BucketStats, FileController, FileEntity, FileService, MimeTypeStats, StorageStats, StorageStatsController, bucketStatsSchema, files, mimeTypeStatsSchema, storageStatsSchema };
201
502
  //# sourceMappingURL=index.d.ts.map