Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/move resources #139

Merged
merged 18 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
88c1270
Feat/get all collections directories (#117)
kwajiehao Mar 3, 2021
efc8e55
fix: merge conflict in folder, from #117
gweiying Mar 3, 2021
168c820
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 4, 2021
4ec4c25
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 8, 2021
fbaf164
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 9, 2021
ab82dbf
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 10, 2021
d287b1e
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 10, 2021
6e7fc62
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 16, 2021
1be7c61
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 16, 2021
b3f4036
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 17, 2021
349c398
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 18, 2021
486b7a5
Merge branch 'staging' of https://github.com/isomerpages/isomercms-ba…
gweiying Mar 23, 2021
edf8363
feat: add moveResources API
gweiying Mar 23, 2021
5d09a06
fix: adds backend check for if resource categories exist prior to move
gweiying Mar 24, 2021
bd0cb25
fix: update resourcePages routes to handle Base64 encoding on backend
gweiying Mar 24, 2021
81b4786
fix: update moveResource route to work on single file only
gweiying Mar 24, 2021
ddc0c54
Revert "fix: update moveResource route to work on single file only"
gweiying Mar 24, 2021
fd3410c
fix: typo in auth for resources route
gweiying Mar 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 All @@ -125,6 +126,7 @@ auth.get('/v1/sites/:siteName/resources', verifyJwt)
auth.post('/v1/sites/:siteName/resources', verifyJwt)
auth.delete('/v1/sites/:siteName/resources/:resourceName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/rename/:newResourceName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/move/:newResourceName', verifyJwt)

// Settings
auth.get('/v1/sites/:siteName/settings', 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
32 changes: 32 additions & 0 deletions routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { attachReadRouteHandlerWrapper, attachRollbackRouteHandlerWrapper } = req
// Import classes
const { ResourceRoom } = require('../classes/ResourceRoom.js')
const { Resource } = require('../classes/Resource.js')
const { File, ResourcePageType } = require('../classes/File');

// List resources
async function listResources (req, res, next) {
Expand Down Expand Up @@ -65,9 +66,40 @@ async function renameResource (req, res, next) {
res.status(200).json({ resourceName, newResourceName })
}

// Move resource
async function moveResources (req, res, next) {
const { accessToken } = req
const { siteName, resourceName, newResourceName } = req.params
const { files } = req.body

const ResourceRoomInstance = new ResourceRoom(accessToken, siteName)
const resourceRoomName = await ResourceRoomInstance.get()

const IsomerResource = new Resource(accessToken, siteName)
const resources = await IsomerResource.list(resourceRoomName)
const resourceCategories = resources.map(resource => resource.dirName)
if (!resourceCategories.includes(resourceName)) throw new NotFoundError(`Resource category ${resourceName} was not found!`)
if (!resourceCategories.includes(newResourceName)) throw new NotFoundError(`Resource category ${newResourceName} was not found!`)

const oldIsomerFile = new File(accessToken, siteName)
const newIsomerFile = new File(accessToken, siteName)
const oldResourcePageType = new ResourcePageType(resourceRoomName, resourceName)
const newResourcePageType = new ResourcePageType(resourceRoomName, newResourceName)
oldIsomerFile.setFileType(oldResourcePageType)
newIsomerFile.setFileType(newResourcePageType)

for (const fileName of files) {
const { content, sha } = await oldIsomerFile.read(fileName)
await oldIsomerFile.delete(fileName, sha)
await newIsomerFile.create(fileName, content)
}
res.status(200).send('OK')
}

router.get('/:siteName/resources', attachReadRouteHandlerWrapper(listResources))
router.post('/:siteName/resources', attachRollbackRouteHandlerWrapper(createNewResource))
router.delete('/:siteName/resources/:resourceName', attachRollbackRouteHandlerWrapper(deleteResource))
router.post('/:siteName/resources/:resourceName/rename/:newResourceName', attachRollbackRouteHandlerWrapper(renameResource))
router.post('/:siteName/resources/:resourceName/move/:newResourceName', attachRollbackRouteHandlerWrapper(moveResources))

module.exports = router;