mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
Move old backend code to old_src folder
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
c42d2223e8
commit
7b9f9a487b
97 changed files with 7 additions and 7 deletions
95
old_src/lib/web/note/actions.ts
Normal file
95
old_src/lib/web/note/actions.ts
Normal file
|
@ -0,0 +1,95 @@
|
|||
import { Request, Response } from 'express'
|
||||
|
||||
import { Note, Revision } from '../../models'
|
||||
import { logger } from '../../logger'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import shortId from 'shortid'
|
||||
import moment from 'moment'
|
||||
import querystring from 'querystring'
|
||||
|
||||
export function getInfo (req: Request, res: Response, note: Note): void {
|
||||
const body = note.content
|
||||
const extracted = Note.extractMeta(body)
|
||||
const markdown = extracted.markdown
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
const title = Note.decodeTitle(note.title)
|
||||
const data = {
|
||||
title: meta.title || title,
|
||||
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
|
||||
viewcount: note.viewcount,
|
||||
createtime: note.createdAt,
|
||||
updatetime: note.lastchangeAt
|
||||
}
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||
'Access-Control-Allow-Headers': 'Range',
|
||||
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(data)
|
||||
}
|
||||
|
||||
export function createGist (req: Request, res: Response, note: Note): void {
|
||||
const data = {
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
client_id: config.github.clientID,
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
redirect_uri: config.serverURL + '/auth/github/callback/' + Note.encodeNoteId(note.id) + '/gist',
|
||||
scope: 'gist',
|
||||
state: shortId.generate()
|
||||
}
|
||||
const query = querystring.stringify(data)
|
||||
res.redirect('https://github.com/login/oauth/authorize?' + query)
|
||||
}
|
||||
|
||||
export function getRevision (req: Request, res: Response, note: Note): void {
|
||||
const actionId = req.params.actionId
|
||||
if (actionId) {
|
||||
const time = moment(parseInt(actionId))
|
||||
if (time.isValid()) {
|
||||
Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
errors.errorInternalError(res)
|
||||
|
||||
return
|
||||
}
|
||||
if (!content) {
|
||||
errors.errorNotFound(res)
|
||||
return
|
||||
}
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||
'Access-Control-Allow-Headers': 'Range',
|
||||
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(content)
|
||||
})
|
||||
} else {
|
||||
errors.errorNotFound(res)
|
||||
}
|
||||
} else {
|
||||
Revision.getNoteRevisions(note, function (err, data) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
errors.errorInternalError(res)
|
||||
return
|
||||
}
|
||||
const out = {
|
||||
revision: data
|
||||
}
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||
'Access-Control-Allow-Headers': 'Range',
|
||||
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(out)
|
||||
})
|
||||
}
|
||||
}
|
131
old_src/lib/web/note/controller.ts
Normal file
131
old_src/lib/web/note/controller.ts
Normal file
|
@ -0,0 +1,131 @@
|
|||
import { Request, Response } from 'express'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { Note } from '../../models'
|
||||
import * as ActionController from './actions'
|
||||
import * as NoteUtils from './util'
|
||||
|
||||
export function publishNoteActions (req: Request, res: Response): void {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
switch (action) {
|
||||
case 'download':
|
||||
exports.downloadMarkdown(req, res, note)
|
||||
break
|
||||
case 'edit':
|
||||
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
||||
break
|
||||
default:
|
||||
res.redirect(config.serverURL + '/s/' + note.shortid)
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function showPublishNote (req: Request, res: Response): void {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use short id
|
||||
const shortid = req.params.shortid
|
||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||
}
|
||||
note.increment('viewcount').then(function (note) {
|
||||
if (!note) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||
res.set({
|
||||
'Cache-Control': 'private' // only cache by client
|
||||
})
|
||||
return res.render('pretty.ejs', data)
|
||||
})
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function showNote (req: Request, res: Response): void {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use note id
|
||||
const noteId = req.params.noteId
|
||||
const id = Note.encodeNoteId(note.id)
|
||||
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
|
||||
return res.redirect(config.serverURL + '/' + (note.alias || id))
|
||||
}
|
||||
const body = note.content
|
||||
const extracted = Note.extractMeta(body)
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
let title = Note.decodeTitle(note.title)
|
||||
title = Note.generateWebTitle(meta.title || title)
|
||||
const opengraph = Note.parseOpengraph(meta, title)
|
||||
res.set({
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
return res.render('codimd.ejs', {
|
||||
title: title,
|
||||
opengraph: opengraph
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function createFromPOST (req: Request, res: Response): void {
|
||||
let body = ''
|
||||
if (req.body && req.body.length > config.documentMaxLength) {
|
||||
return errors.errorTooLong(res)
|
||||
} else if (req.body) {
|
||||
body = req.body
|
||||
}
|
||||
body = body.replace(/[\r]/g, '')
|
||||
return NoteUtils.newNote(req, res, body)
|
||||
}
|
||||
|
||||
export function doAction (req: Request, res: Response): void {
|
||||
const noteId = req.params.noteId
|
||||
NoteUtils.findNoteOrCreate(req, res, (note) => {
|
||||
const action = req.params.action
|
||||
// TODO: Don't switch on action, choose action in Router and use separate functions
|
||||
switch (action) {
|
||||
case 'publish':
|
||||
case 'pretty': // pretty deprecated
|
||||
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||
break
|
||||
case 'slide':
|
||||
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||
break
|
||||
case 'download':
|
||||
exports.downloadMarkdown(req, res, note)
|
||||
break
|
||||
case 'info':
|
||||
ActionController.getInfo(req, res, note)
|
||||
break
|
||||
case 'gist':
|
||||
ActionController.createGist(req, res, note)
|
||||
break
|
||||
case 'revision':
|
||||
ActionController.getRevision(req, res, note)
|
||||
break
|
||||
default:
|
||||
return res.redirect(config.serverURL + '/' + noteId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function downloadMarkdown (req: Request, res: Response, note): void {
|
||||
const body = note.content
|
||||
let filename = Note.decodeTitle(note.title)
|
||||
filename = encodeURIComponent(filename)
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||
'Access-Control-Allow-Headers': 'Range',
|
||||
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
||||
'Content-Type': 'text/markdown; charset=UTF-8',
|
||||
'Cache-Control': 'private',
|
||||
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(body)
|
||||
}
|
29
old_src/lib/web/note/router.ts
Normal file
29
old_src/lib/web/note/router.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { Router } from 'express'
|
||||
import { markdownParser } from '../utils'
|
||||
import * as NoteController from './controller'
|
||||
|
||||
import * as SlideController from './slide'
|
||||
|
||||
const NoteRouter = Router()
|
||||
// get new note
|
||||
NoteRouter.get('/new', NoteController.createFromPOST)
|
||||
// post new note with content
|
||||
NoteRouter.post('/new', markdownParser, NoteController.createFromPOST)
|
||||
// post new note with content and alias
|
||||
NoteRouter.post('/new/:noteId', markdownParser, NoteController.createFromPOST)
|
||||
// get publish note
|
||||
NoteRouter.get('/s/:shortid', NoteController.showPublishNote)
|
||||
// publish note actions
|
||||
NoteRouter.get('/s/:shortid/:action', NoteController.publishNoteActions)
|
||||
// get publish slide
|
||||
NoteRouter.get('/p/:shortid', SlideController.showPublishSlide)
|
||||
// publish slide actions
|
||||
NoteRouter.get('/p/:shortid/:action', SlideController.publishSlideActions)
|
||||
// get note by id
|
||||
NoteRouter.get('/:noteId', NoteController.showNote)
|
||||
// note actions
|
||||
NoteRouter.get('/:noteId/:action', NoteController.doAction)
|
||||
// note actions with action id
|
||||
NoteRouter.get('/:noteId/:action/:actionId', NoteController.doAction)
|
||||
|
||||
export { NoteRouter }
|
41
old_src/lib/web/note/slide.ts
Normal file
41
old_src/lib/web/note/slide.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { Request, Response } from 'express'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { Note } from '../../models'
|
||||
import * as NoteUtils from './util'
|
||||
|
||||
export function publishSlideActions (req: Request, res: Response): void {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
if (action === 'edit') {
|
||||
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
||||
} else {
|
||||
res.redirect(config.serverURL + '/p/' + note.shortid)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function showPublishSlide (req: Request, res: Response): void {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use short id
|
||||
const shortid = req.params.shortid
|
||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||
return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||
}
|
||||
note.increment('viewcount').then(function (note) {
|
||||
if (!note) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||
res.set({
|
||||
'Cache-Control': 'private' // only cache by client
|
||||
})
|
||||
return res.render('slide.ejs', data)
|
||||
})
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
})
|
||||
}
|
149
old_src/lib/web/note/util.ts
Normal file
149
old_src/lib/web/note/util.ts
Normal file
|
@ -0,0 +1,149 @@
|
|||
import { Request, Response } from 'express'
|
||||
import fs from 'fs'
|
||||
|
||||
import path from 'path'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { Note } from '../../models'
|
||||
import { PhotoProfile } from '../../utils/PhotoProfile'
|
||||
|
||||
export function newNote (req, res: Response, body: string | null): void {
|
||||
let owner = null
|
||||
const noteId = req.params.noteId ? req.params.noteId : null
|
||||
if (req.isAuthenticated()) {
|
||||
owner = req.user.id
|
||||
} else if (!config.allowAnonymous) {
|
||||
return errors.errorForbidden(res)
|
||||
}
|
||||
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
|
||||
req.alias = noteId
|
||||
} else if (noteId) {
|
||||
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
|
||||
}
|
||||
Note.create({
|
||||
ownerId: owner,
|
||||
alias: req.alias ? req.alias : null,
|
||||
content: body
|
||||
}).then(function (note) {
|
||||
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)))
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}
|
||||
|
||||
export enum Permission {
|
||||
None,
|
||||
Read,
|
||||
Write,
|
||||
Owner
|
||||
}
|
||||
|
||||
interface NoteObject {
|
||||
ownerId?: string;
|
||||
permission: string;
|
||||
}
|
||||
|
||||
export function getPermission (user, note: NoteObject): Permission {
|
||||
// There are two possible User objects we get passed. One is from socket.io
|
||||
// and the other is from passport directly. The former sets the logged_in
|
||||
// parameter to either true or false, whereas for the latter, the logged_in
|
||||
// parameter is always undefined, and the existence of user itself means the
|
||||
// user is logged in.
|
||||
if (!user || user.logged_in === false) {
|
||||
// Anonymous
|
||||
switch (note.permission) {
|
||||
case 'freely':
|
||||
return Permission.Write
|
||||
case 'editable':
|
||||
case 'locked':
|
||||
return Permission.Read
|
||||
default:
|
||||
return Permission.None
|
||||
}
|
||||
} else if (note.ownerId === user.id) {
|
||||
// Owner
|
||||
return Permission.Owner
|
||||
} else {
|
||||
// Registered user
|
||||
switch (note.permission) {
|
||||
case 'editable':
|
||||
case 'limited':
|
||||
case 'freely':
|
||||
return Permission.Write
|
||||
case 'locked':
|
||||
case 'protected':
|
||||
return Permission.Read
|
||||
default:
|
||||
return Permission.None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function findNoteOrCreate (req: Request, res: Response, callback: (note: Note) => void): void {
|
||||
const id = req.params.noteId || req.params.shortid
|
||||
Note.parseNoteId(id, function (err, _id) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
}
|
||||
Note.findOne({
|
||||
where: {
|
||||
id: _id
|
||||
}
|
||||
}).then(function (note) {
|
||||
if (!note) {
|
||||
return newNote(req, res, '')
|
||||
}
|
||||
if (getPermission(req.user, note) === Permission.None) {
|
||||
return errors.errorForbidden(res)
|
||||
} else {
|
||||
return callback(note)
|
||||
}
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function isRevealTheme (theme: string): string | undefined {
|
||||
if (fs.existsSync(path.join(__dirname, '..', '..', '..', '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
|
||||
return theme
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function getPublishData (req: Request, res: Response, note, callback: (data) => void): void {
|
||||
const body = note.content
|
||||
const extracted = Note.extractMeta(body)
|
||||
const markdown = extracted.markdown
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
const createtime = note.createdAt
|
||||
const updatetime = note.lastchangeAt
|
||||
let title = Note.decodeTitle(note.title)
|
||||
title = Note.generateWebTitle(meta.title || title)
|
||||
const ogdata = Note.parseOpengraph(meta, title)
|
||||
const data = {
|
||||
title: title,
|
||||
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
|
||||
viewcount: note.viewcount,
|
||||
createtime: createtime,
|
||||
updatetime: updatetime,
|
||||
body: markdown,
|
||||
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
|
||||
meta: JSON.stringify(extracted.meta),
|
||||
owner: note.owner ? note.owner.id : null,
|
||||
ownerprofile: note.owner ? PhotoProfile.fromUser(note.owner) : null,
|
||||
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
||||
lastchangeuserprofile: note.lastchangeuser ? PhotoProfile.fromUser(note.lastchangeuser) : null,
|
||||
robots: meta.robots || false, // default allow robots
|
||||
GA: meta.GA,
|
||||
disqus: meta.disqus,
|
||||
cspNonce: res.locals.nonce,
|
||||
dnt: req.headers.dnt,
|
||||
opengraph: ogdata
|
||||
}
|
||||
callback(data)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue