Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed Jan 6, 2018
1 parent c420fcd commit 22f6eb1
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 31 deletions.
53 changes: 29 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const _ = require("lodash");

const fs = require("fs");
const path = require("path");
const glob = require("glob");
const url = require("url");
const util = require("util");

Expand All @@ -16,6 +15,8 @@ const session = require("express-session");
const SQLiteStore = require("connect-sqlite3")(session);
const helpers = require("handlebars-helpers")();
const dateformat = require("helper-dateformat");
const Finder = require("fs-finder");
const moment = require("moment");

const paginator = new require("paginator")(48, 8);
const crypto = require("crypto");
Expand Down Expand Up @@ -200,38 +201,42 @@ app.get("/paste/:file", (req, res) => {
});
});

function fileListing(globPattern, pageTemplate, route, req, res) {
glob(globPattern, { cwd: config.imagePath }, (err, files) => {
let page = typeof req.params.page !== "undefined" ? parseInt(req.params.page) : 0;
page = Math.min(Math.max(0, page), files.length);
function fileListing(mask, pageTemplate, route, req, res) {
const finder = Finder.from(config.imagePath);
if (req.query.start) finder.date(">", moment(req.query.start).set({hours: 0, minutes: 0, seconds: 0, milliseconds: 0}).toISOString());
if (req.query.end) finder.date("<", moment(req.query.end).set({hours: 0, minutes: 0, seconds: 0, milliseconds: 0}).add(1, "day").toISOString());
const files = finder.findFiles(mask);

const paginationInfo = paginator.build(files.length, page);
let page = typeof req.params.page !== "undefined" ? parseInt(req.params.page) : 0;
page = Math.min(Math.max(0, page), files.length);

const fullFiles = _.reverse(_.sortBy(_.map(files, f => {
if (statCache[f]) return statCache[f];
const paginationInfo = paginator.build(files.length, page);

const stat = fs.statSync(`${config.imagePath}/${f}`);
const o = {
name: f,
size: stat.size,
mtime: stat.mtime
};
const fullFiles = _.reverse(_.sortBy(_.map(files, f => {
if (statCache[f]) return statCache[f];

statCache[f] = o;
const stat = fs.statSync(`${f}`);
const o = {
name: path.relative(config.imagePath, f),
size: stat.size,
mtime: stat.mtime
};

return o;
}), "mtime"));
statCache[f] = o;

res.render(pageTemplate, {
route,
paginationInfo,
pages: _.range(paginationInfo.first_page, paginationInfo.last_page),
files: _.slice(fullFiles, paginationInfo.first_result, paginationInfo.last_result + 1)
});
return o;
}), "mtime"));

res.render(pageTemplate, {
route,
query: url.parse(req.url).query,
paginationInfo,
pages: _.range(paginationInfo.first_page, paginationInfo.last_page + 1),
files: _.slice(fullFiles, paginationInfo.first_result, paginationInfo.last_result + 1)
});
}

app.get("/gallery/:page?", auth, (req, res) => fileListing("*.{jpg,png,gif,jpeg}", "gallery", "gallery", req, res));
app.get("/gallery/:page?", auth, (req, res) => fileListing("*.<(jpeg|jpg|png|gif)$>", "gallery", "gallery", req, res));
app.get("/list/:page?", auth, (req, res) => fileListing("*.*", "list", "list", req, res));

app.listen(config.listen);
47 changes: 43 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
"express-prom-bundle": "^2.2.0",
"express-session": "^1.15.6",
"express-static-gzip": "^0.2.2",
"fs-finder": "^1.8.1",
"handlebars-helpers": "^0.8.1",
"helper-dateformat": "^1.0.0",
"highlights": "^3.0.1",
"lodash": "^4.17.4",
"moment": "^2.20.1",
"paginator": "^1.0.0",
"sanitize-filename": "^1.6.1"
}
Expand Down
1 change: 1 addition & 0 deletions views/gallery.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{> navigation }}
</div>
<div class="ui content center aligned">
{{> filters }}
{{> pagination }}
</div>
<div class="ui content">
Expand Down
26 changes: 26 additions & 0 deletions views/layouts/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="utf-8" />

<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css" rel="stylesheet">
<link href="https://cdn.rawgit.com/mdehoog/Semantic-UI-Calendar/76959c6f7d33a527b49be76789e984a0a407350b/dist/calendar.min.css" rel="stylesheet" type="text/css" />

<style>
body {
Expand All @@ -20,6 +21,8 @@
{{{body}}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.js"></script>
<script src="https://cdn.rawgit.com/mdehoog/Semantic-UI-Calendar/76959c6f7d33a527b49be76789e984a0a407350b/dist/calendar.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qs/6.5.1/qs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery_lazyload/1.9.7/jquery.lazyload.min.js"></script>
<script>
$(function() {
Expand All @@ -43,6 +46,29 @@
$("input:text", file.parent()).val(name);
});
$("#rangestart").calendar({
type: "date",
endCalendar: $("#rangeend")
});
$("#rangeend").calendar({
type: "date",
startCalendar: $("#rangestart")
});
$(".ui.accordion").accordion();
$("#filter-submit").click(function() {
const q = Qs.parse(location.search);
q.start = $("#rangestart").calendar("get date");
q.end = $("#rangeend").calendar("get date");
location.search = Qs.stringify(q);
});
$("#filter-clear").click(function() {
location.search = "";
});
});
</script>
</body>
Expand Down
1 change: 1 addition & 0 deletions views/list.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{> navigation }}
</div>
<div class="ui content center aligned">
{{> filters }}
{{> pagination }}
</div>
<div class="ui content">
Expand Down
34 changes: 34 additions & 0 deletions views/partials/filters.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div class="ui styled fluid accordion" style="max-width: 400px; margin: 0 auto 1rem;">
<div class="title" style="border-top: none !important;">
<i class="dropdown icon"></i>
Filters
</div>
<div class="content">
<div class="three fields">
<div class="two fields">
<div class="field">
<label>Start date</label>
<div class="ui calendar" id="rangestart">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input type="text" placeholder="Start">
</div>
</div>
</div>
<br/>
<div class="field">
<label>End date</label>
<div class="ui calendar" id="rangeend">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input type="text" placeholder="End">
</div>
</div>
</div>
<br/>
<button class="ui button" id="filter-clear">Clear</button>
<button class="ui primary button" id="filter-submit">Filter</button>
</div>
</div>
</div>
</div>
6 changes: 3 additions & 3 deletions views/partials/pagination.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<b>{{addCommas paginationInfo.total_results}}</b> total results - <b>{{addCommas paginationInfo.total_pages}}</b> pages
</p>
<div class="ui buttons">
<a href="/{{@root.route}}/{{paginationInfo.previous_page}}" class="ui icon button {{#unless paginationInfo.has_previous_page}}disabled{{/unless}}">
<a href="/{{@root.route}}/{{paginationInfo.previous_page}}{{#if @root.query}}?{{@root.query}}{{/if}}" class="ui icon button {{#unless paginationInfo.has_previous_page}}disabled{{/unless}}">
<i class="left chevron icon"></i>
</a>
{{#each pages}}
<a href="/{{@root.route}}/{{this}}" class="ui button {{#is @root.paginationInfo.current_page this}}disabled{{/is}}">{{this}}</a>
<a href="/{{@root.route}}/{{this}}{{#if @root.query}}?{{@root.query}}{{/if}}" class="ui button {{#is @root.paginationInfo.current_page this}}disabled{{/is}}">{{this}}</a>
{{/each}}
<a href="/{{@root.route}}/{{paginationInfo.next_page}}" class="ui right icon button {{#unless paginationInfo.has_next_page}}disabled{{/unless}}">
<a href="/{{@root.route}}/{{paginationInfo.next_page}}{{#if @root.query}}?{{@root.query}}{{/if}}" class="ui right icon button {{#unless paginationInfo.has_next_page}}disabled{{/unless}}">
<i class="right chevron icon"></i>
</a>
</div>

0 comments on commit 22f6eb1

Please sign in to comment.