File size: 1,928 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export type UploadContent =
  | Buffer
  | Blob
  | File
  | ArrayBuffer
  | ArrayBufferView
  | ReadableStream<Uint8Array>
  | NodeJS.ReadableStream;

export interface FileMetadata {
  key: string;
  filename: string;
  contentType: string;
  size: number;
  uploadedAt?: Date;
}

export interface UploadOptions {
  filename?: string;
  contentType?: string;
}

export interface UploadResult {
  key: string;
  sourceUrl: string; // Public URL that anyone can access
  metadata: FileMetadata;
}

export interface UploadUrlOptions {
  filename: string;
  contentType: string;
  expiresInSeconds?: number;
}

export type UploadUrlMethod = "PUT" | "POST";

export interface UploadUrl {
  key: string;
  url: string;
  method: UploadUrlMethod;
  expiresAt: Date;
  headers?: Record<string, string>;
  fields?: Record<string, string>;
}

export interface FileStorage {
  /** Upload file content directly from the server (e.g. AI generated image). */
  upload(
    content: UploadContent,
    options?: UploadOptions,
  ): Promise<UploadResult>;

  /**
   * Create a short-lived public upload target for clients (e.g. presigned URL).
   * Return null if the backend does not support client-side uploads.
   */
  createUploadUrl?(options: UploadUrlOptions): Promise<UploadUrl | null>;

  /** Retrieve file bytes in server environment. */
  download(key: string): Promise<Buffer>;

  /** Delete the file from storage. */
  delete(key: string): Promise<void>;

  /** Check existence without downloading. */
  exists(key: string): Promise<boolean>;

  /** Fetch stored metadata. */
  getMetadata(key: string): Promise<FileMetadata | null>;

  /** Public URL used by clients to read the file (same as UploadResult.sourceUrl). */
  getSourceUrl(key: string): Promise<string | null>;

  /** Optional convenience for providing a forced download URL when supported by the backend. */
  getDownloadUrl?(key: string): Promise<string | null>;
}