Edits history of script submission #14400 for ' Get file thumbnail (box)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Box = {
      token: string;
    };
    /**
     * Get file thumbnail
     * Retrieves a thumbnail, or smaller image representation, of a file.
    
    Sizes of `32x32`,`64x64`, `128x128`, and `256x256` can be returned in
    the `.png` format and sizes of `32x32`, `160x160`, and `320x320`
    can be returned in the `.jpg` format.
    
    Thumbnails can be generated for the image and video file formats listed
    [found on our community site][1].
    
    [1]: https://community.box.com/t5/Migrating-and-Previewing-Content/File-Types-and-Fonts-Supported-in-Box-Content-Preview/ta-p/327
     */
    export async function main(
      auth: Box,
      file_id: string,
      extension: "png" | "jpg",
      min_height: string | undefined,
      min_width: string | undefined,
      max_height: string | undefined,
      max_width: string | undefined,
    ) {
      const url = new URL(
        `https://api.box.com/2.0/files/${file_id}/thumbnail.${extension}`,
      );
      for (const [k, v] of [
        ["min_height", min_height],
        ["min_width", min_width],
        ["max_height", max_height],
        ["max_width", max_width],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago