Skip to content

Commit

Permalink
File Upload Database
Browse files Browse the repository at this point in the history
  • Loading branch information
WebDevSimplified committed Mar 16, 2019
1 parent f53e6ff commit 28ae0b6
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 175 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.env
node_modules
public/uploads
node_modules
16 changes: 8 additions & 8 deletions models/book.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const mongoose = require('mongoose')
const path = require('path')

const coverImageBasePath = 'uploads/bookCovers'

const bookSchema = new mongoose.Schema({
title: {
Expand All @@ -24,7 +21,11 @@ const bookSchema = new mongoose.Schema({
required: true,
default: Date.now
},
coverImageName: {
coverImage: {
type: Buffer,
required: true
},
coverImageType: {
type: String,
required: true
},
Expand All @@ -36,10 +37,9 @@ const bookSchema = new mongoose.Schema({
})

bookSchema.virtual('coverImagePath').get(function() {
if (this.coverImageName != null) {
return path.join('/', coverImageBasePath, this.coverImageName)
if (this.coverImage != null && this.coverImageType != null) {
return `data:${this.coverImageType};charset=utf-8;base64,${this.coverImage.toString('base64')}`
}
})

module.exports = mongoose.model('Book', bookSchema)
module.exports.coverImageBasePath = coverImageBasePath
module.exports = mongoose.model('Book', bookSchema)
149 changes: 10 additions & 139 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-ejs-layouts": "^2.5.0",
"mongoose": "^5.4.18",
"multer": "^1.4.1"
"mongoose": "^5.4.18"
},
"devDependencies": {
"dotenv": "^6.2.0",
Expand Down
13 changes: 13 additions & 0 deletions public/javascripts/fileUpload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FilePond.registerPlugin(
FilePondPluginImagePreview,
FilePondPluginImageResize,
FilePondPluginFileEncode,
)

FilePond.setOptions({
stylePanelAspectRatio: 150 / 100,
imageResizeTargetWidth: 100,
imageResizeTargetHeight: 150
})

FilePond.parse(document.body);
Loading

0 comments on commit 28ae0b6

Please sign in to comment.