Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
SSD Assignment2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pumudu seniya fernando
SSD Assignment2
Commits
e9b2efac
Commit
e9b2efac
authored
Oct 06, 2020
by
pumudu seniya fernando
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
768a1819
Pipeline
#1700
canceled with stages
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
0 deletions
+84
-0
routes/home.js
routes/home.js
+84
-0
No files found.
routes/home.js
0 → 100644
View file @
e9b2efac
const
{
Router
}
=
require
(
'
express
'
)
const
passport
=
require
(
'
passport
'
)
const
{
google
}
=
require
(
'
googleapis
'
)
const
KEYS
=
require
(
'
../configs/keys
'
)
const
router
=
Router
()
router
.
get
(
'
/
'
,
function
(
req
,
res
)
{
res
.
render
(
'
Main.html
'
,
{
'
title
'
:
'
Home
'
})
})
router
.
get
(
'
/dashboard
'
,
function
(
req
,
res
)
{
// if not user
if
(
typeof
req
.
user
==
"
undefined
"
)
res
.
redirect
(
'
/auth/login/google
'
)
else
{
let
parseData
=
{
title
:
'
File Uploader
'
,
googleid
:
req
.
user
.
_id
,
name
:
req
.
user
.
name
,
avatar
:
req
.
user
.
pic_url
,
email
:
req
.
user
.
email
}
// redirect with google drive response will be
if
(
req
.
query
.
file
!==
undefined
)
{
// successfully upload the file
if
(
req
.
query
.
file
==
"
upload
"
)
parseData
.
file
=
"
uploaded
"
else
if
(
req
.
query
.
file
==
"
notupload
"
)
parseData
.
file
=
"
notuploaded
"
}
res
.
render
(
'
uploader.html
'
,
parseData
)
}
})
router
.
post
(
'
/uploadtheFile
'
,
function
(
req
,
res
)
{
// not auth
if
(
!
req
.
user
)
res
.
redirect
(
'
/auth/login/google
'
)
else
{
// auth user
// config google drive with client token
const
oauth2Client
=
new
google
.
auth
.
OAuth2
()
oauth2Client
.
setCredentials
({
'
access_token
'
:
req
.
user
.
accessToken
});
const
drive
=
google
.
drive
({
version
:
'
v3
'
,
auth
:
oauth2Client
});
//move file to google drive
let
{
name
:
filename
,
mimetype
,
data
}
=
req
.
files
.
file_upload
const
driveResponse
=
drive
.
files
.
create
({
requestBody
:
{
name
:
filename
,
mimeType
:
mimetype
},
media
:
{
mimeType
:
mimetype
,
body
:
Buffer
.
from
(
data
).
toString
()
}
});
driveResponse
.
then
(
data
=>
{
if
(
data
.
status
==
200
)
res
.
redirect
(
'
/dashboard?file=upload
'
)
// success
else
res
.
redirect
(
'
/dashboard?file=notupload
'
)
// unsuccess
}).
catch
(
err
=>
{
throw
new
Error
(
err
)
})
}
})
module
.
exports
=
router
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment