import * as uuid from 'uuid' ; import { KeyParams } from '@standardnotes/auth' ; import { S3 } from 'aws-sdk' ; import { inject , injectable } from 'inversify' ; import { Logger } from 'winston' ; import TYPES from '../../Bootstrap/Types' ; import { Item } from '../../Domain/Item/Item' ; import { ItemBackupServiceInterface } from '../../Domain/Item/ItemBackupServiceInterface' ; import { ProjectorInterface } from '../../Projection/ProjectorInterface' ; @ injectable ( ) export class S3ItemBackupService implements ItemBackupServiceInterface { constructor ( @ inject ( TYPES . S3_BACKUP_BUCKET_NAME ) private s3BackupBucketName , @ inject ( TYPES . ItemProjector ) private itemProjector , @ inject ( TYPES . Logger ) private logger , @ inject ( TYPES . S3 ) private s3Client ? ) { } async backup ( items , authParams ) { if ( ! this . s3BackupBucketName || this . s3Client === undefined ) { this . logger . warn ( 'S3 backup not configured' ) ; return '' ; } const fileName = uuid . v4 ( ) ; const itemProjections = [ ] ; for ( const item of items ) { itemProjections . push ( await this . itemProjector . projectFull ( item ) ) ; } const uploadResult = await this . s3Client . upload ( { Bucket : this . s3BackupBucketName , Key : fileName , Body : JSON . stringify ( { items : itemProjections , auth_params : authParams , } ) , } ) . promise ( ) ; return uploadResult . Key ; } }