Skip to content

Commit

Permalink
Schema nome dei file. Static route.
Browse files Browse the repository at this point in the history
Adesso il nome dei file è un hash SHA1 della data di caricamento. 
Corretti alcuni errori relativi alla directory pubblica.
  • Loading branch information
Richard1984 committed Mar 19, 2019
1 parent a71760a commit 929a4a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions server/controllers/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const {

const getDocument = async (req, res, next) => {
const url = req.url
const id = path.basename(url, path.extname(url))
const document = await Document.findById(id)

console.log(document)
const directory = path.basename(url)
const [document] = await Document.find({
directory
})

if (!document) {
return res.status(404).json({
Expand Down
10 changes: 7 additions & 3 deletions server/middlewares/file_upload.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const multer = require('multer')
const path = require('path')
const crypto = require('crypto')

const {
asyncMiddleware
} = require('./async')

const storage = multer.diskStorage({
destination: function (req, file, cb) {
destination (req, file, cb) {
cb(null, path.join(__dirname, '..', 'public', 'public', 'documents'))
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + path.extname(file.originalname))
filename (req, file, cb) {
const hash = crypto.createHash('SHA1')
const date = new Date()
hash.update(date.toISOString())
cb(null, hash.digest('hex') + path.extname(file.originalname))
}
})

Expand Down
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ app.use((req, res, next) => {
next()
})
// Routes
app.use('/static', require('./routes/api/api'))
app.use('/static', require('./routes/static'))
app.use('/api', require('./routes/api/api'))

// Public directory
app.use(express.static(path.join(__dirname, '/public')))
app.use('/pics', express.static(path.join(__dirname, 'public', 'pics')))
app.use(express.static(path.join(__dirname, '/client')))

app.get('/', (req, res) => {
Expand Down

0 comments on commit 929a4a4

Please sign in to comment.