Files
doneit-web/src/app/store/localstore.service.ts
T

95 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-07-18 18:56:53 +01:00
import { Injectable } from '@angular/core';
2021-07-18 20:20:30 +01:00
import { AES, enc, SHA1 } from 'crypto-js'
2022-12-23 12:23:16 +01:00
import { environment } from 'src/environments/environment';
2021-07-18 18:56:53 +01:00
@Injectable({
providedIn: 'root'
})
export class LocalstoreService {
2023-01-25 15:06:19 +01:00
private prefix = environment.version.lastCommitNumber + environment.id+"-";
2022-12-19 20:15:56 +01:00
private previewPrefix = 'v17-';
2021-07-18 18:56:53 +01:00
2023-01-24 15:56:47 +01:00
callbacks: {[key: string]: {
path: string,
funx: Function,
id: string
}} = {}
2021-07-18 20:52:14 +01:00
constructor() {
2021-07-30 11:45:18 +01:00
2021-07-18 20:52:14 +01:00
const key = SHA1('version').toString()
this.set(key, this.prefix)
}
2021-07-18 18:56:53 +01:00
2021-08-31 13:46:39 +01:00
getKey(keyName:string) {
2021-07-18 20:52:01 +01:00
return this.prefix + keyName
}
2023-01-05 12:11:50 +01:00
keyExist(keyName) {
keyName = this.getKey(keyName)
return !localStorage.getItem(keyName) === null
}
2021-07-18 20:52:14 +01:00
2021-08-31 13:46:39 +01:00
get( keyName:string, safe) {
2021-07-18 18:56:53 +01:00
2021-07-18 20:52:01 +01:00
keyName = this.getKey(keyName)
2021-07-18 20:20:30 +01:00
2021-07-30 11:45:18 +01:00
const ciphertext = localStorage.getItem(keyName)
2021-07-18 18:56:53 +01:00
2021-07-18 20:20:30 +01:00
const hashKey = SHA1(keyName).toString()
2021-07-18 18:56:53 +01:00
if(ciphertext) {
2021-07-18 20:20:30 +01:00
const bytes = AES.decrypt(ciphertext, hashKey)
var decryptedData = bytes.toString(enc.Utf8);
2021-07-18 20:52:01 +01:00
try {
return JSON.parse(decryptedData)
} catch {
return decryptedData;
2021-07-18 20:20:30 +01:00
}
2021-07-18 20:52:01 +01:00
2021-07-18 20:52:14 +01:00
} else {
return safe
2021-07-18 18:56:53 +01:00
}
}
2021-08-31 13:46:39 +01:00
set(keyName:string, value) {
2021-07-30 11:45:18 +01:00
2021-07-18 20:52:01 +01:00
keyName = this.getKey(keyName)
2021-07-18 18:56:53 +01:00
if(typeof(value) != 'string') {
value = JSON.stringify(value)
2021-07-18 20:20:30 +01:00
}
const hashKey = SHA1(keyName).toString()
2021-07-18 18:56:53 +01:00
const data = value
2021-07-18 20:20:30 +01:00
const encoded = AES.encrypt( data, hashKey).toString();
localStorage.setItem(keyName, encoded)
2021-07-18 18:56:53 +01:00
}
2021-08-31 13:46:39 +01:00
delete(keyName:string) {
2021-08-27 13:39:52 +01:00
keyName = this.getKey(keyName)
localStorage.removeItem(keyName)
}
2023-01-24 15:56:47 +01:00
private async change(changeType: 'set' | 'delete') {
const currentPath = window.location.pathname
for (const [key, value] of Object.entries(this.callbacks)) {
if(currentPath.startsWith(value.path)) {}
const dontRepeat = await value.funx({event:{type: changeType}})
if(dontRepeat) {
delete this.callbacks[key]
}
}
}
listener() {}
2021-07-18 18:56:53 +01:00
}
2021-07-20 19:18:16 +01:00
2021-09-03 15:50:13 +01:00
export const localstoreService = new LocalstoreService()