mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
move store in local storage
This commit is contained in:
37
lib/dataStore.ts
Normal file
37
lib/dataStore.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { IStoreItem } from '../interfaces/IStoreItem'
|
||||
|
||||
const LOCAL_STORAGE_KEY = 'pdfer-data'
|
||||
|
||||
function readData(): IStoreItem[] {
|
||||
const dataString = localStorage.getItem(LOCAL_STORAGE_KEY)
|
||||
|
||||
let data = []
|
||||
|
||||
if (dataString) {
|
||||
data = JSON.parse(dataString)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
function storeData(data: IStoreItem[]): void {
|
||||
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(data))
|
||||
}
|
||||
|
||||
function withoutId(data: IStoreItem[], id: string) {
|
||||
return data.filter((item) => item.id !== id)
|
||||
}
|
||||
|
||||
export function addDocument(templateData: IStoreItem) {
|
||||
const data = [templateData, ...withoutId(readData(), templateData.id)]
|
||||
storeData(data)
|
||||
}
|
||||
|
||||
export function removeDocument(id: string) {
|
||||
const newData = withoutId(readData(), id)
|
||||
storeData(newData)
|
||||
}
|
||||
|
||||
export function getDocuments() {
|
||||
return readData()
|
||||
}
|
||||
8
lib/id.ts
Normal file
8
lib/id.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { createHash } from 'crypto'
|
||||
import { IDocProps } from '../interfaces/IDocProps'
|
||||
|
||||
export function getId(data: IDocProps) {
|
||||
let sum = createHash('sha1')
|
||||
sum.update(JSON.stringify(data))
|
||||
return sum.digest('hex')
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { promisify } from 'util'
|
||||
import { promises } from 'fs'
|
||||
import { exec } from 'child_process'
|
||||
import { v1 as uuidv1 } from 'uuid'
|
||||
import { getDirPath, getDocPath, getTexCmd } from './utils'
|
||||
|
||||
const execPromise = promisify(exec)
|
||||
@@ -10,7 +9,12 @@ async function copyToTemp(id: string, texDocument: string): Promise<void> {
|
||||
const dirPath = getDirPath(id)
|
||||
const docPath = getDocPath(id)
|
||||
|
||||
await promises.mkdir(dirPath)
|
||||
try {
|
||||
await promises.access(dirPath)
|
||||
} catch (_) {
|
||||
// well, seems it does not exists, let's create it
|
||||
await promises.mkdir(dirPath)
|
||||
}
|
||||
await promises.writeFile(docPath, texDocument)
|
||||
}
|
||||
|
||||
@@ -27,8 +31,7 @@ async function generateDoc(id: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (texDocument: string) {
|
||||
const id = uuidv1()
|
||||
export default async function (id: string, texDocument: string) {
|
||||
await copyToTemp(id, texDocument)
|
||||
await generateDoc(id)
|
||||
return id
|
||||
|
||||
15
lib/store.ts
15
lib/store.ts
@@ -1,15 +0,0 @@
|
||||
const storeDir = process.env.JSON_STORE || './pdfer-store/'
|
||||
|
||||
import { promisify } from 'util'
|
||||
|
||||
// @ts-ignore
|
||||
import JsonStore from 'json-fs-store'
|
||||
|
||||
const store = JsonStore(storeDir)
|
||||
|
||||
console.log(`using json-store at ${storeDir}`)
|
||||
|
||||
export const list = promisify(store.list)
|
||||
export const load = promisify(store.load)
|
||||
export const add = promisify(store.add)
|
||||
export const remove = promisify(store.remove)
|
||||
Reference in New Issue
Block a user