Miss any of our Open RFC calls?Watch the recordings here! »

@holofy/utils

1.0.12 • Public • Published

holofy-utils

About

Utility functions

  • @holofy/utils provides the following utility functions:

  • { s3: { uploadBase64: uploadBase64(content, folder, bucket), uploadMedia: uploadMedia(req, folder, bucket), uploadImage: uploadImage(path, folder, bucket), } }

Usage

Install the package:

npm install @holofy/utils --save-prod

Import as necessary:

const Utils = require('@holofy/utils');
const utils = Utils(config.utils);

To test on dev

Add .env to project root folder

AWS_S3_ACCESS_KEY=
AWS_S3_SECRET=
AWS_S3_REGION=
AWS_S3_BUCKET=

uploadBase64

Add /sandbox/upload-base64-index.js to project root folder

// eslint-disable-next-line import/no-extraneous-dependencies
require('dotenv').config();
const fs = require('fs');
 
const Utils = require('../index');
 
const config = {
  s3: {
    awsS3AccessKey: process.env.AWS_S3_ACCESS_KEY,
    awsS3Secret: process.env.AWS_S3_SECRET,
    awsS3Region: process.env.AWS_S3_REGION,
    awsS3Bucket: process.env.AWS_S3_BUCKET,
  },
};
 
const utils = Utils(config);
 
const content = fs.readFileSync('./sandbox/image.txt', 'utf8');
 
Promise.resolve()
  .then(() => utils.s3.uploadBase64(content, 'spaces', config.s3.awsS3Bucket))
  .then((key) => console.log(`Upload ${key} to ${config.s3.awsS3Bucket} completed`));

Add /sandbox/image.txt to project root folder

image.txt should contain a base64 encoded image string data

On terminal

npm i && node sandbox/upload-base64-index.js

uploadMedia TODO: update

Add /sandbox/upload-media-index.js to project root folder

// eslint-disable-next-line import/no-extraneous-dependencies
require('dotenv').config();
const http = require('http');
const Utils = require('../index');
 
const PORT = process.env.PORT || 8080;
 
const config = {
  s3: {
    awsS3AccessKey: process.env.AWS_S3_ACCESS_KEY,
    awsS3Secret: process.env.AWS_S3_SECRET,
    awsS3Region: process.env.AWS_S3_REGION,
    awsS3Bucket: process.env.AWS_S3_BUCKET,
  },
};
 
const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.writeHead(200, { 'content-type': 'text/html' });
    res.end(
      '<form action="/upload" enctype="multipart/form-data" method="post">'
        + '<input type="text" name="title"><br>'
        + '<input type="file" name="file" multiple="multiple"><br>'
        + '<input type="submit" value="Upload">'
        + '</form>',
    );
  } else if (req.url === '/upload') {
    const utils = Utils(config);
 
    Promise.resolve()
      .then(() => utils.s3.uploadMedia(req, 'assets/video', config.s3.awsS3Bucket))
      .then((result) => res.end(`Upload ${JSON.stringify(result)} to ${config.s3.awsS3Bucket} completed`));
  } else {
    res.writeHead(404, { 'content-type': 'text/plain' });
    res.end('404');
  }
});
server.listen(PORT, () => {
  console.info(`listening on http://localhost:${PORT}/`);
});

On terminal

npm i && node sandbox/upload-media-index.js

uploadImage

Add /sandbox/upload-image-index.js to project root folder

// eslint-disable-next-line import/no-extraneous-dependencies
require('dotenv').config();
 
const Utils = require('../index');
 
const config = {
  s3: {
    awsS3AccessKey: process.env.AWS_S3_ACCESS_KEY,
    awsS3Secret: process.env.AWS_S3_SECRET,
    awsS3Region: process.env.AWS_S3_REGION,
    awsS3Bucket: process.env.AWS_S3_BUCKET,
  },
};
 
const utils = Utils(config);
 
Promise.resolve()
  .then(() => utils.s3.uploadImage('./screenshot-1.jpg', 'assets/video/images', config.s3.awsS3Bucket))
  .then((key) => console.log(`Upload ${key} to ${config.s3.awsS3Bucket} completed`));

On terminal

npm i && node sandbox/upload-image-index.js

uploadFile

Add /sandbox/upload-file-index.js to project root folder

// eslint-disable-next-line import/no-extraneous-dependencies
require('dotenv').config();
 
const Utils = require('../index');
 
const config = {
  s3: {
    awsS3AccessKey: process.env.AWS_S3_ACCESS_KEY,
    awsS3Secret: process.env.AWS_S3_SECRET,
    awsS3Region: process.env.AWS_S3_REGION,
    awsS3Bucket: process.env.AWS_S3_BUCKET,
  },
};
 
const utils = Utils(config);
 
Promise.resolve()
  .then(() => utils.s3.uploadFile('./sandbox/index.html', 'accountWidget', config.s3.awsS3Bucket))
  .then((key) => console.log(`Upload ${key} to ${config.s3.awsS3Bucket} completed`));

On terminal

npm i && node sandbox/upload-file-index.js

Please do not commit sandbox/* and .env

Publish New Package

  • Only once: In this repository: Add NPM_AUTH_TOKEN to GitLab -> Settings -> CI/CD -> Settings -> Variables
git add .
git commit
npm version [major | minor | patch]

Install

npm i @holofy/utils

DownloadsWeekly Downloads

56

Version

1.0.12

License

MIT

Unpacked Size

9.35 kB

Total Files

11

Last publish

Collaborators

  • avatar