Skip to content

Commit 590780a

Browse files
committed
add file retrival
1 parent e066c71 commit 590780a

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

create-tables.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,5 @@ CREATE TABLE IF NOT EXISTS `uploads` (
143143
`name` varchar(255),
144144
`file` blob
145145
);
146+
147+
CREATE UNIQUE INDEX IF NOT EXISTS `name_unique` on `uploads` (`name`);

src/handlers/news.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@ async function getNews({
55
page = 0,
66
category = -1,
77
before = -1,
8-
after = 1
8+
after = -1
99
}) {
1010
const queryHandler = this.server.queryHandler;
1111

1212
const articles = queryHandler.getNews({
1313
id,
1414
terms,
1515
page,
16-
category,
16+
category: category > -1 ? category - 1 : category,
1717
before,
1818
after
1919
});
2020

21-
this.socket.sendMessage({
22-
token,
23-
articles
24-
});
21+
this.socket.sendMessage({ token, articles });
2522
}
2623

2724
async function addNews({ token, title, category, body }) {}
@@ -30,4 +27,12 @@ async function editNews({ token, id, title, category, body }) {}
3027

3128
async function uploadFile({ token, name, file }) {}
3229

33-
module.exports = { getNews, addNews, editNews, uploadFile };
30+
async function getFile({ token, name }) {
31+
const queryHandler = this.server.queryHandler;
32+
let file = queryHandler.getFile(name);
33+
file = file ? file.toString('base64') : null;
34+
35+
this.socket.sendMessage({ token, file });
36+
}
37+
38+
module.exports = { getNews, addNews, editNews, uploadFile, getFile };

src/query-handler.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ class QueryHandler {
149149
'FROM `news` WHERE ' +
150150
'CASE WHEN :category > -1 THEN `category` = :category ELSE ' +
151151
'true END AND ' +
152-
'CASE WHEN LENGTH(:terms) > 0 THEN `title` LIKE :terms OR ' +
152+
'CASE WHEN LENGTH(:terms) > 2 THEN `title` LIKE :terms OR ' +
153153
'`body` LIKE :terms ELSE true END AND ' +
154154
'CASE WHEN :before > -1 AND :after > -1 THEN ' +
155155
'`date` < :before AND `date` >= :after ELSE true END ' +
156156
'ORDER BY `date` DESC ' +
157157
`LIMIT ${NEWS_PER_PAGE} OFFSET (:page * ${NEWS_PER_PAGE})`,
158-
getNews: 'SELECT * FROM `news` WHERE `id` = ?'
158+
getNews: 'SELECT * FROM `news` WHERE `id` = ?',
159+
getFile: 'SELECT `file` FROM `uploads` WHERE `name` = ?'
159160
};
160161

161162
for (const [name, statement] of Object.entries(this.statements)) {
@@ -459,7 +460,34 @@ class QueryHandler {
459460
delete query.id;
460461
query.terms = `%${query.terms.replace(/%|_/g, '')}%`;
461462

462-
return this.statements.searchNews.all(query);
463+
if (!query.page) {
464+
query.page = -1;
465+
}
466+
467+
if (typeof query.category === 'undefined') {
468+
query.category = -1;
469+
}
470+
471+
return this.statements.searchNews.all(query).map((article) => {
472+
const summary = article.summary.trim();
473+
const newLineAt = summary.indexOf('\n');
474+
475+
article.summary =
476+
newLineAt > -1
477+
? summary.slice(0, newLineAt).replace(/\.|!|\?$/, '')
478+
: summary;
479+
480+
article.summary = article.summary.replace(
481+
/[^a-z0-9\-=/$@!.,'"? :]/gi,
482+
''
483+
).trim();
484+
485+
return article;
486+
});
487+
}
488+
489+
getFile(name) {
490+
return this.statements.getFile.pluck().get(name);
463491
}
464492

465493
sync() {

0 commit comments

Comments
 (0)