-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFileStore.ts
More file actions
37 lines (32 loc) · 1017 Bytes
/
FileStore.ts
File metadata and controls
37 lines (32 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { promises as fsp } from 'fs';
import fs from 'fs';
import path from 'path'
import StoreLogger from './StoreLogger'
export default class FileStore {
directory: string
logger: StoreLogger
constructor(_directory: string, _logger: StoreLogger) {
this.directory = _directory;
this.logger = _logger;
}
public async save(id: number, message: string): Promise<any> {
this.logger.saving(id);
var fileFullName = this.getFileInfo(id);
await fsp.writeFile(fileFullName, message)
.then(() => this.logger.saved(id))
.catch((err: any) => this.logger.errorSaving(id))
}
public read(id: number): string {
this.logger.readingFilestore(id)
var fileFullName = this.getFileInfo(id);
var exists = fs.existsSync(fileFullName);
if(!exists) {
this.logger.didNotFind(id);
return ''
}
return fs.readFileSync(fileFullName, {encoding: 'ASCII'});
}
public getFileInfo(id: number) {
return path.join(__dirname, this.directory, `${id}.txt`)
}
}