2020-04-11 10:08:40 -04:00
|
|
|
import { Response } from 'express'
|
2020-04-10 16:06:12 -04:00
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
import { Note, Revision } from '../../models'
|
2020-04-12 07:11:06 -04:00
|
|
|
import { logger } from '../../logger'
|
|
|
|
import { config } from '../../config'
|
2020-04-12 11:39:00 -04:00
|
|
|
import { errors } from '../../errors'
|
2020-04-11 10:08:40 -04:00
|
|
|
import shortId from 'shortid'
|
|
|
|
import moment from 'moment'
|
|
|
|
import querystring from 'querystring'
|
2020-04-10 16:06:12 -04:00
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
export function getInfo (req: any, 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
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
2020-04-11 10:08:40 -04:00
|
|
|
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)
|
|
|
|
}
|
2020-04-10 16:06:12 -04:00
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
export function createGist (req: any, res: Response, note: Note): void {
|
|
|
|
const data = {
|
|
|
|
client_id: config.github.clientID,
|
|
|
|
redirect_uri: config.serverURL + '/auth/github/callback/' + Note.encodeNoteId(note.id) + '/gist',
|
|
|
|
scope: 'gist',
|
|
|
|
state: shortId.generate()
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
2020-04-11 10:08:40 -04:00
|
|
|
const query = querystring.stringify(data)
|
|
|
|
res.redirect('https://github.com/login/oauth/authorize?' + query)
|
|
|
|
}
|
2020-04-10 16:06:12 -04:00
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
export function getRevision (req: any, 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) {
|
2020-04-10 16:06:12 -04:00
|
|
|
if (err) {
|
|
|
|
logger.error(err)
|
2020-04-11 10:08:40 -04:00
|
|
|
errors.errorInternalError(res)
|
2020-04-12 08:37:20 -04:00
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
return
|
|
|
|
}if (!content) {
|
2020-04-11 10:08:40 -04:00
|
|
|
errors.errorNotFound(res)
|
2020-04-11 10:08:40 -04:00
|
|
|
return}
|
2020-04-10 16:06:12 -04:00
|
|
|
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
|
|
|
|
})
|
2020-04-11 10:08:40 -04:00
|
|
|
res.send(content)
|
2020-04-10 16:06:12 -04:00
|
|
|
})
|
2020-04-11 10:08:40 -04:00
|
|
|
} else {
|
2020-04-12 11:40:34 -04:00
|
|
|
errors.errorNotFound(res)
|
|
|
|
return
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
2020-04-11 10:08:40 -04:00
|
|
|
} else {
|
|
|
|
Revision.getNoteRevisions(note, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err)
|
2020-04-12 11:40:34 -04:00
|
|
|
errors.errorInternalError(res)
|
|
|
|
return
|
2020-04-11 10:08:40 -04:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
})
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
|
|
|
}
|