Skip to content

Commit

Permalink
fix: update resourcePages routes to handle Base64 encoding on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gweiying committed Mar 24, 2021
1 parent 5d09a06 commit bd0cb25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ auth.post('/v1/sites/:siteName/pages/move/:newPagePath', verifyJwt)
auth.get('/v1/sites/:siteName/resources/:resourceName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages', verifyJwt)
auth.get('/v1/sites/:siteName/resources/:resourceName/pages/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages/new/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages/:pageName', verifyJwt)
auth.delete('/v1/sites/:siteName/resources/:resourceName/pages/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages/:pageName/rename/:newPageName', verifyJwt)
Expand Down
27 changes: 15 additions & 12 deletions routes/resourcePages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express');
const router = express.Router();
const base64 = require('base-64');

// Import middleware
const {
Expand Down Expand Up @@ -33,8 +34,8 @@ async function listResourcePages (req, res, next) {
async function createNewResourcePage(req, res, next) {
const { accessToken } = req

const { siteName, resourceName } = req.params
const { pageName, content } = req.body
const { siteName, resourceName, pageName } = req.params
const { content: pageContent } = req.body

// TO-DO:
// Validate pageName and content
Expand All @@ -52,9 +53,10 @@ async function createNewResourcePage(req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { sha } = await IsomerFile.create(pageName, content)

res.status(200).json({ resourceName, pageName, content, sha })
const { sha } = await IsomerFile.create(pageName, Base64.encode(pageContent))

res.status(200).json({ resourceName, pageName, pageContent, sha })
}

// Read page in resource
Expand All @@ -68,7 +70,8 @@ async function readResourcePage (req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { sha, content } = await IsomerFile.read(pageName)
const { sha, content: encodedContent } = await IsomerFile.read(pageName)
const content = base64.decode(encodedContent)

// TO-DO:
// Validate content
Expand All @@ -81,7 +84,7 @@ async function updateResourcePage (req, res, next) {
const { accessToken } = req

const { siteName, pageName, resourceName } = req.params
const { content, sha } = req.body
const { content: pageContent, sha } = req.body

// TO-DO:
// Validate pageName and content
Expand All @@ -91,9 +94,9 @@ async function updateResourcePage (req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { newSha } = await IsomerFile.update(pageName, content, sha)
const { newSha } = await IsomerFile.update(pageName, base64.encode(pageContent), sha)

res.status(200).json({ resourceName, pageName, content, sha: newSha })
res.status(200).json({ resourceName, pageName, pageContent, sha: newSha })
}

// Delete page in resource
Expand Down Expand Up @@ -125,7 +128,7 @@ async function renameResourcePage (req, res, next) {
const { accessToken } = req

const { siteName, pageName, resourceName, newPageName } = req.params
const { sha, content } = req.body
const { sha, content: pageContent } = req.body

// TO-DO:
// Validate that resource exists
Expand All @@ -138,13 +141,13 @@ async function renameResourcePage (req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { sha: newSha } = await IsomerFile.create(newPageName, content)
const { sha: newSha } = await IsomerFile.create(newPageName, base64.encode(pageContent))
await IsomerFile.delete(pageName, sha)

res.status(200).json({ resourceName, pageName: newPageName, content, sha: newSha })
res.status(200).json({ resourceName, pageName: newPageName, pageContent, sha: newSha })
}
router.get('/:siteName/resources/:resourceName', attachReadRouteHandlerWrapper(listResourcePages))
router.post('/:siteName/resources/:resourceName/pages', attachRollbackRouteHandlerWrapper(createNewResourcePage))
router.post('/:siteName/resources/:resourceName/pages/new/:pageName', attachRollbackRouteHandlerWrapper(createNewResourcePage))
router.get('/:siteName/resources/:resourceName/pages/:pageName', attachReadRouteHandlerWrapper(readResourcePage))
router.post('/:siteName/resources/:resourceName/pages/:pageName', attachWriteRouteHandlerWrapper(updateResourcePage))
router.delete('/:siteName/resources/:resourceName/pages/:pageName', attachRollbackRouteHandlerWrapper(deleteResourcePage))
Expand Down

0 comments on commit bd0cb25

Please sign in to comment.