Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design1
/
MAC_Project1
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
오윤석
2021-05-12 05:06:42 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3c2f65549f18a46f5302ba322ae62999258d3507
3c2f6554
1 parent
3d233512
gif upload
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
0 deletions
lambda/gif-upload/src/index.js
lambda/gif-upload/src/index.js
0 → 100644
View file @
3c2f655
const
imports
=
require
(
'./import'
)[
'import'
];
const
Busboy
=
imports
.
Busboy
;
const
UUID
=
imports
.
UUID
;
const
aws
=
require
(
'aws-sdk'
);
const
s3
=
new
aws
.
S3
();
exports
.
handler
=
async
(
event
,
context
)
=>
{
if
(
!
event
.
body
||
!
/^multipart
\/
form
\-
data/
.
test
(
event
.
headers
[
'content-type'
]))
{
return
{
statusCode
:
400
}
}
const
formData
=
await
parse
(
event
);
if
(
!
formData
[
'gif'
])
{
return
{
statusCode
:
400
}
}
const
id
=
await
upload
(
formData
[
'gif'
]);
return
{
statusCode
:
200
,
headers
:{
"Content-Type"
:
"json"
},
body
:
JSON
.
stringify
({
id
}),
}
}
const
parse
=
(
event
)
=>
new
Promise
((
resolve
,
reject
)
=>
{
const
bodyBuffer
=
new
Buffer
(
event
.
body
.
toString
(),
"base64"
);
const
busboy
=
new
Busboy
({
headers
:
{
'content-type'
:
event
.
headers
[
'content-type'
]
}
});
const
formData
=
{};
busboy
.
on
(
'file'
,
(
fieldname
,
file
,
filename
,
encoding
,
mimetype
)
=>
{
console
.
log
(
'File [%s]: filename=%j; encoding=%j; mimetype=%j'
,
fieldname
,
filename
,
encoding
,
mimetype
);
const
chunks
=
[];
file
.
on
(
'data'
,
data
=>
{
chunks
.
push
(
data
);
}).
on
(
'end'
,
()
=>
{
formData
[
fieldname
]
=
{
name
:
filename
,
data
:
Buffer
.
concat
(
chunks
),
mimetype
:
mimetype
};
console
.
log
(
"File [%s] finished."
,
filename
);
});
});
busboy
.
on
(
'field'
,
(
fieldname
,
value
)
=>
{
console
.
log
(
"["
+
fieldname
+
"] >> "
+
value
);
formData
[
fieldname
]
=
value
;
});
busboy
.
on
(
'error'
,
error
=>
{
reject
(
error
);
});
busboy
.
on
(
'finish'
,
()
=>
{
resolve
(
formData
);
});
busboy
.
write
(
bodyBuffer
,
event
.
isBase64Encoded
?
'base64'
:
'binary'
);
busboy
.
end
();
});
const
upload
=
({
data
,
mimetype
})
=>
new
Promise
((
resolve
,
reject
)
=>
{
const
bucket
=
'gif-generator'
;
const
path
=
'/gif'
;
const
id
=
UUID
().
replace
(
/
\-
/g
,
''
);
const
fileFullName
=
path
+
'/'
+
id
+
'.gif'
;
const
params
=
{
Bucket
:
bucket
,
Key
:
fileFullName
,
Body
:
data
,
ContentType
:
mimetype
};
s3
.
upload
(
params
,
(
err
,
data
)
=>
{
if
(
err
){
console
.
log
(
"upload err"
);
console
.
log
(
err
);
reject
(
err
);
}
else
{
console
.
log
(
"upload success"
);
console
.
log
(
data
);
resolve
(
id
);
}
});
});
\ No newline at end of file
Please
register
or
login
to post a comment