Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

Commit

Permalink
Added Backup Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
izu-co authored Nov 7, 2020
2 parents a91af2c + 4e259c3 commit 3ad879a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
25 changes: 19 additions & 6 deletions backend/cache.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {cacheInterface, filePathsInterface, IntroSkipInterface, LoginInterface, SettingsInterface, StatusInterface} from "../interfaces"
import * as fs from "fs"
import * as path from "path"

function readCache(paths:filePathsInterface):cacheInterface {
const startCache:cacheInterface = {
"introSkipPositions": <IntroSkipInterface> readFile(paths.introSkips.path),
"logins": <LoginInterface> readFile(paths.logins.path),
"settings": <SettingsInterface> readFile(paths.settings.path),
"status": <StatusInterface> readFile(paths.status.path)
"introSkips": <IntroSkipInterface> (paths.introSkips.hasOwnProperty("backup") ? readFile(paths.introSkips.backup) : readFile(paths.introSkips.path)),
"logins": <LoginInterface> (paths.logins.hasOwnProperty("backup") ? readFile(paths.logins.backup) : readFile(paths.logins.path)),
"settings": <SettingsInterface> (paths.settings.hasOwnProperty("backup") ? readFile(paths.settings.backup) : readFile(paths.settings.path)),
"status": <StatusInterface> (paths.status.hasOwnProperty("backup") ? readFile(paths.status.backup) : readFile(paths.status.path))
}
return startCache;
}

function saveCache(cache:cacheInterface, paths: filePathsInterface):void {
saveFile(paths.introSkips.path, cache.introSkipPositions)
saveFile(paths.introSkips.path, cache.introSkips)
saveFile(paths.logins.path, cache.logins)
saveFile(paths.settings.path, cache.settings)
saveFile(paths.status.path, cache.status)
Expand All @@ -22,6 +23,12 @@ function saveFile(path:fs.PathLike, data:object) {
fs.writeFileSync(path, JSON.stringify(data, null, 4))
}

function getBackupPath(filePath: string) : string {
let pathArr = filePath.split(path.sep).reverse()[0].split(".")
pathArr.splice(1, 0, "backup")
return path.resolve(filePath, "..", pathArr.join("."))
}

function readFile(path:fs.PathLike):object {
let ret:object

Expand All @@ -34,4 +41,10 @@ function readFile(path:fs.PathLike):object {
return ret;
}

export {readCache, saveCache}
function backupCache(cache:cacheInterface, paths: filePathsInterface):void {
for (let key in paths) {
saveFile(getBackupPath(paths[key]["path"].toString()), cache[key]);
}
}

export {readCache, saveCache, backupCache, getBackupPath}
2 changes: 1 addition & 1 deletion backend/fileStuff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function saveData(data:StatusInterface) {
index.cache.status = data
}
function loadSkips(): IntroSkipInterface {
return index.cache.introSkipPositions;
return index.cache.introSkips;
}

async function readdir(path:string) {
Expand Down
9 changes: 2 additions & 7 deletions backend/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Updater {
}

downloadUpdate(update: Update, downloadPreRelase = false, overwrite= true) {
let updateFun = this.update
if (!downloadPreRelase && update.preRelease)
return;
if (!fs.existsSync("update.zip") || overwrite) {
Expand Down Expand Up @@ -111,22 +110,18 @@ class Updater {
if (!fs.existsSync(resolve(entry.path, "..")))
fs.mkdirSync(resolve(entry.path, ".."))
entry.pipe(fs.createWriteStream(entry.path))
}
} else
entry.autodrain()
break;
case FileSettings.Override:
if (!fs.existsSync(resolve(entry.path, "..")))
fs.mkdirSync(resolve(entry.path, ".."))
entry.pipe(fs.createWriteStream(entry.path))
break;
}
/*if (entry.path.startsWith("data") && fs.existsSync(entry.path))
entry.autodrain()
else
entry.pipe(fs.createWriteStream(entry.path))*/
})
})
stream.on("close", () => {
//fs.unlinkSync(file.path)
console.log("[INFO][Update] Update compleated. Please restart.")
})
}
Expand Down
10 changes: 9 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (!fs.existsSync(argv["Video Directory"])) {

let VideoNameExtensions = ["mp4"]

import { readCache, saveCache } from "./backend/cache"
import { backupCache, readCache, saveCache } from "./backend/cache"

const startCache = readCache(filePaths)

Expand All @@ -33,6 +33,14 @@ function saveCacheRegular() {
}, 1000 * 10);
}

backupCacheRegual()
function backupCacheRegual() {
backupCache(startCache, filePaths)
setTimeout(() => {
backupCacheRegual()
}, 1000 * 60 * 10);
}

import router from "./routes/index"
import { init } from "./routes/ExpressUses";
import * as fileStuff from "./backend/fileStuff";
Expand Down
5 changes: 3 additions & 2 deletions interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ export interface SettingsInterface {
}

export interface cacheInterface {
"introSkipPositions": IntroSkipInterface
"introSkips": IntroSkipInterface
"logins": LoginInterface
"settings": SettingsInterface,
"status": StatusInterface
}

export interface pathCheck {
"path": PathLike,
"exists": boolean
"exists": boolean,
"backup"?: PathLike
}
export interface filePathsInterface {
"introSkips": pathCheck,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"@types/yargs": "^15.0.8"
},
"scripts": {
"prebuild": "del .\\build /s /Q /F && rmdir .\\build /Q /S || exit 0",
"prebuild": "IF EXIST build (rmdir .\\build /Q /S)",
"build": "tsc",
"postbuild": "robocopy . .\\build /e /xf *.ts tsconfig.json intros.json status.json .gitignore update.zip /xd build .git* node_modules || exit 0"
"postbuild": "robocopy . .\\build /e /xf *.ts tsconfig.json settings.json settings.backup.json logins.backup.json intros*.json status*.json .gitignore update.zip /xd test build .git* node_modules || exit 0"
},
"repository": {
"type": "git",
Expand Down
14 changes: 12 additions & 2 deletions yargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as yargs from "yargs";
import path from "path"
import fs from "fs"
import { filePathsInterface, settingsInterface } from "./interfaces";
import { getBackupPath } from "./backend/cache";

const argv = yargs
.option('Video Directory', {
Expand Down Expand Up @@ -72,13 +73,22 @@ const filePaths:filePathsInterface = {
},
}


for (let a in filePaths)
if (!filePaths[a]["exists"]) {
if (fs.existsSync(getBackupPath(filePaths[a]["path"].toString()))) {
filePaths[a] = {
path: filePaths[a]["path"],
backup: getBackupPath(filePaths[a]["path"].toString()),
exists: true
}
continue;
}
try {
fs.writeFileSync(filePaths[a]["path"], "{}")
} catch (e) {
console.log("[ERROR] The path for the '" + a + "' file was not found")
process.exit(1)
console.log("[ERROR] The path for the '" + a + "' file was not found")
process.exit(1)
}
}

Expand Down

0 comments on commit 3ad879a

Please sign in to comment.