+ // Moves older posts to the archive
+
+ async _archive() {
+ internals.debuglog('Archiving post');
+ const post = await this._readPost(0);
+ await this._ensureDirectoryExists(this.archiveDirectory);
+
+ const targetPath = join(this.archiveDirectory, post.id);
+
+ internals.debuglog(`Removing ${targetPath}`);
+ await rm(targetPath, { recursive: true, force: true });
+ internals.debuglog(`Adding ${post.location} to ${targetPath}`);
+ await this._ensureDirectoryExists(targetPath);
+ await cp(post.location, targetPath, { recursive: true });
+ internals.debuglog(`Added ${post.location} to ${targetPath}`);
+ }
+
+ // Attempts to read existing metadata. Otherwise generates new set.
+
+ async _getMetadata(index = 0) {
+
+ const metadataTarget = join(this.postsDirectory, String(index), internals.kMetadataFilename);
+
+ try {
+ internals.debuglog(`Looking for metadata at ${metadataTarget}`);
+ return JSON.parse(await readFile(metadataTarget, { encoding: 'utf8' }));
+ }
+ catch (e) {
+ internals.debuglog(`Metadata not found or unreadable. Generating new set.`);
+ const createdOn = Date.now();
+ const metadata = {
+ id: String(createdOn),
+ createdOn
+ };
+
+ return metadata;
+ }
+ }
+
+ // Writes metadata. Assumes post 0 since it only gets written
+ // on create
+
+ async _writeMetadata(metadata) {
+
+ const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename);
+ internals.debuglog(`Writing ${metadataTarget}`);
+ await writeFile(metadataTarget, JSON.stringify(metadata, null, 2));
+ }
+