This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
186 lines (154 loc) · 5.44 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const express = require('express')
const cors = require('cors')
const compression = require('compression')
//const cacheControl = require('express-cache-controller')
const logger = require('morgan')
const os = require('os')
const fs = require('fs')
const package = require('./package.json')
const node_env = process.env.NODE_ENV || 'development'
/**
* Transform milliseconds to Day Hours Minutes Seconds
* @param milli
* @returns {string}
*/
const getTimeString = (milli) => {
let d, h, m, s, ms;
s = Math.floor(milli / 1000);
m = Math.floor(s / 60);
s = s % 60; s = s < 10 ? '0' + s : s;
h = Math.floor(m / 60);
m = m % 60; m = m < 10 ? '0' + m : m;
d = Math.floor(h / 24);
h = h % 24; h = h < 10 ? '0' + h : h;
ms = Math.floor((milli % 1000) * 1000) / 1000;
return `${d}d ${h}:${m}:${s}`;
}
/**
* Remove duplicate entries in array
* @param Array array
* @returns {array[]}
*/
const removeDuplicate = array => [...new Set(array)];
/**
* Gete Quiver Notebooks and Notes
* Format result as a array<JSON>
* @return {array}
*/
const getQuiverNoteBooks = () => {
const result = []
const path = 'data'; // Path of Quiver Notebooks
// Create a All notes notebook where we will push all notes and tag from the whole notebooks
result.push({
'name': 'All notes',
'notes': [],
'tags': []
})
// Get notebooks from path
const notebooks = fs.readdirSync(path)
// Get notes from notebooks
notebooks.map(notebook => {
// Return if object is not a notebook
if (!notebook.includes('qvnotebook')) return
let tags = []
// Read notebooks meta.json
const notebookMeta = JSON.parse(fs.readFileSync(`${path}/${notebook}/meta.json`, 'utf8'))
// Read notes inside notebook
const notes = fs.readdirSync(`${path}/${notebook}`)
// Read notes meta.json
const notesObj = []
notes.map(note => {
/// Return if object is not a note
if (!note.includes('qvnote')) return
const noteMeta = JSON.parse(fs.readFileSync(`${path}/${notebook}/${note}/meta.json`, 'utf8'))
notesObj.push({
'title': noteMeta.title,
'uuid': noteMeta.uuid,
'dir': `${path}/${notebook}/${note}`,
'created_at': noteMeta.created_at,
'updated_at': noteMeta.updated_at,
'tags': noteMeta.tags
})
// Add tags in tags array only if there is more than one
if (noteMeta.tags.length >0) tags = [...tags, ...noteMeta.tags]
})
// Format the result
result.push({
'name': notebookMeta.name,
'uuid': notebookMeta.uuid,
'dir': `${path}/${notebook}`,
'notes': notesObj.sort((a, b) => a.title.localeCompare(b.title)),
'tags': removeDuplicate(tags.sort())
})
// Push notes and tags in All notes notebook
result[0].notes = [...result[0].notes, ...notesObj]
result[0].notes = result[0].notes.sort((a, b) => a.title.localeCompare(b.title))
result[0].tags = [...result[0].tags, ...tags]
result[0].tags = removeDuplicate(result[0].tags.sort())
})
return result
}
/**
* Gets note by full path
* @param path
* @returns {}
*/
const getQuiverNote = (path) => {
const noteMeta = JSON.parse(fs.readFileSync(`${path}/meta.json`, 'utf8')) // TODO: already inside React APP, should not be re-read here
// Replace Quiver img URL by real image path
let noteStr = fs.readFileSync(`${path}/content.json`, 'utf8')
noteStr = noteStr.replace(/quiver-image-url/g, `${path}/resources`)
const noteContent = JSON.parse(noteStr)
return {
title: noteMeta.title,
tags: noteMeta.tags,
created_at: noteMeta.created_at,
updated_at: noteMeta.updated_at,
cells: noteContent.cells
}
}
const app = express()
app.use(compression())
//app.use(cacheControl(node_env === 'production' ? { maxAge: 15768000 } : { noCache:true }));
app.use(cors())
app.use(logger(node_env === 'production' ? 'combined':'dev'))
app.use(express.static('app'))
app.use('/data', express.static('data'))
app.get('/', (req, res, next) => {
res.send('Hello Quiver Node Note')
})
app.get('/app-info', (req, res, next) => {
res.json({
name: package.name,
description: package.description,
version: package.version,
author: package.author,
repository: package.repository,
os: {
platform: os.platform(),
arch: os.arch(),
release: os.release(),
hostname: os.hostname(),
type: os.type(),
cpuload: os.loadavg(),
usedmem: Math.round((os.totalmem() - os.freemem()) / 1024 / 1024),
totalmem: Math.round(os.totalmem() / 1024 / 1024),
uptime: getTimeString(os.uptime() * 1000)
}
})
})
app.get('/quiver/notebooks', (req, res, next) => {
const notebooks = getQuiverNoteBooks();
res.json(notebooks)
})
app.get('/quiver/note/:path', (req, res, next) => {
const note = getQuiverNote(req.params.path)
res.json(note)
})
// catch 404 and forward to error handler
app.use((req, res, next) => {
res.status(404).json({status: 'error', msg: 'Not found', url: req.url})
})
app.listen(node_env === 'production' ? 8080 : 3000, () => {
if (node_env === 'development') console.log('Server running on http://localhost:3000')
})