Browse Source

Always keep meta files and support for archiving of pastes

pull/4/head v0.3.4
Kim Grytøyr 3 years ago
parent
commit
a86e506ac4
  1. 2
      .gitignore
  2. 0
      data/archive/.gitkeep
  3. 1
      data/config.development.json
  4. 1
      data/config.production.json
  5. 2
      src/lib/config.js
  6. 9
      src/lib/paste.js
  7. 15
      src/lib/scheduler.js
  8. 5
      src/views/header.jade

2
.gitignore

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

0
data/archive/.gitkeep

1
data/config.development.json

@ -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
data/config.production.json

@ -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
}

2
src/lib/config.js

@ -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
src/lib/paste.js

@ -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));

15
src/lib/scheduler.js

@ -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);
}
}
}
}

5
src/views/header.jade

@ -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