File size: 13,704 Bytes
1d8be9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { Client, handle_file } from "@gradio/client";
import type {
  EnhancementResponse,
  EnhancementSettings,
  ImageMetadata,
} from "@/types/enhancement";
import { HF_INFERENCE_SPACE, REQUIRES_HF_SIGN_IN } from "@/lib/deployment";

const API_BASE_URL = (process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000").replace(/\/+$/, "");

const ERROR_MESSAGES: Record<string, string> = {
  upload_too_large: "This image is larger than the 20 MB upload limit.",
  auth_required: "Sign in with Hugging Face before enhancing images.",
  gpu_quota_exceeded: "Your Hugging Face GPU time is used up for now. Try again after your quota resets.",
  queue_unavailable: "The shared AI queue is full right now. Please try again shortly.",
  resource_unavailable: "The AI service could not prepare this image. Try a smaller input.",
  unauthorized: "Your Hugging Face sign-in expired. Sign in again, then retry this image.",
  unsupported_media_type: "Choose a JPG, PNG, or WEBP image.",
  invalid_image: "This image is unsupported or exceeds the AI service's image limits. Try a supported still image with fewer pixels.",
  invalid_request: "Choose an upscale factor and output format, then try again.",
  model_not_ready: "The AI service is starting up. Please try again in a moment.",
  gpu_out_of_memory: "This image is too large for the available GPU memory. Try a smaller image.",
  enhancement_failed: "The AI service could not finish this image. Try a smaller image.",
  inference_failed: "The AI service could not enhance this image. Please try again.",
  encoding_failed: "The enhanced image could not be prepared for download.",
  internal_error: "Something went wrong while preparing the enhanced image.",
};

export type EnhancementProgress = {
  stage: "connecting" | "queued" | "working";
  position?: number;
  etaSeconds?: number;
};

export type EnhancementProgressCallback = (progress: EnhancementProgress) => void;

function uploadBlob(file: File): Blob {
  if (file.type.trim()) return file;

  const extension = file.name.split(".").pop()?.toLowerCase();
  const inferredType = extension === "jpg" || extension === "jpeg"
    ? "image/jpeg"
    : extension === "png"
      ? "image/png"
      : extension === "webp"
        ? "image/webp"
        : "application/octet-stream";
  return file.slice(0, file.size, inferredType);
}

export class EnhancementApiError extends Error {
  constructor(
    message: string,
    readonly status: number,
    readonly code?: string,
  ) {
    super(message);
    this.name = "EnhancementApiError";
  }
}

function readHeaderInteger(headers: Headers, name: string, fallback: number): number {
  const value = Number(headers.get(name));
  return Number.isSafeInteger(value) && value > 0 ? value : fallback;
}

function readFilename(headers: Headers, fallback: string): string {
  const value = headers.get("content-disposition")?.match(/filename="([^"]+)"/i)?.[1];
  if (!value) return fallback;
  const safeName = value.replace(/[^A-Za-z0-9._-]/g, "").slice(0, 120);
  return safeName || fallback;
}

async function responseError(response: Response): Promise<EnhancementApiError> {
  let code = "";
  try {
    const payload: unknown = await response.json();
    if (typeof payload === "object" && payload !== null && "error" in payload) {
      const error = payload.error;
      if (typeof error === "object" && error !== null && "code" in error) {
        code = typeof error.code === "string" ? error.code : "";
      }
    }
  } catch {
    // Use the safe status-based message when the backend did not return JSON.
  }

  const message = ERROR_MESSAGES[code] ?? (response.status === 413
    ? ERROR_MESSAGES.upload_too_large
    : response.status >= 500
      ? "The AI service is unavailable right now. Please try again shortly."
      : "The image could not be processed. Check the file and try again.");
  return new EnhancementApiError(message, response.status, code || undefined);
}

function hostedSpaceOrigin(): string {
  if (/^https?:\/\//i.test(HF_INFERENCE_SPACE)) {
    return new URL(HF_INFERENCE_SPACE).origin;
  }
  const [owner, space] = HF_INFERENCE_SPACE.split("/");
  if (!owner || !space) {
    throw new EnhancementApiError(
      "The hosted AI service is not configured correctly.",
      0,
      "invalid_configuration",
    );
  }
  return `https://${owner}-${space}.hf.space`;
}

function asRecord(value: unknown): Record<string, unknown> | null {
  return typeof value === "object" && value !== null
    ? value as Record<string, unknown>
    : null;
}

async function hostedOutputBlob(value: unknown, contentType: string): Promise<Blob> {
  if (value instanceof Blob) return new Blob([value], { type: contentType });
  const fileData = asRecord(value);
  if (fileData?.blob instanceof Blob) {
    return new Blob([fileData.blob], { type: contentType });
  }

  const candidate = typeof value === "string"
    ? value
    : [fileData?.data, fileData?.url, fileData?.path]
      .find((item): item is string => typeof item === "string" && item.length > 0);
  if (typeof candidate !== "string") {
    throw new EnhancementApiError(
      "The service did not return a downloadable image.",
      502,
      "invalid_response",
    );
  }

  let url: URL;
  try {
    url = candidate.startsWith("data:") || candidate.startsWith("blob:")
      ? new URL(candidate)
      : new URL(candidate, `${hostedSpaceOrigin()}/`);
  } catch {
    throw new EnhancementApiError(
      "The service returned an invalid image reference.",
      502,
      "invalid_response",
    );
  }
  if (url.protocol === "data:" || url.protocol === "blob:") {
    const inlineResponse = await fetch(url);
    if (!inlineResponse.ok) {
      throw new EnhancementApiError(
        "The generated image could not be read.",
        inlineResponse.status,
        "invalid_response",
      );
    }
    return new Blob([await inlineResponse.blob()], { type: contentType });
  }
  if (url.origin !== hostedSpaceOrigin() || url.protocol !== "https:") {
    throw new EnhancementApiError(
      "The service returned an unexpected image location.",
      502,
      "invalid_response",
    );
  }
  const response = await fetch(url, { credentials: "omit" });
  if (!response.ok) {
    throw new EnhancementApiError(
      response.status === 401 || response.status === 403
        ? ERROR_MESSAGES.unauthorized
        : "The generated image could not be downloaded.",
      response.status,
      response.status === 401 || response.status === 403
        ? "unauthorized"
        : "invalid_response",
    );
  }
  return new Blob([await response.blob()], { type: contentType });
}

function metadataInteger(
  metadata: Record<string, unknown>,
  name: string,
  fallback: number,
): number {
  const value = metadata[name];
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0
    ? value
    : fallback;
}

function hostedFailure(error: unknown): EnhancementApiError {
  if (error instanceof EnhancementApiError) return error;
  const message = error instanceof Error ? error.message : "";
  if (/oauth|sign.?in|unauthori[sz]ed|401|expired token/i.test(message)) {
    return new EnhancementApiError(ERROR_MESSAGES.unauthorized, 401, "auth_required");
  }
  if (/quota|over.?quota|daily limit/i.test(message)) {
    return new EnhancementApiError(ERROR_MESSAGES.gpu_quota_exceeded, 429, "gpu_quota_exceeded");
  }
  if (/queue.*(full|limit)|queue is full/i.test(message)) {
    return new EnhancementApiError(ERROR_MESSAGES.queue_unavailable, 503, "queue_full");
  }
  if (/out of memory/i.test(message)) {
    return new EnhancementApiError(ERROR_MESSAGES.gpu_out_of_memory, 503, "gpu_out_of_memory");
  }
  return new EnhancementApiError(
    "The AI service is unavailable right now. Please try again shortly.",
    503,
    "inference_failed",
  );
}

async function enhanceWithHostedSpace(
  file: File,
  settings: EnhancementSettings,
  original: { width: number; height: number },
  accessToken: string | null | undefined,
  onProgress?: EnhancementProgressCallback,
): Promise<EnhancementResponse> {
  if (!accessToken || !accessToken.startsWith("hf_")) {
    throw new EnhancementApiError(ERROR_MESSAGES.auth_required, 401, "auth_required");
  }

  onProgress?.({ stage: "connecting" });
  try {
    const client = await Client.connect(HF_INFERENCE_SPACE, {
      token: accessToken as `hf_${string}`,
      oauth_token: accessToken,
      events: ["data", "status"],
      credentials: "omit",
      record_history: false,
    });
    const submission = client.submit("/enhance", [
      handle_file(uploadBlob(file)),
      file.name || "pasted-image.png",
      String(settings.scale),
      settings.outputFormat,
    ]);
    let resultData: unknown[] | null = null;
    let queueError = "";
    for await (const event of submission) {
      if (event.type === "status") {
        if (event.stage === "error") {
          queueError = typeof event.message === "string" ? event.message : "";
        } else if (event.stage === "pending") {
          onProgress?.({
            stage: "queued",
            position: event.position,
            etaSeconds: event.eta,
          });
        } else if (event.stage === "generating") {
          onProgress?.({ stage: "working" });
        }
      } else if (event.type === "data") {
        resultData = Array.isArray(event.data) ? event.data : null;
      }
    }

    if (queueError) throw hostedFailure(new Error(queueError));
    if (!resultData || resultData.length < 2) {
      throw new EnhancementApiError(
        "The service returned an incomplete enhancement result.",
        502,
        "invalid_response",
      );
    }

    const metadata = asRecord(resultData[1]);
    const errorPayload = asRecord(metadata?.error);
    if (errorPayload) {
      const code = typeof errorPayload.code === "string" ? errorPayload.code : "inference_failed";
      throw new EnhancementApiError(
        ERROR_MESSAGES[code] ?? "The image could not be processed. Check the file and try again.",
        code === "auth_required" ? 401 : 422,
        code,
      );
    }
    if (!metadata) {
      throw new EnhancementApiError(
        "The service returned invalid image metadata.",
        502,
        "invalid_response",
      );
    }

    const allowedContentTypes = new Set(["image/png", "image/jpeg", "image/webp"]);
    const declaredContentType = metadata.content_type;
    const contentType = typeof declaredContentType === "string"
      && allowedContentTypes.has(declaredContentType)
      ? declaredContentType
      : `image/${settings.outputFormat === "jpeg" ? "jpeg" : settings.outputFormat}`;
    const blob = await hostedOutputBlob(resultData[0], contentType);
    const filename = typeof metadata.filename === "string"
      && /^[A-Za-z0-9._-]{1,120}$/.test(metadata.filename)
      ? metadata.filename
      : `${file.name.replace(/\.[^.]+$/, "") || "image"}-enhanced-${settings.scale}x.${settings.outputFormat}`;

    const result: ImageMetadata = {
      originalWidth: metadataInteger(metadata, "original_width", original.width),
      originalHeight: metadataInteger(metadata, "original_height", original.height),
      enhancedWidth: metadataInteger(metadata, "enhanced_width", original.width * settings.scale),
      enhancedHeight: metadataInteger(metadata, "enhanced_height", original.height * settings.scale),
      scale: settings.scale,
      model: typeof metadata.model === "string" ? metadata.model : "Real-ESRGAN",
      filename,
    };
    return { blob, metadata: result };
  } catch (error) {
    throw hostedFailure(error);
  }
}

export async function enhanceImage(
  file: File,
  settings: EnhancementSettings,
  original: { width: number; height: number },
  accessToken?: string | null,
  onProgress?: EnhancementProgressCallback,
): Promise<EnhancementResponse> {
  if (REQUIRES_HF_SIGN_IN) {
    return enhanceWithHostedSpace(file, settings, original, accessToken, onProgress);
  }

  const formData = new FormData();
  formData.append("file", uploadBlob(file), file.name || "pasted-image.png");
  formData.append("scale", String(settings.scale));
  formData.append("output_format", settings.outputFormat);

  let response: Response;
  try {
    response = await fetch(`${API_BASE_URL}/api/enhance`, {
      method: "POST",
      body: formData,
    });
  } catch {
    throw new EnhancementApiError(
      "Could not reach the AI service. Check that the backend is running and try again.",
      0,
      "network_error",
    );
  }

  if (!response.ok) throw await responseError(response);

  const contentType = response.headers.get("content-type")?.split(";", 1)[0].trim();
  if (!contentType?.startsWith("image/")) {
    throw new EnhancementApiError(
      "The service returned an unexpected result. Please try again.",
      response.status,
      "invalid_response",
    );
  }

  const blob = await response.blob();
  const metadata: ImageMetadata = {
    originalWidth: readHeaderInteger(response.headers, "x-original-width", original.width),
    originalHeight: readHeaderInteger(response.headers, "x-original-height", original.height),
    enhancedWidth: readHeaderInteger(
      response.headers,
      "x-enhanced-width",
      original.width * settings.scale,
    ),
    enhancedHeight: readHeaderInteger(
      response.headers,
      "x-enhanced-height",
      original.height * settings.scale,
    ),
    scale: settings.scale,
    model: response.headers.get("x-model") || "Real-ESRGAN",
    filename: readFilename(
      response.headers,
      `${file.name.replace(/\.[^.]+$/, "") || "image"}-enhanced-${settings.scale}x.${settings.outputFormat}`,
    ),
  };

  return { blob, metadata };
}