Files
doneit-web/src/app/models/beast-orm-function.ts
T

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-10 12:32:01 +01:00
import { AES, SHA1, enc } from "crypto-js";
import { environment } from 'src/environments/environment'
function prefix() {
return environment.version.lastCommitNumber + environment.id+"-";
}
export function GET({key, localStorage, instance}) {
if(environment.storageProduction) {
try {
const newKey = prefix() + SHA1(key).toString()
const cipherText = localStorage.getItem(newKey)
const bytes = AES.decrypt(cipherText, newKey)
var decryptedData = bytes.toString(enc.Utf8);
const restoredData = JSON.parse(decryptedData)
Object.assign(instance, restoredData);
return restoredData
} catch(error) {
console.log(error)
return {}
}
} else {
return JSON.parse(localStorage.getItem(prefix() + key))
}
}
export function SAVE({key, localStorage, instance, dataToSave}) {
if(environment.storageProduction) {
const newKey = prefix() + SHA1(key).toString()
const stringifyData = JSON.stringify(dataToSave)
const cipherText = AES.encrypt(stringifyData, newKey).toString();
localStorage.setItem(newKey, cipherText)
} else {
2023-07-10 14:12:56 +01:00
const stringifyData = JSON.stringify(dataToSave)
localStorage.setItem(prefix() + key, stringifyData)
2023-07-10 12:32:01 +01:00
}
}
export function DELETE({key, localStorage, instance}) {
if(environment.storageProduction) {
const newKey = prefix() + SHA1(key).toString()
localStorage.removeItem(newKey)
} else {
localStorage.removeItem(prefix() + key)
}
}