Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const viewAllPath = './views/viewAll.html'
- const addMemePath = './views/addMeme.html'
- const getDetailsPath = './views/details.html'
- const dbPath = './db/db.json'
- const fs = require('fs')
- const url = require('url')
- const util = require('util')
- const randomstring = require('randomstring')
- const formidable = require('formidable')
- // count of img-s per folder
- const maxFilesInFolder = 10
- function viewAll (req, res) {
- fs.readFile(viewAllPath, 'utf8', (err, data) => {
- if (err) {
- console.log(err)
- return
- }
- res.writeHead(200, {
- 'Content-Type': 'text/html'
- })
- fs.readFile(dbPath, 'utf8', (err, db) => {
- if (err) {
- console.log(err)
- return
- }
- db = JSON.parse(db)
- db = db.sort((a, b) => {
- return parseInt(b.dateStamp) - parseInt(a.dateStamp)
- })
- let memes = []
- for (let index in db) {
- let meme = db[index]
- // if (meme.privacy === 'on') {
- memes.push(
- `<div class="meme">
- <a href="/getDetails?id=${meme.id}">
- <img class="memePoster" height="150" src="${meme.memeSrc}"/>
- </div>`
- )
- // }
- }
- data = data.replace(
- '<div id="replaceMe">{{replaceMe}}</div>',
- memes.join('')
- )
- res.end(data)
- })
- })
- }
- function viewAddMeme (req, res) {
- fs.readFile(addMemePath, 'utf8', (err, data) => {
- if (err) {
- console.log(err)
- return
- }
- res.writeHead(200, {
- 'Content-Type': 'text/html'
- })
- res.end(data)
- })
- }
- // not finished
- function addMeme (req, res) {
- let form = new formidable.IncomingForm()
- // opens the core storage folder
- fs.readdir('./public/memeStorage/', (err, foldersData) => {
- // there is no point to loop over all folders
- // this will check the count of elems in the folder and try to access the folder with name with length-1
- // so if there is 1 elem named 0 it will access the 0 folder
- // !!! you must always have folder named 0, even if you have 0 elems
- let folderLength = foldersData.length - 1
- // first we will read the folder that is named with highest number
- fs.readdir(`./public/memeStorage/${folderLength}`, (err, data) => {
- if (err) {
- console.log(err)
- return
- }
- // this will check if the folder is overfiled
- // and if it is it will increment the folderLenght variable
- if (data.length >= maxFilesInFolder) {
- folderLength++
- }
- let folderName = `./public/memeStorage/${folderLength.toString()}`
- // this will check if the folder you are looking for exists and if not will create a new one with sync function
- if (foldersData.indexOf(folderLength.toString()) === -1) {
- fs.mkdirSync(folderName)
- }
- // set the folder path to the form
- form.uploadDir = folderName
- form.parse(req, function (err, fields, files) {
- res.writeHead(200, {
- 'content-type': 'text/plain'
- })
- // an atempt reads the original db file
- fs.readFile(dbPath, 'utf8', (err, data) => {
- if (err) {
- console.log(err)
- return
- }
- // gets the existing db as JSON
- data = JSON.parse(data)
- // creates a new item for the db
- let entry = {
- id: randomstring.generate(10),
- memeSrc: files.meme.path,
- memeTitle: fields.memeTitle,
- status: fields.status,
- memeDescription: fields.memeDescription,
- dateStamp: Date.now()
- }
- // add the new entry to the db
- data.push(entry)
- // creates a new file for the db(wtih the newest entry)
- fs.writeFile('./db/db1.json', JSON.stringify(data), err => {
- if (err) {
- console.log(err)
- return
- }
- console.log(`The file has been saved in folder ${folderName}`)
- })
- res.write('received upload:\n\n')
- res.end(
- util.inspect({
- fields: fields,
- files: files
- })
- )
- })
- })
- })
- })
- }
- function getDetails (req, res) {
- fs.readFile(getDetailsPath, 'utf8', (err, data) => {
- if (err) {
- console.log(err)
- return
- }
- res.writeHead(200, {
- 'Content-Type': 'text/html'
- })
- fs.readFile(dbPath, 'utf8', (err, db) => {
- if (err) {
- console.log(err)
- return
- }
- let memeId = url.parse(req.url, true).query.id
- db = JSON.parse(db)
- for (let targetedMeme of db) {
- if (targetedMeme.id === memeId) {
- let memeHtml = `<div class="content">
- <img src="${targetedMeme.memeSrc}" alt=""/>
- <h3>Title ${targetedMeme.title}</h3>
- <p> ${targetedMeme.description}</p>
- <button><a href="${targetedMeme.memeSrc}" download>Download Meme</a></button>
- </div>`
- data = data.replace(
- '<div id="replaceMe">{{replaceMe}}</div>',
- memeHtml
- )
- res.end(data)
- }
- }
- })
- })
- }
- module.exports = (req, res) => {
- if (req.pathname === '/viewAllMemes' && req.method === 'GET') {
- viewAll(req, res)
- } else if (req.pathname === '/addMeme' && req.method === 'GET') {
- viewAddMeme(req, res)
- } else if (req.pathname === '/addMeme' && req.method === 'POST') {
- addMeme(req, res)
- } else if (req.pathname.startsWith('/getDetails') && req.method === 'GET') {
- getDetails(req, res)
- } else {
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement