import settings from '../settings' ; import { S3Settings } from './settings' ; import * as mime from 'mime-types' ; import * as path from 'path' ; import * as crypto from 'crypto' ; import S3 from 'aws-sdk/clients/s3' ; import GitlabHelper from './gitlabHelper' ; export const sleep = ( milliseconds ) => { return new Promise ( ( resolve ) => setTimeout ( resolve , milliseconds ) ) ; } ; /**
 * Generate regular expression which finds userid and cross-project issue references
 * from usermap and projectmap
 */ export const generateUserProjectRegex = ( ) => { let reString = '' ; if ( settings . usermap !== null && Object . keys ( settings . usermap ) . length > 0 ) { reString = '@' + Object . keys ( settings . usermap ) . join ( '|@' ) ; } if ( settings . projectmap !== null && Object . keys ( settings . projectmap ) . length > 0 ) { if ( reString . length > 0 ) { reString += '|' ; } reString += Object . keys ( settings . projectmap ) . join ( '#|' ) + '#' ; } return new RegExp ( reString , 'g' ) ; } ; export const migrateAttachments = async ( body , githubRepoId , s3 , gitlabHelper ) => { const regexp = /(!?)\[([^\]]+)\]\((\/uploads[^)]+)\)/g ; const offsetToAttachment = { } ; const matches = body . matchAll ( regexp ) ; for ( const match of matches ) { const prefix = match [ 1 ] || '' ; const name = match [ 2 ] ; const url = match [ 3 ] ; if ( s3 && s3 . bucket ) { const basename = path . basename ( url ) ; const mimeType = mime . lookup ( basename ) ; const attachmentBuffer = await gitlabHelper . getAttachment ( url ) ; if ( ! attachmentBuffer ) { continue ; } const hash = crypto . createHash ( 'sha256' ) ; hash . update ( url ) ; const newFileName = hash . digest ( 'hex' ) + '/' + basename ; const relativePath = githubRepoId ? `${ githubRepoId }/${ newFileName }` : newFileName ; const s3url = `https://${ s3 . bucket }.s3.amazonaws.com/${ relativePath }` ; const s3bucket = new S3 ( ) ; s3bucket . createBucket ( ( ) => { const params = { Key : relativePath , Body : attachmentBuffer , ContentType : mimeType === false ? null : mimeType , Bucket : s3 . bucket , } ; s3bucket . upload ( params , function ( err , data ) { console . log ( `\tUploading ${ basename } to ${ s3url }... ` ) ; if ( err ) { console . log ( 'ERROR: ' , err ) ; } else { console . log ( `\t...Done uploading` ) ; } } ) ; } ) ; offsetToAttachment [ match . index ] = `${ prefix }[${ name }](${ s3url })` ; } else { const host = gitlabHelper . host . endsWith ( '/' ) ? gitlabHelper . host : gitlabHelper . host + '/' ; const attachmentUrl = host + gitlabHelper . projectPath + url ; offsetToAttachment [ match . index ] = `${ prefix }[${ name }](${ attachmentUrl })` ; } } return body . replace ( regexp , ( { } , { } , { } , { } , offset , { } ) => offsetToAttachment [ offset ] ) ; } ;