async uploadFile()

in slingpost/index.js [281:320]


  async uploadFile(file, path, params, fileName) {
    this.log.info(`Uploading files: ${file}`);
    const files = glob.sync(file);
    for (let idx in files) {
      const f = files[idx];
      const p = path || this.repositoryPath(mod_path.dirname(f));
      const fn = fileName || mod_path.basename(f);
      this.log.debug(`Uploading file: ${f} to ${p}`);
      const formData = new FormData();
      if (params) {
        this.log.silly(`Sending parameters: ${JSON.stringify(params)}`);
        Object.keys(params).forEach((key) => {
          formData.append(key, params[key]);
        });
      }
      formData.append("*", fs.createReadStream(f), { filename: fn });
      const response = await fetch(`${this.config.url}${p}`, {
        method: "POST",
        headers: {
          Authorization: `Basic ${Buffer.from(
            this.config.username + ":" + this.config.password
          ).toString("base64")}`,
          Accept: "application/json",
        },
        body: formData,
      });
      if (!response.ok) {
        throw new Error(
          `Failed with invalid status: ${response.status} - ${response.statusText}`
        );
      } else {
        this.log.debug(
          `Post successful: ${response.status} - ${response.statusText}`
        );
        const body = await response.text();
        this.log.silly(`Retrieved response: ${body}`);
      }
    }
    this.log.info(`Successfully uploaded: ${files.length} files`);
  }