11import Minio = require( 'minio' )
22import v4 = require( 'uuid/v4' )
3+ import axios from 'axios'
34import config = require( '../../config' )
45
56const client = new Minio . Client ( {
@@ -17,11 +18,31 @@ export type savedFile = {
1718
1819export const urlForFilename = ( bucket : string , filename : string ) : string => `http${ config . S3 . ssl ? 's' : '' } ://${ config . S3 . endpoint } /${ bucket } /${ filename } `
1920
21+ /**
22+ * Uploads an object to s3 encoded as json
23+ * @param {object } object
24+ * @param {string } filename The filename (Default = randomized)
25+ * @param {string } bucket The bucket name (Default = picked from config.json)
26+ * @returns {Promise<savedFile> } The etag and url for the file saved
27+ */
2028export const upload = function ( object :object , filename :string = v4 ( ) + '.json' , bucket :string = config . S3 . bucket ) : Promise < savedFile > {
2129 return new Promise ( ( resolve , reject ) => {
2230 client . putObject ( bucket , filename , JSON . stringify ( object ) , function ( err , etag ) {
2331 if ( err ) return reject ( err )
2432 resolve ( { etag, url : urlForFilename ( bucket , filename ) } )
2533 } )
2634 } )
35+ }
36+
37+ /**
38+ * Downloads a file from url and encodes it
39+ * @param {string } url
40+ * @param {string } enc (Default = 'base64')
41+ * @returns {Promise<string> } the downloaded file encoded as specified
42+ */
43+ export const download = async function ( url : string , enc : string = 'base64' ) : Promise < string > {
44+ if ( ! url ) return ''
45+
46+ const { data} = await axios . get ( url )
47+ return Buffer . from ( data ) . toString ( enc )
2748}
0 commit comments