Browse Source

Always keep meta files and support for archiving of pastes

tags/v0.3.4^0
Kim Grytøyr 1 year ago
parent
commit
a86e506ac4

+ 2
- 0
.gitignore View File

@@ -1,5 +1,7 @@
# ignore data dir, except .gitkeep
data/pastes/*
data/archive/*
!data/pastes/.gitkeep
!data/archive/.gitkeep
*/node_modules


+ 0
- 0
data/archive/.gitkeep View File


+ 1
- 0
data/config.development.json View File

@@ -3,6 +3,7 @@
"uri_base": "http://localhost:3000",
"filename_length": 16,
"path": "/home/node/app/data/pastes/",
"archive_path": "/home/node/app/data/archive/",
"max_age": 0,
"default_age": 0
}

+ 1
- 0
data/config.production.json View File

@@ -3,6 +3,7 @@
"uri_base": "https://paste.grytoyr.io",
"filename_length": 16,
"path": "/home/node/app/data/pastes/",
"archive_path": "/home/node/app/data/archive/",
"max_age": 0,
"default_age": 0
}

+ 1
- 1
src/lib/config.js View File

@@ -2,7 +2,7 @@
const fs = require('fs');

const env = process.env.NODE_ENV || 'development';
const VERSION = "v0.3.3";
const VERSION = "v0.3.4";

exports.getConfig = () => {
const configFolder = process.env.CONFIG_PATH || '../data/';

+ 9
- 0
src/lib/paste.js View File

@@ -29,6 +29,10 @@ const getPaste = (pasteId) => {

if (fs.existsSync(config.path + pasteId + '.' + paste.metadata.extension)) {
paste.data = fs.readFileSync(config.path + pasteId + '.' + paste.metadata.extension);
} else {
if (fs.existsSync(config.archive_path + pasteId + '.' + paste.metadata.extension)) {
paste.archived = true;
}
}

return paste;
@@ -152,6 +156,11 @@ const add = (req, res, next) => {
// Age of 0 means no expiration.
metadata.expiresAt = req.body.age ? helpers.parseAge(req.body.age) : null;

// If submitter wants to keep this paste forever, set an archive flag
// so that the scheduler will move it to an archive instead of physically
// deleting the file
metadata.archive = req.body.archive == 1 ? true : false;

// Create .meta file
// TODO: Move this to function or module
fs.writeFileSync(config.path + filename + '.meta', JSON.stringify(metadata));

+ 13
- 2
src/lib/scheduler.js View File

@@ -14,13 +14,24 @@ exports.run = () => {
const file = pastes[i];
if (file.indexOf('.meta') !== -1) {
const metadata = JSON.parse(fs.readFileSync(config.path + file));

if (!fs.existsSync(config.path + metadata.id + '.' + metadata.extension)) {
// File probably archived..
continue;
}

const diff = (now - (config.max_age * 1000 * 60));

const aboveGlobalMaxAge = config.max_age > 0 && diff > metadata.timestamp;
const expired = metadata.expiresAt !== null && metadata.expiresAt < now;
if (aboveGlobalMaxAge || expired) {
// Delete file..
fs.unlinkSync(config.path + metadata.id + '.' + metadata.extension);
if (metadata.archive === true) {
// Move file to archive
fs.renameSync(config.path + metadata.id + '.' + metadata.extension, config.archive_path + metadata.id + '.' + metadata.extension);
} else {
// Physically delete file
fs.unlinkSync(config.path + metadata.id + '.' + metadata.extension);
}
}
}
}

+ 4
- 1
src/views/header.jade View File

@@ -2,7 +2,10 @@ h1 #{paste.id}
p.metadata
| pasted #[span(title="#{fullTimestamp}") #{postedAt}] by #{paste.submitter}.
if expiresAt
| expires #{expiresAt}
if paste.archive
| will be archived #{expiresAt}
if !paste.archive
| expires #{expiresAt}
| | #[a(href="#{rawUrl}") view raw]
if paste.contentType == "text/plain"
| | #[a#toggleWrapping(href="#") toggle wrapping]

Loading…
Cancel
Save