Edits history of script submission #15756 for ' Creates a multipart external upload (discourse)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Discourse = {
      apiKey: string;
      defaultHost: string;
      apiUsername: string;
    };
    /**
     * Creates a multipart external upload
     * Creates a multipart upload in the external storage provider, storing
    a temporary reference to the external upload similar to /get-presigned-put.
    
    You must have the correct permissions and CORS settings configured in your
    external provider. We support AWS S3 as the default. See:
    
    https://meta.discourse.org/t/-/210469#s3-multipart-direct-uploads-4.
    
    An external file store must be set up and `enable_direct_s3_uploads` must
    be set to true for this endpoint to function.
    
    
     */
    export async function main(
      auth: Discourse,
      body: {
        upload_type:
          | "avatar"
          | "profile_background"
          | "card_background"
          | "custom_emoji"
          | "composer";
        file_name: string;
        file_size: number;
        metadata?: { "sha1-checksum"?: string };
      },
    ) {
      const url = new URL(`https://${auth.defaultHost}/uploads/create-multipart.json`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "API-KEY": auth.apiKey,
          "API-USERNAME": auth.apiUsername,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago