Scripts

Random scripts

Methods

(async, static) downloadImage(url, file_path) → {Promise}

downloadImage

Parameters:
Name Type Description
url string

Url of image to download

file_path string

Complete path to final filename, for example: ~/home/user/Documents/project/download.png

Returns:
Type
Promise

(static) fluentPromise(promise) → {Object}

Wrap a promise resolution into a {value, status} object.

Used along with Promise.all allows rejected promises without breaking execution of other Promises. To see how to use it along with Promise.all go to https://gist.github.com/Rantelo/84684a7b7babf64183587e61d98945df

Parameters:
Name Type Description
promise Promise

Promise to be wrapped

Returns:
  • {value, status}
Type
Object

(static) getJSONDownloadString(obj) → {string}

Generates the string to use as download link of a object in JSON format

Example
<a href={getJSONDownloadString(object)} download={`${fileName}.json`}>Download JSON</a>
Parameters:
Name Type Description
obj Object

Object that will be downloaded as JSON

Returns:
Type
string

(static) signGoogleMapsURL(url, secret) → {string}

Sign GoogleMapsStaticAPI url Signature generating algorithm: (extracted from https://developers.google.com/maps/documentation/maps-static/get-api-key#client-id)

  1. Generate query with clientID (signature is not added yet)
  2. Strip domain from request ( localhost:3000/ -- /maps/api/static?center=... )
  3. Retrieve private key, originally in Base64 Sign the URL using HMAC-SHA1. First decode the private key. Note that Base64 for URLS replaces + and / with - and _
  4. Encode the resulting binary signature using the modified Base64
  5. Attach the signature to the URL
Parameters:
Name Type Description
url string

Url to be signed

secret srting

Secret

Returns:
Type
string

(static) signS3Object(s3_domain, s3_bucket, s3_object_key, AWSAccessKeyId, AWSSecretAccessKey, secs_to_expire) → {string}

Signature generating algorithm: (extracted from https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth)

  1. Create a string_to_sign containing the http verb, time to expire in seconds since epoch and route to object - for example: "GET\n\n\n1528921484\n/bucketname/object.png"

    Note: use the same amount of line breaks, since they are headers. We are not taking them into consideration right now. Maybe later...

  2. Sign the string_to_sign using HMAC-SHA1 and the AWS secretAccessKey
  3. Encode the result into Base64
  4. Make resulting binary Url-safe
  5. Use this signature as the ?Signature= param in the url

  6. You migh also need to add AWSAccessKeyId= and Expires= params to the url. At the end, the url will look like the following: https://s3.aws.com/bucket/obj.png?AWSAccessKeyId=XXXX&Signature=YYYY&Expires=12345

Parameters:
Name Type Default Description
s3_domain string

For example https://s3-west.aws.com

s3_bucket string

S3 bucket

s3_object_key string

File name with extension

AWSAccessKeyId string

AWS Access Key ID

AWSSecretAccessKey string

AWS Access Key Secret

secs_to_expire number 120

Number of seconds in which signature will expire this param is set to 120secs by default

Returns:
Type
string

(static) uploadToS3(filename, content, permission, AWS_region, AWS_bucket) → {Promise}

Uploads a file S3. In order to use this method you should have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env variables set in the process.env

Parameters:
Name Type Default Description
filename string

Filename of Object to be uploaded. Must contain extension.

content Binary

File content in a Binary string.

permission string private

Object's permission. "private" by default. Available options private | public-read | public-read-write | authenticated-read | aws-exec-read | bucket-owner-read | bucket-owner-full-control

AWS_region string

Bucket's AWS region. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html

AWS_bucket string

AWS Bucket name.

Returns:
Type
Promise

(inner) existsInS3(filename, AWS_region, AWS_bucket) → {Promise}

Checks if a file exists in S3. In order to use this method you should have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env variables set in the process.env

Parameters:
Name Type Description
filename string

Filename of Object to be checked. Must contain extension.

AWS_region string

Bucket's AWS region. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html

AWS_bucket string

AWS Bucket name.

Returns:
Type
Promise