Files
doneit-web/src/app/models/beast-orm-function.ts
T
Peter Maquiran 1087e68690 add
2023-07-12 13:54:20 +01:00

64 lines
1.7 KiB
TypeScript

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, 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 {
const newKey = prefix() + key
const restoredData = JSON.parse(localStorage.getItem(newKey))
Object.assign(instance, restoredData);
return restoredData
}
}
export function SAVE({key, 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 {
const stringifyData = JSON.stringify(dataToSave)
const newKey = prefix() + key
localStorage.setItem(newKey, stringifyData)
}
}
export function DELETE({key, instance}) {
if(environment.storageProduction) {
const newKey = prefix() + SHA1(key).toString()
localStorage.removeItem(newKey)
} else {
const newKey = prefix() + key
localStorage.removeItem(newKey)
}
}