-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3505c3f
commit 365a8da
Showing
14 changed files
with
3,761 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
end_of_line = lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "airbnb-base", | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"class-methods-use-this": ["error", { "exceptMethods": ["store", "show"] }], | ||
"no-underscore-dangle": ["error", { "allow": ["_id"] }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,10 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# vscode | ||
.vscode | ||
|
||
# upload files | ||
tmp/* | ||
!tmp/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{ | ||
"info": { | ||
"_postman_id": "7de34d26-553a-4a71-be91-b22462e0fe99", | ||
"name": "Box-React", | ||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" | ||
}, | ||
"item": [ | ||
{ | ||
"name": "boxes-store", | ||
"request": { | ||
"method": "POST", | ||
"header": [ | ||
{ | ||
"key": "Content-Type", | ||
"name": "Content-Type", | ||
"value": "application/json", | ||
"type": "text" | ||
} | ||
], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n\t\"title\":\"teste\"\n}" | ||
}, | ||
"url": { | ||
"raw": "{{host}}/boxes", | ||
"host": [ | ||
"{{host}}" | ||
], | ||
"path": [ | ||
"boxes" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "boxes view", | ||
"request": { | ||
"method": "GET", | ||
"header": [ | ||
{ | ||
"key": "Content-Type", | ||
"name": "Content-Type", | ||
"value": "application/x-www-form-urlencoded", | ||
"type": "text" | ||
} | ||
], | ||
"url": { | ||
"raw": "{{host}}/boxes/5cdcc793620f164d4e3fe9d5", | ||
"host": [ | ||
"{{host}}" | ||
], | ||
"path": [ | ||
"boxes", | ||
"5cdcc793620f164d4e3fe9d5" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "boxes store file", | ||
"request": { | ||
"method": "POST", | ||
"header": [ | ||
{ | ||
"key": "Content-Type", | ||
"name": "Content-Type", | ||
"value": "application/x-www-form-urlencoded", | ||
"type": "text" | ||
} | ||
], | ||
"body": { | ||
"mode": "formdata", | ||
"formdata": [ | ||
{ | ||
"key": "file", | ||
"type": "file", | ||
"src": "/home/edeno/Imagens/Screenshot_2019-03-29-08-34-10-951_com.android.chrome.jpg" | ||
} | ||
] | ||
}, | ||
"url": { | ||
"raw": "{{host}}/boxes/5cdcc793620f164d4e3fe9d5/files", | ||
"host": [ | ||
"{{host}}" | ||
], | ||
"path": [ | ||
"boxes", | ||
"5cdcc793620f164d4e3fe9d5", | ||
"files" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
} | ||
], | ||
"variable": [ | ||
{ | ||
"id": "9d3d4442-6273-41c8-a2c1-9b965135c770", | ||
"key": "host", | ||
"value": "http://localhost:3333", | ||
"type": "string" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "backend", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "nodemon src/server.js", | ||
"start": "node src/server.js" | ||
}, | ||
"dependencies": { | ||
"cors": "^2.8.5", | ||
"express": "^4.16.4", | ||
"mongoose": "^5.5.8", | ||
"multer": "^1.4.1", | ||
"socket.io": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.1", | ||
"eslint-plugin-react": "^7.13.0", | ||
"jslint": "^0.12.1", | ||
"nodemon": "^1.19.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const multer = require('multer'); | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
|
||
const storePath = path.resolve(__dirname, '..', '..', 'tmp'); | ||
|
||
module.exports = { | ||
dest: storePath, | ||
storage: multer.diskStorage({ | ||
destination: (req, file, callBack) => { | ||
callBack(null, storePath); | ||
}, | ||
filename: (req, file, callBack) => { | ||
crypto.randomBytes(16, (err, hash) => { | ||
if (err) callBack(err); | ||
file.key = `${hash.toString('hex')}-${file.originalname}`; | ||
callBack(null, file.key); | ||
}); | ||
}, | ||
}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const Box = require('../models/Box'); | ||
|
||
class BoxController { | ||
async store(req, res) { | ||
const box = await Box.create({ title: req.body.title }); | ||
return res.json(box); | ||
} | ||
|
||
async show(req, res) { | ||
const box = await Box.findById(req.params.id).populate({ | ||
path: 'files', | ||
options: { sort: { createdAr: -1 } }, | ||
}); | ||
return res.json(box); | ||
} | ||
} | ||
|
||
module.exports = new BoxController(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const File = require('../models/File'); | ||
const Box = require('../models/Box'); | ||
|
||
class FileController { | ||
async store(req, res) { | ||
const box = await Box.findById(req.params.id); | ||
const file = await File.create({ title: req.file.originalname, path: req.file.key }); | ||
box.files.push(file); | ||
await box.save(); | ||
req.io.sockets.in(box._id).emit('file', file); | ||
return res.json(file); | ||
} | ||
} | ||
|
||
module.exports = new FileController(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const Box = new mongoose.Schema( | ||
{ | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'File' }], | ||
}, | ||
{ | ||
timestamps: true, | ||
}, | ||
); | ||
|
||
module.exports = mongoose.model('Box', Box); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const File = new mongoose.Schema( | ||
{ | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
path: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
toObject: { virtuals: true }, | ||
toJSON: { virtuals: true }, | ||
}, | ||
); | ||
|
||
function VirtualUrl() { | ||
const url = process.env.URL || 'http://localhost:3333'; | ||
return `${url}/files/${encodeURIComponent(this.path)}`; | ||
} | ||
|
||
File.virtual('url').get(VirtualUrl); | ||
|
||
module.exports = mongoose.model('File', File); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const express = require('express'); | ||
const multer = require('multer'); | ||
const multerConfig = require('../config/multer'); | ||
|
||
const BoxController = require('../controllers/BoxController'); | ||
const FileController = require('../controllers/FileController'); | ||
|
||
const routes = express.Router(); | ||
|
||
routes.post('/boxes', BoxController.store); | ||
routes.get('/boxes/:id', BoxController.show); | ||
routes.post('/boxes/:id/files', multer(multerConfig).single('file'), FileController.store); | ||
|
||
module.exports = routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const cors = require('cors'); | ||
const mongoose = require('mongoose'); | ||
|
||
const app = express(); | ||
app.use(cors()); | ||
const server = require('http').Server(app); | ||
const io = require('socket.io')(server); | ||
const routes = require('./routes'); | ||
|
||
io.on('connection', (socket) => { | ||
socket.on('connectRoom', (box) => { | ||
socket.join(box); | ||
}); | ||
}); | ||
|
||
mongoose.connect('mongodb+srv://omnistack:[email protected]/omnistack?retryWrites=true', { | ||
useNewUrlParser: true, | ||
}); | ||
|
||
app.use((req, res, next) => { | ||
req.io = io; | ||
return next(); | ||
}); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use('/files', express.static(path.resolve(__dirname, '..', 'tmp'))); | ||
|
||
app.use(routes); | ||
|
||
server.listen(process.env.PORT || 3333); |
Empty file.
Oops, something went wrong.