PageSourceSearch

https://bello.gov.co/SiteAssets/v1/assets/components/home/slider-enlaces-interes/api/files/upload.js

js bello.gov.co collected 2026-09-25 04:16:04 UTC 2,107 bytes, 77 lines download raw bytes

1import { API_BASE, SITE, LIST } from "../../config.js";
2
3/**
4 * Sube un archivo a la lista/biblioteca y retorna el JSON normalizado de tu API.
5 * Normaliza la respuesta para siempre exponer .FileRef (si el backend envía "url", la mapeamos).
6 *
7 * Ejemplos de respuestas soportadas:
8 * { status: "ok", url: "/ruta/archivo.png", id: 20, fileName: "archivo.png" }
9 * { status: "ok", FileRef: "/ruta/archivo.png", ... }
10 * { status: "ok", data: { url: "/ruta/archivo.png" } }
11 *
12 * @param {File} file
13 * @returns {Promise<object>} JSON con al menos { status, FileRef? }
14 */
15export async function uploadFile(file) {
16  const formdata = new FormData();
17  // Importante: tu backend espera la clave vacía "", mantener así
18  formdata.append("", file);
19
20  let resp;
21  try {
22    resp = await fetch(
23      `${API_BASE}?ref=upload-file&site=${encodeURIComponent(
24        SITE
25      )}&list=${encodeURIComponent(LIST)}&dev=false`,
26      { method: "POST", body: formdata }
27    );
28  } catch (e) {
29    return {
30      status: "error",
31      message: e?.message || "Fallo de red al subir el archivo",
32    };
33  }
34
35  const text = await resp.text();
36
37  // Si el servidor no respondió 2xx, devolvemos error informativo
38  if (!resp.ok) {
39    return {
40      status: "error",
41      message: `HTTP ${resp.status} - ${
42        resp.statusText || "Error al subir el archivo"
43      }`,
44      raw: text,
45    };
46  }
47
48  // Intentar parsear JSON
49  let json;
50  try {
51    json = JSON.parse(text);
52  } catch {
53    return {
54      status: "error",
55      message: text || "Respuesta inválida del servidor",
56    };
57  }
58
59  // ===== Normalización de FileRef =====
60  // Aceptamos varias formas y consolidamos en .FileRef
61  const fileRef =
62    json?.FileRef ??
63    json?.fileRef ??
64    json?.url ??
65    json?.data?.FileRef ??
66    json?.data?.fileRef ??
67    json?.data?.url ??
68    null;
69
70  if (fileRef) {
71    json.FileRef = fileRef;
72    // De paso, si no viene "url", lo igualamos para mayor compatibilidad
73    if (!json.url) json.url = fileRef;
74  }
75
76  return json;
77}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.