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 @@
1 1
 # ignore data dir, except .gitkeep
2 2
 data/pastes/*
3
+data/archive/*
3 4
 !data/pastes/.gitkeep
5
+!data/archive/.gitkeep
4 6
 */node_modules
5 7
 

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


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

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

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

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

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

@@ -2,7 +2,7 @@
2 2
 const fs = require('fs');
3 3
 
4 4
 const env = process.env.NODE_ENV || 'development';
5
-const VERSION = "v0.3.3";
5
+const VERSION = "v0.3.4";
6 6
 
7 7
 exports.getConfig = () => {
8 8
   const configFolder = process.env.CONFIG_PATH || '../data/';

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

@@ -29,6 +29,10 @@ const getPaste = (pasteId) => {
29 29
 
30 30
   if (fs.existsSync(config.path + pasteId + '.' + paste.metadata.extension)) {
31 31
     paste.data = fs.readFileSync(config.path + pasteId + '.' + paste.metadata.extension);
32
+  } else {
33
+    if (fs.existsSync(config.archive_path + pasteId + '.' + paste.metadata.extension)) {
34
+      paste.archived = true;
35
+    }
32 36
   }
33 37
 
34 38
   return paste;
@@ -152,6 +156,11 @@ const add = (req, res, next) => {
152 156
     // Age of 0 means no expiration.
153 157
     metadata.expiresAt = req.body.age ? helpers.parseAge(req.body.age) : null;
154 158
 
159
+    // If submitter wants to keep this paste forever, set an archive flag
160
+    // so that the scheduler will move it to an archive instead of physically
161
+    // deleting the file
162
+    metadata.archive = req.body.archive == 1 ? true : false;
163
+
155 164
     // Create .meta file
156 165
     // TODO: Move this to function or module
157 166
     fs.writeFileSync(config.path + filename + '.meta', JSON.stringify(metadata));

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

@@ -14,13 +14,24 @@ exports.run = () => {
14 14
       const file = pastes[i];
15 15
       if (file.indexOf('.meta') !== -1) {
16 16
         const metadata = JSON.parse(fs.readFileSync(config.path + file));
17
+
18
+        if (!fs.existsSync(config.path + metadata.id + '.' + metadata.extension)) {
19
+          // File probably archived..
20
+          continue;
21
+        }
22
+
17 23
         const diff = (now - (config.max_age * 1000 * 60));
18 24
 
19 25
         const aboveGlobalMaxAge = config.max_age > 0 && diff > metadata.timestamp;
20 26
         const expired = metadata.expiresAt !== null && metadata.expiresAt < now;
21 27
         if (aboveGlobalMaxAge || expired) {
22
-          // Delete file..
23
-          fs.unlinkSync(config.path + metadata.id + '.' + metadata.extension);
28
+          if (metadata.archive === true) {
29
+            // Move file to archive
30
+            fs.renameSync(config.path + metadata.id + '.' + metadata.extension, config.archive_path + metadata.id + '.' + metadata.extension);
31
+          } else {
32
+            // Physically delete file
33
+            fs.unlinkSync(config.path + metadata.id + '.' + metadata.extension);
34
+          }
24 35
         }
25 36
       }
26 37
     }

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

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

Loading…
Cancel
Save