@@ -28,7 +28,7 @@ function getExtension(filename) {
2828export function getObjectKey ( url ) {
2929 const urlArray = url . split ( '/' ) ;
3030 let objectKey ;
31- if ( urlArray . length === 6 ) {
31+ if ( urlArray . length === 5 ) {
3232 const key = urlArray . pop ( ) ;
3333 const userId = urlArray . pop ( ) ;
3434 objectKey = `${ userId } /${ key } ` ;
@@ -98,24 +98,72 @@ export function signS3(req, res) {
9898 res . json ( result ) ;
9999}
100100
101- export function copyObjectInS3 ( req , res ) {
101+ export function copyObjectInS3 ( url , userId ) {
102+ return new Promise ( ( resolve , reject ) => {
103+ const objectKey = getObjectKey ( url ) ;
104+ const fileExtension = getExtension ( objectKey ) ;
105+ const newFilename = uuid . v4 ( ) + fileExtension ;
106+ const headParams = {
107+ Bucket : `${ process . env . S3_BUCKET } ` ,
108+ Key : `${ objectKey } `
109+ } ;
110+ client . s3 . headObject ( headParams , ( headErr ) => {
111+ if ( headErr ) {
112+ reject ( new Error ( `Object with key ${ process . env . S3_BUCKET } /${ objectKey } does not exist.` ) ) ;
113+ return ;
114+ }
115+ const params = {
116+ Bucket : `${ process . env . S3_BUCKET } ` ,
117+ CopySource : `${ process . env . S3_BUCKET } /${ objectKey } ` ,
118+ Key : `${ userId } /${ newFilename } ` ,
119+ ACL : 'public-read'
120+ } ;
121+ const copy = client . copyObject ( params ) ;
122+ copy . on ( 'err' , ( err ) => {
123+ reject ( err ) ;
124+ } ) ;
125+ copy . on ( 'end' , ( data ) => {
126+ resolve ( `${ s3Bucket } ${ userId } /${ newFilename } ` ) ;
127+ } ) ;
128+ } ) ;
129+ } ) ;
130+ }
131+
132+ export function copyObjectInS3RequestHandler ( req , res ) {
102133 const { url } = req . body ;
103- const objectKey = getObjectKey ( url ) ;
104- const fileExtension = getExtension ( objectKey ) ;
105- const newFilename = uuid . v4 ( ) + fileExtension ;
106- const userId = req . user . id ;
107- const params = {
108- Bucket : `${ process . env . S3_BUCKET } ` ,
109- CopySource : `${ process . env . S3_BUCKET } /${ objectKey } ` ,
110- Key : `${ userId } /${ newFilename } ` ,
111- ACL : 'public-read'
112- } ;
113- const copy = client . copyObject ( params ) ;
114- copy . on ( 'err' , ( err ) => {
115- console . log ( err ) ;
134+ copyObjectInS3 ( url , req . user . id ) . then ( ( newUrl ) => {
135+ res . json ( { url : newUrl } ) ;
116136 } ) ;
117- copy . on ( 'end' , ( data ) => {
118- res . json ( { url : `${ s3Bucket } ${ userId } /${ newFilename } ` } ) ;
137+ }
138+
139+ export function moveObjectToUserInS3 ( url , userId ) {
140+ return new Promise ( ( resolve , reject ) => {
141+ const objectKey = getObjectKey ( url ) ;
142+ const fileExtension = getExtension ( objectKey ) ;
143+ const newFilename = uuid . v4 ( ) + fileExtension ;
144+ const headParams = {
145+ Bucket : `${ process . env . S3_BUCKET } ` ,
146+ Key : `${ objectKey } `
147+ } ;
148+ client . s3 . headObject ( headParams , ( headErr ) => {
149+ if ( headErr ) {
150+ reject ( new Error ( `Object with key ${ process . env . S3_BUCKET } /${ objectKey } does not exist.` ) ) ;
151+ return ;
152+ }
153+ const params = {
154+ Bucket : `${ process . env . S3_BUCKET } ` ,
155+ CopySource : `${ process . env . S3_BUCKET } /${ objectKey } ` ,
156+ Key : `${ userId } /${ newFilename } ` ,
157+ ACL : 'public-read'
158+ } ;
159+ const move = client . moveObject ( params ) ;
160+ move . on ( 'err' , ( err ) => {
161+ reject ( err ) ;
162+ } ) ;
163+ move . on ( 'end' , ( data ) => {
164+ resolve ( `${ s3Bucket } ${ userId } /${ newFilename } ` ) ;
165+ } ) ;
166+ } ) ;
119167 } ) ;
120168}
121169
0 commit comments