Skip to content

Commit

Permalink
Merge pull request #10 from jwblangley/mongo
Browse files Browse the repository at this point in the history
Mongo
  • Loading branch information
jwblangley authored Dec 25, 2019
2 parents 6d6dd1c + 5464fa3 commit 5616995
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 192 deletions.
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ yarn-error.log*


# End of default

# Backend
!backend/.config/directory.txt
backend/.config/

# Frontend
frontend/.env
.env
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Backend

Edit the file `backend/.config/directory.txt` to change working directory location. By default this is the folder `plo-data` in the home directory
Edit the file `backend/run.sh` to change the data directory location - this is where all uploaded media content will be stored. By default this is the folder `.plo` in the home directory

If required edit the port number in `run.sh`. This should match the port number entered in the front end config.
If required edit the port number in `run.sh`. This should match the port number entered in the frontend config.

<hr>
Requires `ffmpeg` to be installed.
<hr>
* Requires `ffmpeg` and `mongo` to be installed.
* Ensure that the mongo data store path is set. This can be done with `mongod --dbpath <location>`. It is recommended to use `/data/db`.

Run `npm start` to start the server

Expand Down
1 change: 0 additions & 1 deletion backend/.config/directory.txt

This file was deleted.

3 changes: 0 additions & 3 deletions backend/defaultManager.json

This file was deleted.

74 changes: 52 additions & 22 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,76 @@ const cors = require('cors')
const fs = require('fs')
const multer = require('multer')

const manager = require('./manager.js')
const mongoManager = require('./mongoManager.js')

const app = express()
const API_PORT = process.env.PORT_BASE
const DATA_DIRECTORY = process.env.DATA_DIRECTORY

const GARBAGE_COLLECT_EVERY = 10

// Setup
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
app.use(bodyParser.json())


// API calls
app.get('/listUsers', (req, res) => {
/*----------------------------------------------------------------------------*/

app.get('/listUsers', async (req, res) => {
console.log("Call to listUsers")
res.send(manager.listUsers())
users = await mongoManager.listUsers()
res.send(users)
})

app.post('/createAccount', (req, res) => {
console.log("Call to createAccount")
app.post('/createUser', async (req, res) => {
console.log("Call to createUser")
const { name } = req.body
try {
manager.createAccount(name)
await mongoManager.createUser(name)
res.send('Added \"' + name + '\"\n')
} catch(err) {
res.status(422).send('Username already exists\n')
return
console.log(err.message)
res.status(422).send(err.message + '\n')
}
res.send('Added \"' + name + '\"\n')
})

app.post('/deleteAccount', (req, res) => {
console.log("Call to deleteAccount")
const { name } = req.body
manager.deleteAccount(name)
res.send('Deleted \"' + name + '\"\n')
app.post('/deleteUser', async (req, res) => {
console.log("Call to deleteUser")
const { user } = req.body
await mongoManager.deleteUser(user)
res.send('Deleted \"' + user + '\"\n')
})

app.get('/:username/getUserContent', (req, res) => {
app.get('/:username/getUserContent', async (req, res) => {
console.log("Call to getUserContent")
const username = req.params.username
res.send(manager.getUserContent(username))
userContent = await mongoManager.getUserContent(username)
res.send(userContent)
})

// TODO: more RESTful solution?
app.post('/:username/saveUserContent', (req, res) => {
var saves = 0
// TODO: more RESTful approach - difficult as we currently bundle changes to save
app.post('/:username/saveUserContent', async (req, res) => {
console.log("Call to saveUserContent")
const { content } = req.body
const username = req.params.username
manager.saveUserContent(username, content)
await mongoManager.saveUserContent(username, content)

if (saves++ % GARBAGE_COLLECT_EVERY === 0) {
console.log("Garbage collect")
await garbageCollect()
}

res.send("Saved user content")
})

// Multer to handle disk storage for uploads
var storage = multer.diskStorage({
destination: function (req, file, cb) {
// Set storage directory to working directory
cb(null, manager.getWorkingDirectory())
cb(null, DATA_DIRECTORY)
},
filename: function (req, file, cb) {
// Rename files by prepending date to avoid name clashes
Expand All @@ -79,7 +94,7 @@ app.post('/:username/addUserMedia', (req, res) => {
}

// Ensure to wait for addUserMedia to finish
await manager.addUserMedia(username, req.files)
await mongoManager.addUserMedia(username, req.files, DATA_DIRECTORY)

return res.send("Successfully uploaded " + req.files.length + " "
+ (req.files.length > 1 ? "files" : "file"))
Expand All @@ -99,13 +114,28 @@ app.post('/:username/addUserGallery', (req, res) => {
}

// Ensure to wait for addUserMedia to finish
await manager.addUserGallery(username, req.files)
await mongoManager.addUserGallery(username, req.files, DATA_DIRECTORY)

return res.send("Successfully uploaded " + req.files.length + " "
+ (req.files.length > 1 ? "files" : "file") + " to gallery")
}
)
})

/*----------------------------------------------------------------------------*/

// Delete files that are no longer being referenced
async function garbageCollect() {
const allMedia = await mongoManager.getAllMedia()

// Read directory
var allFiles = fs.readdirSync(DATA_DIRECTORY)
// Delete all files (except the manager) that are not referenced
allFiles
.filter(f => f !== 'manager.json')
.filter(f => !allMedia.includes(f))
.forEach(f => fs.unlinkSync(DATA_DIRECTORY + "/" + f))
}


app.listen(API_PORT, () => console.log(`Server running on port ${API_PORT}`))
8 changes: 4 additions & 4 deletions backend/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function listUsers() {
return manager.users.map(u => u.name)
}

function createAccount(name) {
function createUser(name) {
var manager = readManagerSync()

if (listUsers().includes(name)) {
Expand All @@ -88,7 +88,7 @@ function createAccount(name) {
writeManagerSync(manager)
}

function deleteAccount(name) {
function deleteUser(name) {
var manager = readManagerSync()

var users = [...manager.users]
Expand Down Expand Up @@ -195,8 +195,8 @@ async function addUserGallery(username, files) {
module.exports = {
getWorkingDirectory,
listUsers,
createAccount,
deleteAccount,
createUser,
deleteUser,
getUserContent,
saveUserContent,
addUserMedia,
Expand Down
Loading

0 comments on commit 5616995

Please sign in to comment.