import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files
 * @returns Object with error_count and array of results (files URLs or error messages)
 *
 * Detects file type/extension using base64 header (e.g. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA...").
 * The output of File Input component can be used as parameter for this function.
 */
type S3 = {
  endPoint: string;
  port: number;
  useSSL: boolean;
  pathStyle: boolean;
  bucket: string;
  accessKey: string;
  secretKey: string;
  region: string;
};
export async function main(s3: S3, files: string[]) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: any[] = [];
  let error_count = 0;
  await Promise.all(
    files.map(async (file: string) => {
      try {
        const mime_data = base64MimeType(file)!;
        if (mime_data) {
          const file_name =
            format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
            "-" +
            Math.random().toString(36).substring(2, 8) +
            "." +
            mime_data.extension;
          const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
          const bytes = Uint8Array.from(atob(base64Data), (c) =>
            c.charCodeAt(0),
          );
          await s3client.putObject(file_name, bytes, {
            metadata: {
              "Content-Type": mime_data.mimetype,
            },
          });
          const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
          out.push({
            success: true,
            url,
          });
        } else {
          out.push({
            success: false,
            message: MESSAGES.error_mimetype,
          });
          error_count++;
        }
      } catch (error) {
        out.push({
          success: false,
          message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
        });
        error_count++;
      }
    }),
  );
  return { error_count, files: out };
}
 Submitted by hugo697 181 days ago
import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files
 * @returns Object with error_count and array of results (files URLs or error messages)
 *
 * Detects file type/extension using base64 header (e.g. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA...").
 * The output of File Input component can be used as parameter for this function.
 */
type S3 = {
  endPoint: string;
  port: number;
  useSSL: boolean;
  pathStyle: boolean;
  bucket: string;
  accessKey: string;
  secretKey: string;
  region: string;
};
export async function main(s3: S3, files: string[]) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: any[] = [];
  let error_count = 0;
  await Promise.all(
    files.map(async (file: string) => {
      try {
        const mime_data = base64MimeType(file)!;
        if (mime_data) {
          const file_name =
            format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
            "-" +
            Math.random().toString(36).substring(2, 8) +
            "." +
            mime_data.extension;
          const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
          const bytes = Uint8Array.from(atob(base64Data), (c) =>
            c.charCodeAt(0),
          );
          await s3client.putObject(file_name, bytes, {
            metadata: {
              "Content-Type": mime_data.mimetype,
            },
          });
          const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
          out.push({
            success: true,
            url,
          });
        } else {
          out.push({
            success: false,
            message: MESSAGES.error_mimetype,
          });
          error_count++;
        }
      } catch (error) {
        out.push({
          success: false,
          message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
        });
        error_count++;
      }
    }),
  );
  return { error_count, files: out };
}
 Submitted by admin 814 days ago
import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files
 * @returns Object with error_count and array of results (files URLs or error messages)
 * 
 * Detects file type/extension using base64 header (e.g. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA..."). 
 * The output of File Input component can be used as parameter for this function.
 */
type S3 = {
  endPoint: string;
  port: number;
  useSSL: boolean;
  pathStyle: boolean;
  bucket: string;
  accessKey: string;
  secretKey: string;
  region: string;
};
export async function main(
  s3: S3,
  files: string[],
) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: (any)[] = [];
  let error_count = 0;
  await Promise.all(files.map(async (file: string) => {
    try {
      const mime_data = base64MimeType(file)!;
      if (mime_data) {
        const file_name = format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
          "-" + Math.random().toString(36).substring(2, 8) + "." +
          mime_data.extension;
        const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
        const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0));
        await s3client.putObject(file_name, bytes, {
          metadata: {
            "Content-Type": mime_data.mimetype,
          },
        });
        const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
        out.push({
          success: true,
          url,
        });
      } else {
        out.push({
          success: false,
          message: MESSAGES.error_mimetype,
        });
        error_count++;
      }
    } catch (error) {
      out.push({
        success: false,
        message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
      });
      error_count++;
    }
  }));
  return { error_count, files: out };
}
 Submitted by admin 817 days ago
import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { Resource } from "https://deno.land/x/windmill@v1.85.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files
 * @returns Object with error_count and array of results (files URLs or error messages)
 * 
 * Detects file type/extension using base64 header (e.g. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA..."). 
 * The output of File Input component can be used as parameter for this function.
 */
export async function main(
  s3: Resource<"s3">,
  files: string[],
) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: (any)[] = [];
  let error_count = 0;
  await Promise.all(files.map(async (file: string) => {
    try {
      const mime_data = base64MimeType(file)!;
      if (mime_data) {
        const file_name = format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
          "-" + Math.random().toString(36).substring(2, 8) + "." +
          mime_data.extension;
        const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
        const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0));
        await s3client.putObject(file_name, bytes, {
          metadata: {
            "Content-Type": mime_data.mimetype,
          },
        });
        const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
        out.push({
          success: true,
          url,
        });
      } else {
        out.push({
          success: false,
          message: MESSAGES.error_mimetype,
        });
        error_count++;
      }
    } catch (error) {
      out.push({
        success: false,
        message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
      });
      error_count++;
    }
  }));
  return { error_count, files: out };
}
 Submitted by adam186 945 days ago
import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { Resource } from "https://deno.land/x/windmill@v1.82.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files
 * @returns Object with error_count and array of results (files URLs or error messages)
 * 
 * Detects file type/extension using base64 header (e.g. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA..."). 
 * The output of File Input component can be used as parameter for this function.
 */
export async function main(
  s3: Resource<"s3">,
  files: string[],
) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: (any)[] = [];
  let error_count = 0;
  await Promise.all(files.map(async (file: string) => {
    try {
      const mime_data = base64MimeType(file)!;
      if (mime_data) {
        const file_name = format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
          "-" + Math.random().toString(36).substring(2, 8) + "." +
          mime_data.extension;
        const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
        const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0));
        await s3client.putObject(file_name, bytes, {
          metadata: {
            "Content-Type": mime_data.mimetype,
          },
        });
        const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
        out.push({
          success: true,
          url,
        });
      } else {
        out.push({
          success: false,
          message: MESSAGES.error_mimetype,
        });
        error_count++;
      }
    } catch (error) {
      out.push({
        success: false,
        message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
      });
      error_count++;
    }
  }));
  return { error_count, files: out };
}
 Submitted by vidz657 956 days ago
import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { Resource } from "https://deno.land/x/windmill@v1.82.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files
 * @returns Object with error_count and array of results (files URLs or error messages)
 * 
 * Detects file type/extension using base64 header (e.g. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA..."). The output of File Input component can be used as parameter for this function.
 */
export async function main(
  s3: Resource<"s3">,
  files: string[],
) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: (any)[] = [];
  let error_count = 0;
  await Promise.all(files.map(async (file: string) => {
    try {
      const mime_data = base64MimeType(file)!;
      if (mime_data) {
        const file_name = format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
          "-" + Math.random().toString(36).substring(2, 8) + "." +
          mime_data.extension;
        const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
        const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0));
        await s3client.putObject(file_name, bytes, {
          metadata: {
            "Content-Type": mime_data.mimetype,
          },
        });
        const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
        out.push({
          success: true,
          url,
        });
      } else {
        out.push({
          success: false,
          message: MESSAGES.error_mimetype,
        });
        error_count++;
      }
    } catch (error) {
      out.push({
        success: false,
        message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
      });
      error_count++;
    }
  }));
  return { error_count, files: out };
}
 Submitted by vidz657 956 days ago
import { S3Client } from "https://deno.land/x/s3_lite_client@0.5.0/mod.ts";
import { Resource } from "https://deno.land/x/windmill@v1.82.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param s3 S3 resource
 * @param files Array of base64 encoded files, like the output of File Input component.
 *              (e.g. ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA...)
 * @returns Object with error_count and array of results (files URLs or error messages)
 */
export async function main(
  s3: Resource<"s3">,
  files: string[],
) {
  if (files.length <= 0) {
    return;
  }
  const s3client = new S3Client(s3);
  const out: (any)[] = [];
  let error_count = 0;
  await Promise.all(files.map(async (file: string) => {
    try {
      const mime_data = base64MimeType(file)!;
      if (mime_data) {
        const file_name = format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
          "-" + Math.random().toString(36).substring(2, 8) + "." +
          mime_data.extension;
        const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
        const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0));
        await s3client.putObject(file_name, bytes, {
          metadata: {
            "Content-Type": mime_data.mimetype,
          },
        });
        const url = `https://${s3["endPoint"]}/${s3["bucket"]}/${file_name}`;
        out.push({
          success: true,
          url,
        });
      } else {
        out.push({
          success: false,
          message: MESSAGES.error_mimetype,
        });
        error_count++;
      }
    } catch (error) {
      out.push({
        success: false,
        message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
      });
      error_count++;
    }
  }));
  return { error_count, files: out };
}
 Submitted by vidz657 956 days ago
import { S3 } from "https://deno.land/x/s3@0.5.0/mod.ts";
import * as wmill from "https://deno.land/x/windmill@v1.81.0/mod.ts";
import { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
const MESSAGES_I9 = {
  en: {
    error_mimetype: "File type could not be identified",
    error_file_upload: "Error uploading file (S3)",
  },
  pt_br: {
    error_mimetype: "Tipo do arquivo não pôde ser identificado",
    error_file_upload: "Erro ao enviar arquivo (S3)",
  },
};
const MESSAGES = MESSAGES_I9.en;
/****
 * Returns the mimetype and extension of a base64 encoded file
 * @param encoded Base64 encoded file
 * @returns Object with mimetype and extension
 */
function base64MimeType(encoded: string) {
  let result = null;
  if (typeof encoded !== "string") {
    return result;
  }
  const match = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
  if (match && match.length) {
    result = {
      mimetype: match[1],
      extension: mime.getExtension(match[1]),
    };
  }
  return result;
}
/****
 * Uploads a list of files to S3
 * @param bucket S3 bucket name
 * @param files Array of base64 encoded files, like the output of File Input component.
 *              (e.g. ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAA...)
 * @returns Object with error_count and array of results (files URLs or error messages)
 *
 * The variable f/utils/aws_cred must be set with the AWS credentials
 */
export async function main(
  bucket: string,
  files: string[],
) {
  if (files.length <= 0) {
    return;
  }
  /*
  // Example for variable f/utils/aws_cred:
  {
    "accessKeyID": "XXXXXX",
    "secretKey": "xxxxxx",
    "region": "us-east-1",
    "endpointURL": "https://s3.us-east-1.amazonaws.com"
  }
  */
  const cred = JSON.parse((await wmill.getVariable("f/utils/aws_cred"))!);
  const s3 = new S3(cred);
  const s3bucket = s3.getBucket(bucket);
  const out: any[] = [];
  let error_count = 0;
  await Promise.all(files.map(async (file: string) => {
    try {
      const mime_data = base64MimeType(file)!;
      if (mime_data) {
        const file_name = format(new Date(), "yyyy-MM-dd-HH-mm-ss") +
          "-" + Math.random().toString(36).substring(2, 8) + "." +
          mime_data.extension;
        const base64Data = file.replace(/^data:\w+\/\w+;base64,/, "");
        const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0));
        await s3bucket.putObject(file_name, bytes, {
          contentType: mime_data.mimetype,
        });
        const url = `${cred["endpointURL"]}/${bucket}/${file_name}`;
        out.push({
          success: true,
          url,
        });
      } else {
        out.push({
          success: false,
          message: MESSAGES.error_mimetype,
        });
        error_count++;
      }
    } catch (error) {
      out.push({
        success: false,
        message: `${MESSAGES.error_file_upload} (S3): ${error.message}`,
      });
      error_count++;
    }
  }));
  return { error_count, files: out };
}
 Submitted by vidz657 956 days ago